asp.net-mvc MVC 4 如何通过客户端验证来验证非美国日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18546971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:08:37  来源:igfitidea点击:

MVC 4 how to validate a non-US date with client validation?

asp.net-mvcasp.net-mvc-3asp.net-mvc-4

提问by user2206329

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).

我遇到了在客户端验证日期时间字段时遇到问题的情况。当我尝试提交时,它一直告诉我日期无效(27/7/2013)。但是,如果我将日期转换为美国格式,它就可以工作(2013 年 7 月 27 日)。

My view model is as follows,

我的视图模型如下,

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

Index.html

索引.html

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

I have updated my web.config

我已经更新了我的 web.config

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

What have I missed out on?

我错过了什么?

Thanks

谢谢

回答by user2206329

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?

其他人为我整理了这个,这完全与客户端 Jquery 验证有关。再次感谢 asymptoticFault .. 你摇滚.. MVC 4 日期文化问题?

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

这是我使用的那个链接的信息......

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 验证在解析日期时没有考虑文化。如果您关闭客户端验证,则可以在了解文化的服务器上很好地解析日期。

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:

修复方法是覆盖 jQuery 日期验证并包含一个额外的 jQuery 全球化插件。您可以在此处找到全球化插件。您还可以使用 Nuget 包管理器轻松下载该插件。我刚刚打开包管理器,选择左侧的 Online 选项卡并在搜索中输入“globalize”,这是第一个结果。安装后,我包含了这两个文件:

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

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:

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

<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>

回答by lukyer

Client validation issues can occur because of MVC bug (even in MVC 5) in jquery.validate.unobtrusive.min.jswhich does not accept date/datetime format in any way. Unfortunately you have to solve it manually.

客户端验证问题可能是由于jquery.validate.unobtrusive.min.js 中的 MVC 错误(即使在 MVC 5 中)而发生它不以任何方式接受日期/日期时间格式。不幸的是,您必须手动解决它。

My finally working solution:

我的最终工作解决方案:

You have to include before:

您必须先包括:

@Scripts.Render("~/Scripts/jquery-3.1.1.js")
@Scripts.Render("~/Scripts/jquery.validate.min.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/moment.js")

You can install moment.js using:

您可以使用以下方法安装 moment.js:

Install-Package Moment.js

And then you can finally add fix for date format parser:

然后你终于可以为日期格式解析器添加修复:

$(function () {
    $.validator.methods.date = function (value, element) {
        return this.optional(element) || moment(value, "DD.MM.YYYY", true).isValid();
    }
});

回答by Abbas Amiri

When using jQuery validation for dates, you will find out that it does not work well for dates using Chrome. This happens when you use a non-US locale.

使用 jQuery 验证日期时,您会发现它不适用于使用 Chrome 的日期。当您使用非美国语言环境时会发生这种情况。

The solution has presented at Fixing jQuery non-US Date Validation for Chrome

该解决方案已在Fixing jQuery non-US Date Validation for Chrome 中提出

回答by Jaimin

Try with this,

试试这个,

View

看法

<div>
                @Html.LabelFor(m => m.DOB)
                @Html.TextBoxFor(m => m.DOB, new { @Formate = "dd/MM/yyyy" })
                @Html.ValidationMessageFor(m => m.DOB)

            </div>

Model

模型

[DisplayName("Date Of Birth")][DataType(DataType.Date)]
public DateTime DOB { get; set; }