Sunday 29 December 2013

How to Validate Date in Different(Any) formats using C#.NET/VB.NET

Hi friends,in this article i will explain about How to Validate Date in Different(Any) formats using C#.NET/VB.NET.
I already explained in the previous articles about Move Gridview Rows Up and Down by using JQuery in ASP.NET using C#.NET/VB.NET,How to Get User IP address in ASP.NET using C#/VB.NET and ASP.NET How to get all file names in a folder using C#/VB.NET
Write the below code in the .aspx page
ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Validate Date in Different formats using C#.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    Start Date
                </td>
                <td align="center" >
                    :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtStartDate"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    End Date
                </td>
                <td align="center">
                    :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtEndDate"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td ></td><td>
                    <asp:Button runat="server" ID="btnCheck" Text="Check"
                        onclick="btnCheck_Click"   />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


In C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ValidateDatesWithanyFormat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    bool validateDate()
    {
        bool bval = true;
        DateTime dt;
        if (!DateTime.TryParse(txtStartDate.Text, out dt))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Invalid", "alert('Enter Valid Start Date');", true);
            bval = false;
        }

        if (!DateTime.TryParse(txtEndDate.Text, out dt))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Invalid", "alert('Enter Valid End Date');", true);
            bval = false;
        }

        if (bval == true)
        {
            DateTime dtStartDate = Convert.ToDateTime(txtStartDate.Text); // start date in textbox
            DateTime dtEndDate = Convert.ToDateTime(txtEndDate.Text);     // end date in textbox
            if (dtStartDate > dtEndDate)
            {

                ClientScript.RegisterStartupScript(this.GetType(), "Invalid", "alert('Enter End Date is Greater than Start Date');", true);

                bval = false;
            }

            DateTime SqlminDate = new DateTime(1900, 1, 1);

            if (SqlminDate > dtStartDate || SqlminDate > dtEndDate)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Invalid", "alert('Date greater than 1900');", true);
                bval = false;
            }

        }
        return bval;

    }
    protected void btnCheck_Click(object sender, EventArgs e)
    {
        validateDate();
    }
}


In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class ValidateDatesWithanyFormat
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub
    Private Function validateDate() As Boolean
        Dim bval As Boolean = True
        Dim dt As DateTime
        If Not DateTime.TryParse(txtStartDate.Text, dt) Then
            ClientScript.RegisterStartupScript(Me.[GetType](), "Invalid", "alert('Enter Valid Start Date');", True)
            bval = False
        End If

        If Not DateTime.TryParse(txtEndDate.Text, dt) Then
            ClientScript.RegisterStartupScript(Me.[GetType](), "Invalid", "alert('Enter Valid End Date');", True)
            bval = False
        End If

        If bval = True Then
            Dim dtStartDate As DateTime = Convert.ToDateTime(txtStartDate.Text)
            ' start date in textbox
            Dim dtEndDate As DateTime = Convert.ToDateTime(txtEndDate.Text)
            ' end date in textbox
            If dtStartDate > dtEndDate Then

                ClientScript.RegisterStartupScript(Me.[GetType](), "Invalid", "alert('Enter End Date is Greater than Start Date');", True)

                bval = False
            End If

            Dim SqlminDate As New DateTime(1900, 1, 1)

            If SqlminDate > dtStartDate OrElse SqlminDate > dtEndDate Then
                ClientScript.RegisterStartupScript(Me.[GetType](), "Invalid", "alert('Date greater than 1900');", True)
                bval = False

            End If
        End If
        Return bval

    End Function
    Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs)
        validateDate()
    End Sub
End Class




The output of the above page as shown in the below figure.If you enter end date is not greater than start Date then it will show Enter End Date is Greater than Start Date.

How to Validate Date

When you enter characters or numbers and what you enter that will not in the date format then it show Enter Valid Date
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.