电子邮件数据注释验证有效,但Phone无效 [英] Email data annotation validation works but Phone doesn't
问题描述
我在ASP.NET MVC4项目中使用数据注释来对电子邮件和电话字段执行客户端验证.电子邮件已在客户端中成功验证,但电话未通过验证-电话允许我输入无效字符,并且仅在提交表单时才警告我(而不是在键入字符后立即警告我)
I'm using data annotations in my ASP.NET MVC4 project to perform client-side validation on email and phone fields. Email is successfully validating in the client but phone doesn't - it allows me to enter invalid characters and only warns me on submission of the form (rather than immediately after the character is typed)
在模型中:
[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; }
我认为该视图包含正确的脚本引用:
In the view I believe I'm including the correct script references:
<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,因为我正在应用为清楚起见在此省略的类属性)
..and using html helpers (I'm using TextBoxFor rather than EditorFor as I'm applying class attributes which I have omitted here for clarity)
@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" })
我想念什么?
推荐答案
我认为问题在于仅不支持电话类型. 请参阅 w3schools的电话类型支持
I think the problem is that phone type is just not supported. See phone type support at w3schools
这篇关于电子邮件数据注释验证有效,但Phone无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!