I am creating a date validation in Javascript with regex. In some cases it is not working
'(?:(0[1-9]|1[012])[/.](0[1-9]|[12][0-9]|3[01])[/.][0-9]{4})'
11/11/1000 – Correct
11/11/0000 – INVALID
Zero should not be allowed in year
I am creating a date validation in Javascript with regex. In some cases it is not working
'(?:(0[1-9]|1[012])[/.](0[1-9]|[12][0-9]|3[01])[/.][0-9]{4})'
11/11/1000 – Correct
11/11/0000 – INVALID
Zero should not be allowed in year
2
Answers
try using this regex instead using a negative look ahead on the 0000
You should avoid using a regular expression to validate dates. Parse the date string as an actual date and check to see if it is actually valid.
Note: The usage of
+token[n]
andparseInt(token[n], 10)
below are interchangeable.Here is an alternate version that is slightly optimized.