验证不工作的日期格式 [英] validating date format not working

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

问题描述

我在与日期验证问题。在我看来,我有一个jQuery的日期选择器 - 我改变了格式从 YY / MM / DD MM / DD / YY 现在我得到的客户端验证错误。例如,

I'm having problem with date validation. In my View, I have a jQuery datepicker - I changed the format from yy/mm/dd to mm/dd/yy and now I get client-side validation errors. For example,

    The value '02/25/2014' is not valid for Date of Birth.

JavaScript的:

The Javascript:

$('#DateOfBirth').datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "mm/dd/yy",
    yearRange: "-90:-5"
});

视图模型:

    [Required]
    [Display(Name = "Date of Birth")]
    public DateTime? DateOfBirth { get; set; }

视图:

@Html.TextBoxFor(m=> m.DateOfBirth, "{0:MM/dd/yyyy}", new { @class = "datepicker" })

在这个任何想法?

Any ideas on this?

感谢。

更新

我忽略了一些东西。验证失败,实际上在服务器端。因此,这无关与jQuery。在 ModelState.IsValid ==虚假我。

I overlooked something. The validation actually fails on the server side. So this has nothing to do with jQuery. The ModelState.IsValid == false for me.

推荐答案

我发现这里的解决方案:的 ASP.NET MVC3 - DateTime格式
并且它与全球化有关。

I found the solution here: ASP.NET MVC3 - DateTime format and it had to do with globalization.

我的locale是 EN-CA

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern

DD / MM / YYYY

因此​​,在的Web.config <&的System.Web GT; 我包括

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

所以,现在的日期时间格式为我工作。

So the DateTime format is working for me now.

P.S。

通过日期,而不必担心特定文化安全的方法是使用ISO 8601的格式 - YYYY-MM-DD (或 YYYY / MM / DD 这也适用)。

A safe way to pass dates without worrying about specific culture is to use ISO 8601 format - yyyy-MM-dd (or yyyy/MM/dd which also works).

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

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