Regular Expression validator for date format DD/MMM/YYYY

<asp:TextBox ID="txtDob" runat="server" MaxLength="100" CssClass="textbox2" size="10"  />
<ajax:TextBoxWatermarkExtender ID="txtWEDOB" WatermarkText="DD/MON/YYYY" TargetControlID="txtDob" runat="server" />
<asp:RegularExpressionValidator ID="rfv" runat="server" ControlToValidate="txtDob" SetFocusOnError="true" 
ValidationExpression="^(3[0-1]|2[0-9]|1[0-9]|0[1-9])[\s{1}|\/|-](Jan|JAN|Feb|FEB|Mar|MAR|Apr|APR|May|MAY|Jun|JUN|Jul|JUL|Aug|AUG|Sep|SEP|Oct|OCT|Nov|NOV|Dec|DEC)[\s{1}|\/|-]\d{4}$"  
Display="None" ValidationGroup="search" ErrorMessage="Please enter correct date">
</asp:RegularExpressionValidator>

I am using this regular expression validator for date. It is not giving error for like 12-FEB-2010 or 12 Feb 2010. I strictly want 12/FEB/2010 format. This Regular expressii\on is not working for date like 29/FEB/2010. what changes need to be made.

Answers


It will be very complicated regular expression if you want to validate all the possible cases (like for example 30/FEB).

You will need sub-expression for months with 30 day, you will need a sub-expression for months with 31 day, and the most interesting is Feb and leap years. I don't think that using a regular expression is a good idea here.

[EDIT]

To the question in the comment:

^((31(?! (FEB|APR|JUN|SEP|NOV)))|((30|29)(?! FEB))|(29(?= FEB (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])/(JAN|FEB|MAR|MAY|APR|JUL|JUN|AUG|OCT|SEP|NOV|DEC)/((1[6-9]|[2-9]\d)\d{2})$

I assume you've cut-and-paste this regex from somewhere else?

This part ([\s{1}|\/|-]) is where your problem is. This allows space (\s) and hyphen (-), as well as the / character.

It should instead be \/. So your current regex

"^(3[0-1]|2[0-9]|1[0-9]|0[1-9])[\s{1}|\/|-](Jan|JAN|Feb|FEB|Mar|MAR|Apr|APR|May|MAY|Jun|JUN|Jul|JUL|Aug|AUG|Sep|SEP|Oct|OCT|Nov|NOV|Dec|DEC)[\s{1}|\/|-]\d{4}$

becomes

"^(3[0-1]|2[0-9]|1[0-9]|0[1-9])\/(Jan|JAN|Feb|FEB|Mar|MAR|Apr|APR|May|MAY|Jun|JUN|Jul|JUL|Aug|AUG|Sep|SEP|Oct|OCT|Nov|NOV|Dec|DEC)\/\d{4}$

edit: there was a typo (a missing () in the 'new' regex in this answer which I've fixed.

Be aware that this does not resolve any problems regarding the number of days in different months (e.g. 30 Feb). However I do not find that it matches "311apr}2011" as Hans has posited in the comments - any further comment?

This correctly addresses your question. I do agree with the comments below that regex are a terrible tool for date validation, but I'm providing an answer rather than trying to give you general advice.


Need Your Help

Trouble with jquery validate when using on checkboxes

jquery-validate

I am trying to use jquery validate to validate the form and I want the user to select atleast 1 checkbox. The reason for having different names is that they match with the database field names.

ClientBundle Resources Path in GWT

maven-2 gwt

I have a maven gwt project, and I'm using ClientBundle to package my image resources within the project. I would like to place the images in /src/main/resources as should be done in maven instead of

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.