输入日期类型的客户端验证不起作用 [英] Client-side validation of input type date does not work
问题描述
我有一个包含日期和时间字段的表单:
I have a form containing a date and datetime field:
@Html.TextBoxFor(model => model.A, new { @type = "datetime" })
@Html.TextBoxFor(model => model.B, new { @type = "date" })
型号:
public class TestModel
{
[DataType(DataType.Date)]
public DateTime A {get;set;}
[DataType(DataType.Date)]
public DateTime B {get;set;}
}
通过使用这些输入类型iPad的显示日期不错(时间)采摘。这些字段是使用客户端验证验证。对于日期时间字段( A
)它的工作原理,但日期字段( B
)将引发一个错误:请输入有效日期。我该如何解决这个问题?
By using these input types an iPad shows nice date(time) pickers. The fields are validated using client-side validation. For the datetime field (A
) it works, but the date field (B
) will raise an error: "please enter a valid date." How do I solve this?
例如:
- 日期时间为这款iPad(Safari浏览器)值是有效的(按照MVC客户端验证):12月15日2011 9:20
- 日期这款iPad(Safari浏览器)值无效:12月15日2011
这是很难在iPad上调试code,所以我不知道Safari浏览器设置输入的值属性时,如何改变日期格式。
It's hard to debug code on an iPad, so I have no clue how Safari changes the date format when setting the input's value attribute.
编辑:
我发现了日期时间格式是通用的日期时间格式( YYYY-MM-DDTHH:MMZ
),而日期格式为 YYYY-MM-DD
。也许客户端验证程序的普遍理解datetime和不 YYYY-MM-DD
由于本地化。
I discovered the datetime format is universal datetime format (yyyy-MM-DDTHH:mmZ
), while the date format is yyyy-MM-dd
. Probably the client-side validator does understands universal datetime and not yyyy-MM-dd
due to localization.
推荐答案
我有完全相同的问题和观看移动网站,我在我的iPhone开发时,被疯狂的令人难以置信。下面这个问题的讨论,解决了我。
I had the exact same problem and was maddened beyond belief when viewing a mobile site I'm developing on my iPhone. The discussion in the issue below solved it for me.
https://github.com/jzaefferer/jquery-validation/issues/20 。
此外,去远方这个以无缝的方式,我创建了日期数据类型以下剃须刀编辑模板:
Also, to go the distance with this in a seamless way, I created the following razor editor template for Date data types:
@model DateTime?
@Html.TextBox("myDate", ViewData.Model.ToIso8601FullDate(), new { type = "date", @class = "text-box single-line" })
和一个方便的扩展方法来喂HTML 5日期输入类型的格式就喜欢和根据的规格输入类型=日期:
and a handy extension method to feed the html 5 date input type a format it enjoys working with according to the spec for input type=date:
public static string ToIso8601FullDate(this DateTime? d)
{
if (!d.HasValue) return null;
return d.Value.ToString("yyyy-MM-dd");
}
这篇关于输入日期类型的客户端验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!