Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Date Validation (YYYY-MM-DD)

I need to validate a date using this script in the format of YYYY-MM-DD, but I can't seem to get it working perfectly. With the regular expression I'm using, it allows for users to enter 10 numbers without dashes, instead of 8 with dashes in the correct places. Is there a way to modify my script to fix this?

jQuery.validator.addMethod("date", function(date, element) {
                return this.optional(element) || date.match(/^[-0-9]{10}$/);
            }, "Please specify a valid date");
like image 368
JacobTheDev Avatar asked Feb 13 '26 06:02

JacobTheDev


1 Answers

You have a wrong regex.

You can use this one instead :

^\d{4}-((0\d)|(1[012]))-(([012]\d)|3[01])$

So it would be :

return this.optional(element) || date.match(/^\d{4}-((0\d)|(1[012]))-(([012]\d)|3[01])$/);
like image 168
Adrien Lacroix Avatar answered Feb 15 '26 20:02

Adrien Lacroix