Phone 验证属性无效
Phone validation attribute not working
我正在尝试验证一些用户联系方式,如下所示:
public class Customer
{
[Display(Name = "Your e-mail :")]
[Required(ErrorMessage = "An email address is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string ContactEmail { get; set; }
[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required")]
[Phone(ErrorMessage = "Invalid Phone Number")]
public string ContactNumber { get; set; }
}
在我看来,电子邮件验证效果很好,只允许有效的电子邮件地址。但是,phone 验证根本不起作用,它允许各种字母和特殊字符。
文档指出 PhoneAttribute
class
Specifies that a data field value is a well-formed phone number using a regular expression for phone numbers
那为什么这没有发生呢?
使用以下代码进行 Phone 验证。
[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required.")]
[DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Phone Number.")]
public string ContactNumber { get; set; }
我正在尝试验证一些用户联系方式,如下所示:
public class Customer
{
[Display(Name = "Your e-mail :")]
[Required(ErrorMessage = "An email address is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string ContactEmail { get; set; }
[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required")]
[Phone(ErrorMessage = "Invalid Phone Number")]
public string ContactNumber { get; set; }
}
在我看来,电子邮件验证效果很好,只允许有效的电子邮件地址。但是,phone 验证根本不起作用,它允许各种字母和特殊字符。
文档指出 PhoneAttribute
class
Specifies that a data field value is a well-formed phone number using a regular expression for phone numbers
那为什么这没有发生呢?
使用以下代码进行 Phone 验证。
[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required.")]
[DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Phone Number.")]
public string ContactNumber { get; set; }