我已经在Stack Overflow上发布了类似的问题,但没有针对此案例。无论出于何种原因,ASP.Net MVC 4客户端验证在Chrome中运行良好,但不会在IE9中启动。
的Web.config
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
查看
可以将任何内容输入这两个字段,然后将它们发回服务器。例如,55/55/5555
会在Chrome中触发错误并阻止表单发布,但是在IE中,表单会将数据发送回服务器并导致网站崩溃...
下面是应用于每个字段的jquery输入掩码和Razor语法。
$('#EffectiveDate').mask('99/99/9999');
$('#Birthdate').mask("99/99/9999");
<div class="M-editor-label">
@Html.LabelFor(model => model.EffectiveDate, new {@class="required"})
</div>
<div class="M-editor-field">
@Html.TextBoxFor(model => model.EffectiveDate, new { maxlength = 10 })
@Html.ValidationMessageFor(model => model.EffectiveDate)
</div>
<div class="M-editor-label">
@Html.LabelFor(model => model.Birthdate, new {@class="required"})
</div>
<div class="M-editor-field">
@Html.EditorFor(model => model.Birthdate)
<br />
@Html.ValidationMessageFor(model => model.Birthdate)
</div>
Layout.cshtml
这是渲染的脚本部分
<script src="/Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.10.4.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script src="/Scripts/typeahead.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.dataTables.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.numeric.js" type="text/javascript"></script>
<script src="/Scripts/jquery.fancybox.js" type="text/javascript"></script>
<script src="/Scripts/jquery.fancybox.pack.js" type="text/javascript"></script>
模型
[DisplayName("Effective Date")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime? EffectiveDate { get; set; }
[DisplayName("Date of Birth")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Birthdate { get; set; }
答案 0 :(得分:2)
请查看工作示例https://dotnetfiddle.net/fEER2v
问题在于日期本地化和jQuery验证插件。最简单的修复方法是覆盖date
验证程序。这是使用moment.js javascript库的示例代码:
$.validator.methods.date = function (value, element) {
return value === "" || moment(value, "MM/DD/YYYY", true).isValid();
};
也可以通过NuGet
获取当然,您需要进行一些优化,例如从元素属性中获取日期格式。