MVC 4如何验证非美国最新与客户端验证? [英] MVC 4 how to validate a non-US date with client validation?

查看:85
本文介绍了MVC 4如何验证非美国最新与客户端验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里我有一个日期时间字段的客户端验证麻烦的情况。当我尝试提交不断告诉我的日期是无效的(27/7/2013)。但是,如果我把日起美国格式它的工作原理(2013年7月27日)。

I have a situation where I am having trouble with the client side validation of a datetime field. When I try to submit it keeps telling me the date is invalid (27/7/2013). But if I turn the date into US format it works (07/27/2013).

我的看法模型如下,

[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? AuditDate { get; set; }

的index.html

Index.html

 @Html.TextBoxFor(m => m.AuditDate)

我已经更新我的web.config

I have updated my web.config

<globalization culture="en-AU" uiCulture="en-AU" />

有什么我错过了?

What have I missed out on?

感谢

推荐答案

别人整理这对我来说,这是所有做的客户端jQuery验证。
再次感谢asymptoticFault ..你..摇滚
MVC 4日期间的文化问题?

Someone else sorted this for me, it was all to do with the client side Jquery validation. Thanks again asymptoticFault.. you rock.. MVC 4 date culture issue?

下面是从该链接,我用...

Here is the info from that link that I used...

问题是解析日期时不占文化的jQuery验证。如果关闭了客户端验证的日期被解析蛮好的是意识到文化的服务器上。

The problem is the jQuery validation not accounting for the culture when parsing a date. If you turn off the client side validation the date is parsed just fine on the server that is aware of the culture.

解决方法是覆盖日期jQuery验证,并包括一个额外的jQuery插件的全球化。你可以找到在全球化插件 href=\"https://github.com/jquery/globalize\">。您也可以轻松地下载使用的NuGet包管理器以及插件。我刚打开包管理器,选择左边的在线选项卡,并输入全球化到搜索,这是第一个结果。一旦你安装我包括这两个文件:

The fix is to override the jQuery validation for date and include an additional jQuery globalization plugin. You can find the globalize plugin here. You can also easily download the plugin using the Nuget Package Manager as well. I just opened the package manager, selected the Online tab on the left and typed "globalize" into the search and it was the first result. Once you have it installed I included these two files:

globalize.js 
globalize.culture.en-AU.js

您可以包括他们直接使用脚本标记或将它们放在一个包,也许与其他jQuery验证文件。

You can either include them directly using a script tag or place them in a bundle, perhaps with the other jQuery validation files.

一旦你拥有了这些,你将需要添加下面的脚本来覆盖日期jQuery验证:

Once you have those you will need to add the following script to override the jQuery validation for date:

<script type="text/javascript">
    $(function () {
        $.validator.methods.date = function (value, element) {
            Globalize.culture("en-AU");
            // you can alternatively pass the culture to parseDate instead of
            // setting the culture above, like so:
            // parseDate(value, null, "en-AU")
            return this.optional(element) || Globalize.parseDate(value) !== null;
        }
    });
</script>

这篇关于MVC 4如何验证非美国最新与客户端验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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