Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clientside validation fails for date format dd/mm/yyyy in jQuery Validate

I am using jQuery Validate plugin for clientside validation in an MVC 5 application. For the date fields cilentside validations fails when using dd/mm/yyyy format. Is there a way to change the date format in jQuery Validation?

like image 802
harini88 Avatar asked Nov 30 '22 02:11

harini88


2 Answers

Adding to Darin's answer. If you happen to already be using the datepicker plugin from JQuery UI then you can use that date parser instead of creating your own:

$.validator.methods.date = function (value, element) {
    return this.optional(element) ||  $.datepicker.parseDate('dd/mm/yy', value);
}
like image 188
Charly Avatar answered Dec 02 '22 16:12

Charly


You could override the date parsing method of the validate plugin:

$.validator.methods.date = function (value, element) {
    return this.optional(element) || parseDate(value, "yyyy-MM-dd") !== null;
}

Here parseDate is a function that you could write yourself. The following thread might give you some ideas. Or use some existing plugin such as datejs or Globalize.

like image 35
Darin Dimitrov Avatar answered Dec 02 '22 17:12

Darin Dimitrov