验证不工作的日期格式 [英] validating date format not working
问题描述
我在与日期验证问题。在我看来,我有一个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屋!