1
[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?

2
  • 1
    You should only pass a string, not a regex object as string. Use "^[A-Za-z,.'-]{2,15}$" Commented May 30, 2016 at 10:12
  • 1
    Very nice placement of the hyphen, BTW. It does not have to be escaped at the end of the character class. Commented May 30, 2016 at 10:16

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.