我尝试使用以下代码进行仅数字验证,以在 Mvc Web 应用程序中进行联系号码验证。
[RegularExpression(@"/(^\(\d{10})?)$/", ErrorMessage = "Please enter proper contact details.")]
[Required]
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
但是验证表达式不起作用。
对于联系号码,我只想接受数字。它可以是 10 位数字的手机号码或固定电话号码。
最佳答案
如果除了数字之外没有任何限制,这应该适合:
[RegularExpression(@"^\d+$", ErrorMessage = "Please enter proper contact details.")]
[Required]
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
关于asp.net-mvc - Mvc验证正则表达式只验证数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8839827/