Bharath Radhekrishna

Regular expression validator for date format dd MMM yy and dd MMM yyyy

Posted by: onetidbit on: May 21, 2008

<asp:RegularExpressionValidator ID=”RegularExpressionValidator1″ runat=”server” ControlToValidate=”TextBox1″ ErrorMessage=”Incorrect format” ValidationExpression=”^(d{0}|(31(?!(Feb|feb|Apr|apr|Jun|jun|Sep|
sep|Nov|nov)))|((30|29)(?!Feb|feb))|(29(?=Feb|feb(((1[6-9]|[2-9]\
d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00
)))))|(29(?=Feb|feb(((0[48]|[2468][048]|[13579]
[26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])[- ](Jan|jan|feb|Feb|mar|Mar|may|May|apr|Apr|jul|Jul|Jun|jun|Aug|aug|
Oct|oct|Sep|sep|Nov|nov|dec|Dec)[- ]((1[6-9]|[2-9]\d)\d{2}|\d{2}|d{0})$”></asp:RegularExpressionValidator>

2 Responses to "Regular expression validator for date format dd MMM yy and dd MMM yyyy"

You can try using a custom validator instead. Here’s some code that you might use to accomplish this:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
DateTime result;
if (!DateTime.TryParse(args.Value, out result))
{
args.IsValid = false;
return;
} else args.IsValid = true;
}

the script is a nice and effective. I used it and found a very suefull, except a problem. It does not adjust starting spaces. For example it will accept “space space Jan 2009″ or ” space JAN 2009″

Leave a Reply