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

前端 未结 9 2116
甜味超标
甜味超标 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:46

    Try using the ASCII code for those values:

    ^([a-zA-Z0-9 .\x26\x27-]+)$
    
    • \x26 = &
    • \x27 = '

    The format is \xnn where nn is the two-digit hexadecimal character code. You could also use \unnnn to specify a four-digit hex character code for the Unicode character.

    0 讨论(0)
  • 2020-11-28 08:46

    The problem is that the regex pattern is being HTML encoded twice, once when the regex is being built, and once when being rendered in your view.

    For now, try wrapping your TextBoxFor in an Html.Raw, like so:

    @Html.Raw(Html.TextBoxFor(model => Model.FirstName, new { }))
    
    0 讨论(0)
  • 2020-11-28 08:47

    Try this one in model class

        [Required(ErrorMessage = "Enter full name.")]
        [RegularExpression("([A-Za-z])+( [A-Za-z]+)", ErrorMessage = "Enter valid full name.")]
    
        public string FullName { get; set; }
    
    0 讨论(0)
提交回复
热议问题