Reputation: 7397
Can anyone please tell me if there is something wrong with this regEx:
/^(0[1-9]|1[0-2])\/(19|20)\d{2}$/
I am trying to create a regEx for dates by MM/DD/YYYY, I thought this would cut it but for some reason it is still saying dates that are correct are invalid.
Such as 02/15/2015 , 01/31/2013 , etc. Any day that should be valid is not being validated so I am thinking this regEx is not doing what I was hoping it was doing...
Will anyone take a look at it and see where I may have messed up please?
Upvotes: 1
Views: 9639
Reputation: 17238
Probably this shouldn't be addressed by a regex, mostly due to the complications arising from leap years. Anyway, disregarding this issue by accepting Feb 29th for all years and considering dates from the 20th and 21st century only, we have (pcre syntax; split across multiple lines for easier understanding. fold into a single line by eliminating all leading whitespace in a line):
/^
(
(0[13578]|1[02])\/(0[1-9]|[12][0-9]|3[01])
| (0[469]|11)\/(0[1-9]|[12][0-9]|30)
| 02\/(0[1-9]|[12][0-9])
)
\/(19|20)\d{2}
$/x
Upvotes: -1
Reputation: 125671
This works for me in RegexBuddy, using it's default PCRE dialect:
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}
It successfully matches
01/01/2014
12/31/2000
07/15/1955
01-01-2012
12.22.1922
It fails (correctly) on
13/01/2001
12/33/2015
01/01/1200
15.36.1948
01.50.1922
Upvotes: 5