[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regular expression for a date. This is for ASP .NET regular expression validator

Posted on 2009-04-28
3
Medium Priority
?
255 Views
Last Modified: 2012-05-06
I need a regular expression for a date. The date can be in one of the three formats
Where the user must enter four digits for the year. "mm" and "dd" are numeric and are mandatory.

mm/dd/yyyy
mm-dd-yyyy
mm dd yyyy
0
Comment
Question by:brgdotnet
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 1040 total points
ID: 24256994
You can try it in one fell swoop if you want the capturing groups to always be $1, $2, and $3:

^([01]\d)[-\/ ]([0-3]\d)[-\/ ](\d{4})$

The only thing is this regex will allow different separator characters to be mixed in one date.

Example:
12/25-2009

To check that, you could break it out into individual regexes or you can capture the separators. If you aren't worried about capturing, then put alternations:

^(([01]\d)-([0-3]\d)-(\d{4})|([01]\d)\/([0-3]\d)\/(\d{4})|([01]\d) ([0-3]\d) (\d{4}))$

In this case, $1 will capture the whole date, but mm, dd and yyyy may be captured in any 3 of the 9 alternate groups.

This regex will filter out a few invalid dates, like months that start with something other than 0 or 1 in the 1st digit, and so forth, but it is not a full date checker.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24257016
Or if you really don't care about checking valid digit ranges or capturing, then you can simplify to use \d for all digits and remove parens.

^\d\d[-\/ ]\d\d[-\/ ]\d\d\d\d$

or

^(\d\d-\d\d-\d\d\d\d|\d\d\/\d\d\/\d\d\d\d|\d\d \d\d \d\d\d\d)$
0
 
LVL 3

Assisted Solution

by:RobbP
RobbP earned 160 total points
ID: 24258308
Check out this site - for all futures Regex's as its got it all. Much easier than having to fight for expressions. Cheers

http://www.regexlib.com/
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question