我在我的 ASP.NET MVC4 项目中使用数据注释对电子邮件和电话字段执行客户端验证。电子邮件在客户端中成功验证,但电话没有 - 它允许我输入无效字符,并且只在提交表单时警告我(而不是在输入字符后立即警告我)
在模型中:
[Required(ErrorMessage = "Email is required")]
[DataType(DataType.EmailAddress)]
[EmailAddress]
[Display(Name = "Email")]
public string Email{ get; set; }
[Required(ErrorMessage = "Mobile is required")]
[DataType(DataType.PhoneNumber)]
[Phone]
[Display(Name = "Mobile number")]
public string Mobile { get; set; }
在视图中,我相信我包含了正确的脚本引用:
<script type="text/javascript" src="~/Scripts/jquery.validate.min.js" ></script
<script type="text/javascript" src="~/Scripts/jquery.validate.unobtrusive.min.js" ></script>
..并使用html助手(我使用的是TextBoxFor而不是EditorFor,因为我正在应用类属性,为了清楚起见,我在此处省略了)
@Html.LabelFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
@Html.TextBoxFor(model => model.Email, new { @type = "email" })
@Html.LabelFor(model => model.Mobile)
@Html.ValidationMessageFor(model => model.Mobile)
@Html.TextBoxFor(model => model.Mobile, new { @type = "phone" })
我错过了什么?