cezarypiatek
cezarypiatek

Reputation: 1124

MVC 3 Client side date validation issue in Internet Explorer

I have some problems with client-side validation DateTime fields. I'm using following DataAnnotation in my model class to DateTime fields:

[Display(Name = "Beginning Date", Description = @"Insert Date yyyy-mm-dd")]
[DataType(DataType.Date, ErrorMessage = @"Insert Date yyyy-mm-dd")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

I'm using Datapicker from jQuery UI to insert date. Date format is enforced by

   $(document).ready(function () {
    $.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });
 });

So it works great on Firefox but completely crash on Internet Explorer when I insert date using Datepicker. When I type date in format yyyy/mm/dd instead of yyyy-mm-dd then is no validation error.

Does anyone know why the validation format is different on Internet Explorer than Firefox and where I can change it?

Thanks for help.

Upvotes: 3

Views: 4284

Answers (2)

jason_ruz
jason_ruz

Reputation: 2125

You can replace the date validator with the dateISO validator.

$.validator.methods["date"] = function (value, element) {
    return $.validator.methods.dateISO.apply(this, arguments); 
};

By default, the jQuery date validation method uses JavaScript's built-in Date object to test if the date is valid. Internet Explorer 8 and below do not support using the ISO Date Format for the Date object (see JavaScript Version Information), which causes the validation to fail.

Upvotes: 7

jl.
jl.

Reputation: 764

Have a look at this blog post: ASP.NET MVC 3: Integrating with the jQuery UI date picker and adding a jQuery validate date range validator. Might be helpful.

Upvotes: 1

Related Questions