由于日期格式,验证失败 [英] Validation failing because of date format

查看:78
本文介绍了由于日期格式,验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC6编辑视图,其出生日期字段如下:

I have an MVC6 Edit View with a date of birth field as follows:

<div class="form-group">
    @Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @*@Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })*@
        @Html.EditorFor(m => m.DateOfBirth,new { htmlAttributes = new { @readonly = "readonly" } })
        @Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })
    </div>
</div>

因此,当我单击保存"时,错误提示:值'07/17/1981'对ApplicationDate无效.如何确保日期以英国格式显示,意味着验证成功.

Therefore when I click save it errors saying: The value '07/17/1981' is not valid for ApplicationDate. How can I ensure that the date is displayed in UK format meaning validation succeeds.

我按照以下答案将以下内容添加到Bubdle.Config中:

I have added the following to the Bubdle.Config following an answer below:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js",
                        "~/Scripts/globalize.js",
                        "~/Scripts/jquery.validate.globalize.js"));

推荐答案

看一下此页面上的示例,检查其是否满足您的要求.

Take a look to the example on this page, check if it does what you want.

https://johnnyreilly.github.io/jQuery.Validation.Unobtrusive.Native/AdvancedDemo/Globalize.html

我看到你问为什么需要包含jquery,答案是因为Microsoft的验证要归功于jquery,因此他们共同努力实现了这一目标.

I saw you asked why you need to include jquery, the answer is because microsoft validation happens thanks to jquery, they work together to achieve this.

如果您的页面是英语(美国),则可以使用默认验证,但是如果您的文化与我的不同,那么您至少需要包括该页面中提到的内容(基本上是jquery,globalize和validation).

If you page is english en-US, default validation will work, but if your culture is different like mine, you need at least to include what is mentioned in that page (basically jquery, globalize and validation).

全球化将覆盖验证以在您希望的区域性中发挥作用.

Globalize will override Validation to work in your desired culture.

尝试一下,让我知道.

这篇关于由于日期格式,验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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