MVC Validation make RegularExpression numeric only on string field

前端 未结 4 1289
轻奢々
轻奢々 2021-01-01 10:40

I have the following property in my view model:

[Required]
[MaxLength(12)]
[MinLength(1)]
[RegularExpression(\"[^0-9]\", ErrorMessage = \"UPRN must be numeri         


        
4条回答
  •  独厮守ぢ
    2021-01-01 10:50

    Or you could do the length validation in the regexp:

    [Required]
    [RegularExpression("^[0-9]{1,12}$", ErrorMessage = "...")]
    public string Uprn { get; set; }
    

    Here's the regex visualized:

    Regular expression visualization

    Debuggex Demo

提交回复
热议问题