MVC Validation make RegularExpression numeric only on string field

前端 未结 4 1330
轻奢々
轻奢々 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 11:01

    The regular expression is wrong. Replace it with:

    [Required]
    [MaxLength(12)]
    [MinLength(1)]
    [RegularExpression("^[0-9]*$", ErrorMessage = "UPRN must be numeric")]
    public string Uprn { get; set; }    
    

    Don't forget to include:

    @Scripts.Render("~/bundles/jqueryval")
    

    in your view for the jquery validation

提交回复
热议问题