In this article I will explain how to validate a dd/MM/yyyy format date string in TextBox in ASP.Net.
The dd/MM/yyyy date format string can be validated in the following possible ways
1. Using RegularExpressionValidator.
2. Using CustomValidator and JavaScript.
3. Using CustomValidator with Server Side validation using C# and VB.Net.
 
 
1. Using RegularExpressionValidator
This is the simplest way to validate a dd/MM/yyyy date format string. And it does work brilliantly except the fact that it cannot validate for months which have less than 31 days for example 30/02/2014 will be considered as a valid date.
<asp:TextBox ID="txtDate1" runat="server" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtDate1" ValidationExpression="(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$"
    ErrorMessage="Invalid date format." ValidationGroup="Group1" />
<br />
<br />
<asp:Button ID="Button1" Text="Validate" runat="server" ValidationGroup="Group1" />
 
 
2. Using CustomValidator and JavaScript
The next approach is using a CustomValidator with JavaScript. The advantage of this approach is that here we can have dual validation i.e.
1. Whether the string is a valid string of format dd/MM/yyyy using Regular Expression.
2. If the above case is true then it is verified whether it is a valid date using JavaScript.
Thus with the above technique we can also quickly pick out invalid dates that the Regular Expression cannot detect.
<asp:TextBox ID="txtDate2" runat="server" Text="11/11/2011" />
<asp:CustomValidator runat="server" ClientValidationFunction="ValidateDate" ControlToValidate="txtDate2"
    ErrorMessage="Invalid Date." ValidationGroup="Group2" />
<br />
<br />
<asp:Button ID="Button2" Text="Validate" runat="server" ValidationGroup="Group2" />
<script type="text/javascript">
    function ValidateDate(sender, args) {
        var dateString = document.getElementById(sender.controltovalidate).value;
        var regex = /(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$/;
        if (regex.test(dateString)) {
            var parts = dateString.split("/");
            var dt = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
            args.IsValid = (dt.getDate() == parts[0] && dt.getMonth() + 1 == parts[1] && dt.getFullYear() == parts[2]);
        } else {
            args.IsValid = false;
        }
    }
</script>
 
 
3. Using CustomValidator with Server Side validation
This is similar to the previous approach with the only difference that the validation is performed on server side using the CustomValidator’s OnServerValidate event.
HTML
<u>3. Using CustomValidator with Server Side validation</u>
<hr />
<asp:TextBox ID="txtDate3" runat="server" Text="11/11/2011" />
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="ValidateDate"
    ControlToValidate="txtDate3" ErrorMessage="Invalid Date." ValidationGroup="Group3" />
<br />
<br />
<asp:Button ID="Button3" Text="Validate" runat="server" ValidationGroup="Group3" />
 
Namespaces
You will need to import the following namespaces.
C#
using System.Globalization;
using System.Text.RegularExpressions;
 
VB.Net
Imports System.Globalization
Imports System.Text.RegularExpressions
 
Code
C#
protected void ValidateDate(object sender, ServerValidateEventArgs e)
{
    if(Regex.IsMatch(txtDate3.Text, "(((0|1)[0-9]|2[0-9]|3[0-1])\\/(0[1-9]|1[0-2])\\/((19|20)\\d\\d))$")){
        DateTime dt;
        e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", new CultureInfo("en-GB"), DateTimeStyles.None, out dt);
        if (e.IsValid)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Valid Date.');", true);
        }
    }
    else{
        e.IsValid = false;
    }
}
 
VB.Net
Protected Sub ValidateDate(sender As Object, e As ServerValidateEventArgs)
    If Regex.IsMatch(txtDate3.Text, "(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$") Then
        Dim dt As DateTime
        e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", New CultureInfo("en-GB"), DateTimeStyles.None, dt)
        If e.IsValid Then
            ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Valid Date.');", True)
        End If
    Else
        e.IsValid = False
    End If
End Sub
 
 
Demo
 
Downloads