I have a RegEx that validates a date coming in. What I want it to allow:
MM/dd/YYYY M/d/YYYY MM-dd-YYYY M-d-YYYY MM.dd.YYYY M.d.YYYY MMddYYYY
And a few other variants.
Here's my expression:
^((0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])[- /.]?(19|20)\d\d)|((((0?[13578])|(1[02]))[- /.]?((0?[1-9])|([12][0-9])|(3[01]))|((0?[469])|(11))[- /.]?((0?[1-9])|([12][0-9])|(30))|(0?[2])[- /.]?((0?[1-9])|([1][0-9])|([2][0-8])))[- /.]?(19\d{2}|20\d{2}))|(((0?[2]))[- /.]?((0?[1-9])|([12][0-9]))[- /.]?((19|20)(04|08|[2468][048]|[13579][26])|2000))$
I'm getting the majority to work, but the dates that I do not want to work is MdYYYY, MMdYYYY, or MddYYYY
I want the RegEx to be the only thing changed because it's being called in multiple spots for the same reason, limiting the amount of code I need to adjust.
I'm calling this RegEx from this Case statement which is in my custom TextBoxPlus.ascx:
Case TextBoxPlusType.DateOnlyMMDDYYYY
WatermarkText = "mmddyyyy"
ValidationExpression = "^((0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])[- /.]?(19|20)\d\d)|((((0?[13578])|(1[02]))[- /.]?((0?[1-9])|([12][0-9])|(3[01]))|((0?[469])|(11))[- /.]?((0?[1-9])|([12][0-9])|(30))|(0?[2])[- /.]?((0?[1-9])|([1][0-9])|([2][0-8])))[- /.]?(19\d{2}|20\d{2}))|(((0?[2]))[- /.]?((0?[1-9])|([12][0-9]))[- /.]?((19|20)(04|08|[2468][048]|[13579][26])|2000))$"
ErrorMessage = "Please enter a valid date format<br><b>mm/dd/yyyy<br>mmddyyyy</b>"
This is on the actual aspx.vb page calling TextBoxPlus (my custom control):
If (Not (Date.TryParseExact(IssueDate.Text, "MMddyyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None, New Date))) Then
If (Not (Date.TryParseExact(IssueDate.Text, "MM/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None, New Date))) Then
showIfBadDate.Visible = True
BadDate_AM.Show()
Else
IssueDate_ = Date.ParseExact(IssueDate.Text, "MM/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
End If
Else
IssueDate_ = Date.ParseExact(IssueDate.Text, "MMddyyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
End If