user6321125
user6321125

Reputation: 137

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?

Upvotes: 1

Views: 693

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627507

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.

Upvotes: 3

Related Questions