What is the regular expression for date format (mm-dd-yyyy) and validating them?
Asked
Active
Viewed 332 times
1 Answers
2
Here it is
((0[1-9])|(1[0-2]))-(([0-2]\d)|([3][01]))-(\d{4})
Explanation:
Since
mmcan be any thing from01to12so to suffice01-09, regular expression would be0[1-9]or asmmcan also be10-12so1[0-2].Hence the regular expression to match
mmwould be(0\d)|(1[0-2])As
ddcan be any thing from01to31so to suffice01-29regular expression would be[0-2]\dor it can also be30-31so[3][01].Hence the regular expression to match
ddwould be(([0-2]\d)|([3][01]))As
yyyycan be any number of 4 digits i.e. 2010, 1999, 2050 etc (as long as you are not considering any specificrange) so the regular expression would simply be 4 digits i.e.\d{4}.Hence the regular expression to match
yyyywould be(\d{4})
So after combining these, the complete regular expression for the date would be:
((0[1-9])|(1[0-2]))-(([0-2]\d)|([3][01]))-(\d{4})
Kamran Ahmed
- 11,809
- 23
- 69
- 101
-
Thank You friends this is very use full for me – Dinesh Dinaz Dec 15 '13 at 06:35
-
Your "Here it is" regular expression at the top does not match your expression at the bottom : your day should be `[0-2]\d` – Mr. Polywhirl Dec 15 '13 at 06:45
-
@Mr.Polywhirl actually I edited the question later and forgot to update that. Updated now. Thanks – Kamran Ahmed Dec 15 '13 at 06:50