Date Validation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Date Validation

I know sometimes regex expressions seems creepy.I was trying to write a date validation regex and came up with this /^\d{4}[/-](0\d|1[0-2])[/-]([0-2]\d|3[0-1])$/g but its somehow incorrect since a month like february can not have 30 and 31 days or 29days(unless its a leap year).Is there an improvement of this or sometimes one should allow false positives or false negatives?And by the way which is better false negatives or positives?

16th Dec 2017, 7:53 PM
KeN
KeN - avatar
2 Answers
+ 1
I personally would write the regex as a primary validation of data type and placement. If the date fits the pattern then use javaScript to do the actual data validation.
16th Dec 2017, 8:14 PM
Kevin Gilkey-Graham
Kevin Gilkey-Graham - avatar
0
Sounds like a cool idea...Thanks Grahambo
17th Dec 2017, 6:39 AM
KeN
KeN - avatar