views:

72

answers:

2

I am using the below Regular Expression which works fine in case of date validation. But if I select the date from the DatePicket even then it throws alert:Enter valid date. How can I modify the regular expression so as to be compatible with DatePicker.

Set RegularExpressionObject = New RegExp

With RegularExpressionObject 
    .Pattern = "^(((0?[1-9]|[12]\d|3[01]).-\/.-\/?\d{2}))|((0?[1-9]|[12]\d|30).-\/.-\/?\d{2}))|((0?[1-9]|1\d|2[0-8])[.-\/]0?2.-\/?\d{2}))|(29[.-\/]0?2.-\/?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$" 
    .IgnoreCase = True 
    .Global = True 
End With 
expressionmatch = RegularExpressionObject.Test(TxtVal)

If expressionmatch Then

Else 
    msgbox "You must enter a valid Date.",,"Invalidentry" 
    form1.item(OHldr).focus()
    Exit Function 
End if
A: 

I know it's not what you're asking, but there's an easier way to validate datetimes. Regular Expressions are good, but sometimes there is an easier way.

Use a CustomeValidator, and in the Server_Validate event, do the following:

bool IsValidDate = false;
if(System.DateTime.TryParse(SomeValue))
{
   IsValidDate = true;
}

return IsValidDate;
David Stratton
A: 

You can use If Date.TryParse(s, d) Then ... where s is a string and d is a date. date.tryparse returns true for a valid date and false for an invalid date. If the date is valid, it converts it to a date datatype in d.

xpda