字段日期必须是 chrome 中 mvc 中的日期 [英] The field date must be a date in mvc in chrome

查看:13
本文介绍了字段日期必须是 chrome 中 mvc 中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的 MVC4 Internet 应用程序,它允许向类别添加一些项目.

I'm doing a simple MVC4 Internet application, which allows to add some items to the categories.

这是我到目前为止所做的.

Here is what i've done so far.

我在 mvc 视图中有一个日期选择器.日期选择器的脚本是这样的.

I've a datepicker in mvc view. The script for the datepicker is like this.

<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
    $(function () {
        $('#dtItemDueDate').datepicker({
            dateFormat: 'dd/mm/yy',
            minDate: 0
        });
    });
</script>

我的模型属性:

        [DisplayName("Item DueDate")]
        [Required]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",ApplyFormatInEditMode = true)]
        [DataType(DataType.DateTime)]
        public DateTime? dtItemDueDate { get; set; }
        public char charCompleted { get; set; }

在我看来,我已经做到了:

and in my view i've done this:

@Html.TextBoxFor(m => m.dtItemDueDate)
@Html.ValidationMessageFor(m => m.dtItemDueDate)

错误是这样的:

The field Item DueDate must be a date.

奇怪的是,它在 IE 和 mozilla 中有效,但在 Chrome 中无效.

The strange this is that it does work in IE and mozilla, but doesnot work in Chrome.

我在 SO 上找到了很多帖子,但都没有帮助

I found many posts on SO, but none of them help

有什么想法/建议吗?

推荐答案

编者注:此答案不再有效,从 jQuery 1.9 (2013) 开始,$.browser 方法具有已删除

Editor note: This answer is no longer valid, as of jQuery 1.9 (2013) the $.browser method has been removed

根据这篇文章,这是基于 Webkit 的浏览器的一个已知怪癖.

According to this post it's a known quirk with Webkit-based browsers.

一种解决方法是修改jquery.validate.js,找到函数date:function(value, element),把这段代码放进去:

One solution is to modify jquery.validate.js by finding the function date: function (value, element) and put this code in it:

if ($.browser.webkit) {
    //ES - Chrome does not use the locale when new Date objects instantiated:
    var d = new Date();
    return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
}
else {
    return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
}

这篇关于字段日期必须是 chrome 中 mvc 中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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