MVC 4:火狐,Chrome,Safari浏览器数据验证错误 - IE OK [英] MVC 4: Date validation error in Firefox, Chrome, Safari - IE ok

查看:162
本文介绍了MVC 4:火狐,Chrome,Safari浏览器数据验证错误 - IE OK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我升级了MVC 3 web应用程序的MVC 4,以下为手动uppgrade的说明。一切都OK,而应用程序是在IE9运行。我有几个数据类型的客户端端和服务器端验证领域表格和提交表单的时候所有人都正确处理。

I upgraded a mvc 3 web app to mvc 4, following the instructions for manual uppgrade. Everything went OK, and the app is running in IE9. I have forms with fields of several data types with both client side and server side validation and all are processed correctly when the form is submitted.

但是当我使用其他浏览 - 与Firefox 8,Chrome浏览器15和Safari 5.1.1测试 - 它验证日期字段时失败。 I''m使用PT-PT文化与格式为DD-MM-YYYY日期,正如我所说,在IE9他们通过验证,但在其他浏览器它说,该领域是不是一个有效日期。

But when i use other browser - tested with Firefox 8, Chrome 15 and Safari 5.1.1 - It fails when validating date fields. I´'m using the 'pt-PT' culture with dates on the format dd-MM-yyyy, and as I said, in IE9 they pass validation, but on the other browsers it says the field is not a valid date.

TIA

若阿金·

推荐答案

我发现这个问题是jQuery验证。它调用JavaScript日期构造函数来检查日期是有效的:

I found that the problem was in JQuery validation. It calls the javascript Date constructor to check if the date is valid:

    // http://docs.jquery.com/Plugins/Validation/Methods/date
    date: function(value, element) {
        return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
    },

由于Javascript Date构造函数期待的格式为yyyy-MM-dd的日期为它在葡萄牙语格式DD-MM-YYY日期返回无效的。

Since javascript Date constructor is expecting a date in the form yyyy-MM-dd it returned Invalid for dates in portuguese format dd-MM-yyy.

唯一的例外是IE不返回无效的,但是从我们推出了一个日期diferent。

The exception is IE that does not return Invalid but a date diferent from the one we introduced.

该soluction是创建一个jquery.validate-pt.js与code。与正确的为我们的格式来覆盖验证:

The soluction was to create a jquery.validate-pt.js with the code to override the validation with the correct one for our format:

$.validator.methods.date = function (value, element) {
    return this.optional(element) || ( /^\d{1,2}[\/-]\d{1,2}[\/-]\d{4}(\s\d{2}:\d{2}(:\d{2})?)?$/.test(value)
        && !/Invalid|NaN/.test(new Date(value.replace("/", "-").split("-")[2], value.replace("/", "-").split("-")[1], value.replace("/", "-").split("-")[0])));
}

这篇关于MVC 4:火狐,Chrome,Safari浏览器数据验证错误 - IE OK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆