MVC Validation make RegularExpression numeric only on string field

前端 未结 4 1284
轻奢々
轻奢々 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:52

    The RegEx should be ^[0-9]*$.

    I.E.

    The property should look like:

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

    See working example.


    I'm sure you already have jQuery referenced but make sure jQuery validate and Microsoft validate scripts are included.

    
    
    
    
    
    

提交回复
热议问题