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
mm
can be any thing from01
to12
so to suffice01-09
, regular expression would be0[1-9]
or asmm
can also be10-12
so1[0-2]
.Hence the regular expression to match
mm
would be(0\d)|(1[0-2])
As
dd
can be any thing from01
to31
so to suffice01-29
regular expression would be[0-2]\d
or it can also be30-31
so[3][01]
.Hence the regular expression to match
dd
would be(([0-2]\d)|([3][01]))
As
yyyy
can 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
yyyy
would 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