dimanche 28 juin 2015

dd-mm-yyyy format for datepicker of MVC is not working in Chrome

In my MVC5 Razor code for entering Date of Birth I am using a datepicker as below

    @Html.EditorFor(model => model.date_of_birth, 
new { htmlAttributes = new { @class = "m-wrap  datepicker" } })

Here for the model.date_of_birth an EditorFor is calling and making it as a datepicker with @class = datepicker

Then the datepicker is initiated at the script area with below code

 $('.datepicker').datepicker({
            format: 'yyyy-mm-dd',
            autoclose: true
        })

Here the date format is 'yyyy-mm-dd' and it is working fine, but the user want it to be in dd-mm-yyyy format. So I changed the format in script as dd-mm-yyyy In Internet Explorer it is working fine but in Chrome it is giving an error for some date

eg:  14-05-2015

The field Date of Birth* must be a date.

the date 11-05-2015 is working fine in chrome also. So I guess Chrome is taking the date format in mm-dd-yyyy.

The format 'dd-M-yyyy' is also working correctly only error coming for dd-mm-yyyy

Any way to overcome this browser specific error?

Edit

$.validator.addMethod('date', function (value, element) {
    if (this.optional(element)) {
        return true;
    }
    var valid = true;
    try {
        $.datepicker.parseDate('dd/mm/yyyy', value);
    }
    catch (err) {
        valid = false;
    }
    return valid;
});

$(function () {

 $('.datepicker').datepicker({
            format: 'dd/mm/yyyy',
            autoclose: true
        })
});

Aucun commentaire:

Enregistrer un commentaire