How to Validate DateTime DataType with Culture Based Value in Asp.Net C# VB.Net

In most cases Asp.Net C# VB.Net developers need to check or validate user input for DateTime value or DataType. Asp.net C# provides us an easy way to validate those user inputs from server side. Here in this small article I will show you how one can validate DateTime using Asp.Net. Basically TryParse method will help us to check user DateTime inputs. It Converts the specified string representation of a DateTime to its equivalent DateTime and returns a value that indicates whether the conversion succeeded or not. In the below example I will show you how to validate DateTime values aside from client end validation.

Output/Objective:
How to Validate DateTime DataType value in Asp.Net Csharp VB.net

To do the Example First Create a Simple UI:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="lbl_Datetime" runat="server" Text="Enter datetime: "></asp:Label>
    <asp:TextBox runat="server" ID="txt_Datetime"></asp:TextBox>
    <asp:Button ID="cmd_Datetime" runat="server" Text="Click to check" OnClick="cmd_Datetime_Click" /><br />
    <asp:Label ID="lbl_datetime_result" runat="server" Font-Bold="True"></asp:Label>
</asp:Content>

Now Validate the DateTime DataType under Button Click event:
C# Code:

    protected void cmd_Datetime_Click(object sender, EventArgs e)
    {
        DateTime check_Datetime;
        DateTimeStyles styles;
        styles = DateTimeStyles.AssumeUniversal;

        if (DateTime.TryParse(txt_Datetime.Text, CultureInfo.CreateSpecificCulture("en-US"),styles, out check_Datetime))
            lbl_datetime_result.Text = "Datetime Validation Successfull.";
        else
            lbl_datetime_result.Text = "Invalid Datetime!!";
    }

Note: Don’t forget to add “System.Globalization” namespace. If you want to omit culture then just remove CultureInfo & styles parameter.

VB.Net Code:

    Protected Sub cmd_Datetime_Click(sender As Object, e As EventArgs)
        Dim check_Datetime As DateTime
        Dim styles As DateTimeStyles
        styles = DateTimeStyles.AssumeUniversal

        If DateTime.TryParse(txt_Datetime.Text, CultureInfo.CreateSpecificCulture("en-US"), styles, check_Datetime) Then
            lbl_datetime_result.Text = "Datetime Validation Successfull."
        Else
            lbl_datetime_result.Text = "Invalid Datetime!!"
        End If
    End Sub

Note: Don’t forget to add “System.Globalization” namespace. If you want to omit culture then just remove CultureInfo & styles parameter.

Hope now you can validate any DateTime value using asp.net from server side. Server side validation is also a good practice along with client side validation.

More about Culture & Style:
http://msdn.microsoft.com/en-us/library/9h21f14e(v=vs.110).aspx

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

     

*