user6321125 user6321125 - 5 months ago 29
ASP.NET (C#) Question

mvc validation regexp not working

[RegularExpression("/^[a-z,.'-]{2,15}$/i", ErrorMessage = "bla bla'")]

I have an input text field regulated with this regexp and for example "asd" should be ok but its not apparently... bla bla is shown!The required data annotation is working propery but this one is not.What am I missing?


You should only pass a string, not a regex object as string.

[RegularExpression("^[A-Za-z,.'-]{2,15}$", ErrorMessage = "bla bla'")]

The /.../ are regex delimiters, and /i is a regex case insensitive modifier. This called a regex literal notation in JavaScript. In ASP.NET, you should only pass the pattern, the part in between the /.../ delimiters. Also, you cannot use regex modifiers, but in this case, you can just add A-Z to the character class.