Search code examples
c#regexcustomvalidator

RegularExpressionValidator ValidationExpression does not match


I tested my regular expression online and it does what I want: only letters, numbers, parenthesis, -, and slash /. Using the RegularExpressionValidator in C#,

It doesn't match my string and I get my ErrorMessage... I tested with the string :

"Asics 2018 - additional digital spend"

The code :

RegularExpressionValidator2.ValidationExpression = @"^[\w, \/ --()]";
RegularExpressionValidator2.ErrorMessage = $"No special character is allowed in the name";

I also tried : @"^[\w, / --()]"

How to use the validator ?


Solution

  • Place a single dash at the beginning or end of the character class or else it's meaning will be to specify a range. Repeat the character class one or more times using + between asserting the start ^ and the end $ of the string

    ^[\w, /()-]+$

    Regex demo