360浏览器下jquery.validate.unobtrusive的日期验证问题
今天在招聘频道(job.cnblogs.com)遭遇这样一个问题——在360浏览器下,在一个表单验证中,虽然输入了有效的日期,却总是提示日期格式错误,见下图:
而在Chrome/Safari/Firefox中都正常。
这个表单验证用的是jquery.validate.js + jquery.validate.unobtrusive.js,验证代码是由ASP.NET MVC自动生成的。
视图中的代码是这么写的:
<label>截止日期<em>*</em>:</label> @Html.TextBoxFor(m => m.ExpireDate, new { @class = "date", Value = Model.ExpireDate.Value.ToString("yyyy-MM-dd") }) @Html.ValidationMessageFor(m => m.ExpireDate)
ViewModel中是这么声明的:
[DataType(DataType.DateTime, ErrorMessageResourceType = typeof(CommonValidationMessage), ErrorMessageResourceName = "DateTime")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "yyyy-MM-dd")] public DateTime? ExpireDate { get; set; }
折腾半天无果。。。
后来转念一想,何必纠结于日期类型,直接用正则表达式验证不是更简单吗?
于是——
视图中去掉class="date":
<label>截止日期<em>*</em>:</label> @Html.TextBoxFor(m => m.ExpireDate, new { Value = Model.ExpireDate.Value.ToString("yyyy-MM-dd") }) @Html.ValidationMessageFor(m => m.ExpireDate)
ViewModel中的声明改为:
[RegularExpression(@"\d\d\d\d-\d\d-\d\d", ErrorMessage = "请输入正确的日期格式(yyyy-MM-dd)")] public DateTime? ExpireDate { get; set; }
问题解决。