2

I am trying to use the following regular expression to test for a valid date.

^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$

which I got from

http://regexlib.com/REDetails.aspx?regexp_id=1071

My JavaScript test code is:

var date='1/1/1965';
var re = new RegExp('^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$');
alert(re.test(date));

I keep getting "false" rather than "true" for this valid test date.

3
  • 2
    Are you sure you want to do this with a regex? Commented Jan 22, 2011 at 0:54
  • It's working when i test it here: nvcc.edu/home/drodgers/ceu/resources/test_regexp.asp Commented Jan 22, 2011 at 1:00
  • How would you test for a valid date including checking for leap years and correct month/day ranges? Commented Jan 22, 2011 at 1:00

3 Answers 3

3

Try this:

var date='1/1/1965'; 
var re = /^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])\/(29|30)|(0?[13578]|1[02])\/31)\/(19|[2-9]\d)\d{2}|0?2\/29\/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/;
alert(re.test(date)); 
Sign up to request clarification or add additional context in comments.

1 Comment

+1 yes, foward slashes must be escaped with \/. Forward slashes delimit JS regular expressions.
0

Your problem might be escaping of the slashes.

There are 2 ways you can insert a regexp in javascript. 1st way is just put it in forward slashes, and the second way, which is the way you are doing it is with RegExp object, which means that you then have to include the regexp string itself in the JavaScript code as a string.

And as every other string in Javascript which contains special characters like backward slashes (which your regexp does), you have to escape them with yet another backslash.

So basically just replace every backslash in your code with double backslash (\\) and you should be fine.

Comments

0

Just put it in forward-slashes:

var re = /^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)\/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/

Now, while that'll get you a regular expression to use, I have to say that this may be one of the silliest applications of a regex ever. You'd be way better off just instantiating a JavaScript "Date" object and using it's surprisingly friendly API to check what you need to check.

If you instantiate a JavaScript "Date" object with a bogus date (like Februrary 30), it'll just roll to the actual date that sort-of corresponds to the bogus date; in other words, it carries over the bogus days into the next month. Thus, if you just hold on to the month and day, and create a "Date" instance, you can know that if the month and day that the "Date" instance gives you when you ask are different from the ones you fed it, then the original date must have been unreal.

2 Comments

Can you easily test for leap years and valid month and day values in equivalent or less code? Thanks for your opinion, it's unfortunate it is expressed in such a condescending manner rather than a professional one.
@ChrisP Yes, you can, because the JavaScript runtime already knows about all that. Take what you think is a year/month/day date, and plug it into a new JavaScript Date object, and it'll give you a Date that actually makes sense in accordance with reality. If you give it 29 Feb in a year that's not a leap year, it gives you 1 Mar.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.