DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view

前端 未结 9 2171
甜味超标
甜味超标 2020-11-28 08:12

The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the special characters in the regular expression.

Model:



        
9条回答
  •  天涯浪人
    2020-11-28 08:23

    Try @ sign at start of expression. So you wont need to type escape characters just copy paste the regular expression in "" and put @ sign. Like so:

    [RegularExpression(@"([a-zA-Z\d]+[\w\d]*|)[a-zA-Z]+[\w\d.]*", ErrorMessage = "Invalid username")]
    public string Username { get; set; }
    

提交回复
热议问题