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");
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])$/);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With