开发者

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 Dat开发者_运维知识库e 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜