Reputation: 915
I have following regex which I found from this website:
Bt somewhere the syntax is incorrect and it throws error:
var myDate = /^(0?[1-9]|1[0-2])\/(0?[1-9]|[12][0-9]|3[01])\/(19|20)[0-9]{2};
Just to be clear:
Date can be anything between 01/01/1900 and 31/12/2099 but format should be strictly:
DD/MM/YYYY
I know there are several solutions on web, all similat but somehow it is throwing javascript error.
Probably syntax mistake because just above that i have put email validation which works fine:
var email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
Upvotes: 0
Views: 74
Reputation: 136094
The message from the console says
SyntaxError: unterminated regular expression literal
you're missing a /
at the end of the line, so it should be
var myDate = /^(0?[1-9]|1[0-2])\/(0?[1-9]|[12][0-9]|3[01])\/(19|20)[0-9]{2}/;
As pointed out in the comments, you may also have intended to put a $
at the end before the /
. This indicates to regex that you should match the end of the string (see your email example later in the OP).
As an aside; validating the general format of a date using regex is valid and worthwhile. Trying to validate it's an actual real, valid (ie, not 30 feb 2012) date using regex is barmy. The regex to be able to do it properly would be pages upon pages long!
Upvotes: 5