In this article I will explain with an example, how to perform Date format validation (dd/MM/yyyy) using Regular Expression in C# and VB.Net.
The User will enter Date in TextBox and when the Button is clicked, the Date will be tested against the Regular Expression for dd/MM/yyyy format and if invalid, a Message Box will be displayed.
 
 
Form Design
The Form consists of a TextBox control and a Button. The Button has been assigned Click event handler.
Date format (dd/MM/yyyy) validation using Regular Expression in C# and VB.Net
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Globalization;
using System.Text.RegularExpressions;
 
VB.Net
Imports System.Globalization
Imports System.Text.RegularExpressions
 
 
Validate Date dd/MM/yyyy format in TextBox using Regular Expression in C# and VB.Net
When the Button is clicked, the Date from the TextBox is tested against the Regular Expression using the IsMatch function.
Once the dd/MM/yyyy format is validated, the Date is verified by converting to DateTime object using DateTime.TryParseExact function to make sure whether it is a valid date.
If the Date is found invalid an error message is displayed using MessageBox.
C#
private void btnValidate_Click(object sender, EventArgs e)
{
    Regex regex = new Regex(@"(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$");
           
    //Verify whether date entered in dd/MM/yyyy format.
    bool isValid = regex.IsMatch(txtDate.Text.Trim());
 
    //Verify whether entered date is Valid date.
    DateTime dt;
    isValid = DateTime.TryParseExact(txtDate.Text, "dd/MM/yyyy", new CultureInfo("en-GB"), DateTimeStyles.None, out dt);
    if (!isValid)
    {
        MessageBox.Show("Invalid Date.");
    }
}
 
VB.Net
Private Sub btnValidate_Click(sender As System.Object, e As System.EventArgs) Handles btnValidate.Click
    Dim regex As Regex = New Regex("(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$")
 
    'Verify whether date entered in dd/MM/yyyy format.
    Dim isValid As Boolean = regex.IsMatch(txtDate.Text.Trim)
 
    'Verify whether entered date is Valid date.
    Dim dt As DateTime
    isValid = DateTime.TryParseExact(txtDate.Text, "dd/MM/yyyy", New CultureInfo("en-GB"), DateTimeStyles.None, dt)
    If Not isValid Then
        MessageBox.Show("Invalid Date.")
    End If
End Sub
 
 
Screenshot
Date format (dd/MM/yyyy) validation using Regular Expression in C# and VB.Net
 
 
Downloads