WizardsSleeve WizardsSleeve - 1 year ago 72
Vb.net Question

Regular Expression to match dates in dd/mm/yy format and check for valid values

Does anyone have a regurlar expression available which only accepts dates in the format dd/mm/yy but also has strict checking to make sure that the date is valid, including leap year support?

I am coding in vb.net and am struggling to work this one out.

Answer Source

I don't think the leap year support is doable in a regex without using some ugly regex.

You will have to check the date validity after validating input with the regex.

As hinted by Keeper, you could use the DateTime.ParseExact method to validate your date :

Public Function IsValidDate(ByVal dateString As String) As Boolean
        DateTime.ParseExact(dateString, "dd/MM/yy", System.Globalization.CultureInfo.InvariantCulture)
        Return True
    Catch ex As FormatException
        Return False
    End Try
End Function
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download