regular expression to check date

pushVTEC

Senior member
Aug 30, 2003
265
0
0
Would this regular expression work to make sure that the input is in MM\DD\YYYY form?

/[0-9]{2}\[0-9]{2}\[0-9]{4}/
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
You need to escape the backslashes. Other than that, yes. Of course, dates like 15/37/9876 will still match just fine, and you may want to allow things like 1/3/2004, your regex requires 01/03/2004.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
your expression doesn't limit the months to be <= 12. at the very minimum, you might say that the first digit is either a 0, 1, or missing.

edit: <= 12 instead of < 12
 

pushVTEC

Senior member
Aug 30, 2003
265
0
0
why isn't this working i want it to check YYYY\MM\DD
var regex = /(19|20)\d\d[\](0[1-9]|1[012])[\](0[1-9]|[12][0-9]|3[01])/;

i get a unmatched ) in regular expression