jQuery MVC 日期时间验证 - 英国日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12053022/
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
MVC DateTime validation - UK Date format
提问by mp3duck
I have a simple view with two date fields with ValidationMessageFor controls added for the unobtrusive JavaScript validation.
我有一个带有两个日期字段的简单视图,其中添加了 ValidationMessageFor 控件,用于不显眼的 JavaScript 验证。
My issue is I keep getting told my date is invalid, when it is in correct format (dd/MM/yyyy)
我的问题是我一直被告知我的日期无效,但格式正确 (dd/MM/yyyy)
I have added <globalization culture="en-GB" uiCulture="en-GB"/>
to my web.config, and also included [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
on each DateTime property, yet it still won't accept UK format dates.
我已经添加<globalization culture="en-GB" uiCulture="en-GB"/>
到我的 web.config 中,并且还包含[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
在每个 DateTime 属性中,但它仍然不接受英国格式的日期。
Is there anything obvious I am missing here?
有什么明显的我在这里失踪了吗?
回答by Bohdan
Actually you just need to overload unobtrusive JavaScript validation method for date
实际上,您只需要为日期重载不显眼的 JavaScript 验证方法
jQuery(function ($) {
$.validator.addMethod('date',
function (value, element) {
if (this.optional(element)) {
return true;
}
var ok = true;
try {
$.datepicker.parseDate('dd/mm/yy', value);
}
catch (err) {
ok = false;
}
return ok;
});
});
回答by kestrelb
Bohdan's example worked a treat! I've +1 your answer mate.
Bohdan 的例子很有用!我已经 +1 你的回答伙伴。
For completeness I've written a blog post on how to integrate all the parts (Model, View, custom EditorTemplate, jQuery includes and adding Bohdan's solution).
为了完整起见,我写了一篇关于如何集成所有部分(模型、视图、自定义 EditorTemplate、jQuery 包括和添加 Bohdan 的解决方案)的博客文章。
You can read it here: http://www.kestrelblackmore.com/blog/jquery-datepicker-mvc4
你可以在这里阅读:http: //www.kestrelblackmore.com/blog/jquery-datepicker-mvc4
回答by Rachid
I know it's old but using this I added to the input an attribute with the current date format (which I fetched from the ASP MVC code with CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToLower()).
我知道它很旧,但是使用它我在输入中添加了一个具有当前日期格式的属性(我从带有 CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToLower() 的 ASP MVC 代码中获取)。
Then I rewrote the validation javascript to use the format found in the attribute.
然后我重写了验证 javascript 以使用在属性中找到的格式。
var format = "mm/dd/yyyy";
if (element.attributes["data-format"]) {
format = element.attributes["data-format"].value;
}
I also read the same attribute for the jquery ui datepicker. It might not be that usefull but it can keep the format consistent with the culture at least. Though it could help someone
我还为 jquery ui datepicker 读取了相同的属性。它可能没有那么有用,但至少可以使格式与文化保持一致。虽然它可以帮助某人
回答by aim
I have taken a different approach to solve this compared to other answers. Use jQuery to add a new date function which will force the date to use the current local culture set in the application.
与其他答案相比,我采用了不同的方法来解决这个问题。使用 jQuery 添加一个新的日期函数,该函数将强制日期使用应用程序中设置的当前本地区域性。
$.validator.addMethod('date', function (value, element) {
var d = new Date();
return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
});
That is if it fails on jQuery validation. I credit this answer to Elton Stoneman post which can be read here
也就是说,如果它在 jQuery 验证中失败。我将此答案归功于 Elton Stoneman 的帖子,可以在此处阅读
Hope this will help someone else.
希望这会帮助别人。
回答by zvonimir
Try change this direct in jquery.validate.js. Work with $.datpicker
尝试在 jquery.validate.js 中直接更改此设置。使用 $.datpicker
date: function (value, element) {
var s = value;
var ind2 = 3;
var ind3 = 6;
var ind4 = 4;
if (s.substr(6, 1) != "2") {
ind2 = ind2 + 1;
ind3 = ind3 + 1;
ind4 = ind4 + 1;
}
var ndan = s.substr(0, 2);
var nmje = s.substr(ind2, 2);
var ngod = s.substr(ind3, ind4);
var ns = ngod + "," + nmje + "," + ndan;
return this.optional(element) || !/Invalid|NaN/.test(new Date(ns));