Asp.NET MVC 正则表达式

Asp .NET MVC Regular Expression

我想验证我的模型只接受 10 个数字,并且数字 不应700 开头。 知道如何使用数据注释来做到这一点吗?

[Required(ErrorMessage = "This field is required ")]
[StringLength(10, ErrorMessage = " CR should be 10 digits")]
public string CR_Number { get; set; } 
 [Required(ErrorMessage = "This field is required ")]
 [StringLength(10, ErrorMessage = " CR should be 10 digits")]
 [RegularExpression("^(?!700)\d", ErrorMessage = "CR should not start with 700.")]
 public string CR_Number { get; set; } 

或者您可以按照评论“^(?!700)\d{10}$”中的建议合并最后两个。