1

I have a RegularExpressionValidator with:

ValidationExpression="^(a-zA-Z0-9 '-]+$"

so this was working for alphanumerics, spaces, apostrophes and hyphen.

I was told that I now need it to additionally work for underscore, ampersand '&', comma, parentheses and fullstop.

I tried:

ValidationExpression="^(a-zA-Z0-9 '-_&,().]+$"

but this made it accept virtually anything.

Could someone please tell me where I'm going wrong? I'm relatively new to Regex.

BigJim
  • 411
  • 6
  • 20

4 Answers4

2

The dot means "anything" (except, depending on your settings, line returns). You must escape it with a backslash if you want to match the dot character:

ValidationExpression="^(a-zA-Z0-9 '-_&,()\.]+$"

You would probably also need to escape parentheses and brackets, since they also are metacharacters.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
1

be careful when using a . in regular expressions, for it is a special character and it matches any character. You might want to escape it like this: \. or have it inside a character class like [.]. Take care of any other special characters too.

You can find a lot of interesting information regarding the dot here, make sure you check the rest of the links in that site, they will give you great insight on how regular expressions work!

Hope this helps.

Federico Cáceres
  • 1,216
  • 12
  • 19
0

try this. put the opening [ in and move the - to the end.

^[a-zA-Z0-9 '_&,().-]+$

BTW: . has no specific reference in character classes neither do most aside from your language's regex expression delimiter from the code itself.

Keng
  • 52,011
  • 32
  • 81
  • 111
0

Please look here and here for how you have to write things that mean alphanumerics, whitespace (probably only horizontal not vertical), dashes, and apostrophes so that it works correctly on Unicode data.

The code there is in Java, but I am pretty sure the same rules and restrictions apply to C♯ as well. The short story is that you have to write this in Java:

dashes                  = "[\\u002D\\u058A\\u05BE\\u1400\\u1806\\u2010\\u2011\\u2012\\u2013\\u2014\\u2015\\u2053\\u207B\\u208B\\u2212\\u2E17\\u2E1A\\u301C\\u3030\\u30A0\\uFE31\\uFE32\\uFE58\\uFE63\\uFF0D]";
horizontal_whitespaces  = "[\\u0009\\u0020\\u00A0\\u1680\\u180E\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200A\\u202F\\u205F\\u3000]";
vertical_whitespace     = "[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]";
whitespace              = "[\\u000A\\u000B\\u000C\\u000D\\u0020\\u0085\\u00A0\\u1680\\u180E\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]";
identifier_chars        = "[\\pL\\pM\\p{Nd}\\p{Nl}\\p{Pc}[\\p{InEnclosedAlphanumerics}&&\\p{So}]]";
apostrophic_chars       = "[\\u0027\\u02BC\\u2019\\uFF07]";
quotation_mark_chars    = "[\\u0022\\u0027\\u00AB\\u00BB\\u2018\\u2019\\u201A\\u201B\\u201C\\u201D\\u201E\\u201F\\u2039\\u203A\\u300C\\u300D\\u300E\\u300F\\u301D\\u301E\\u301F\\uFE41\\uFE42\\uFE43\\uFE44\\uFF02\\uFF07\\uFF62\\uFF63]";
leading_punctuation     = "[\\p{Ps}\\p{Pi}]";
closing_punctuation     = "[\\p{Pe}\\p{Pf}]";
boundary                = "(?:(?<=[\\pL\\pM\\p{Nd}\\p{Nl}\\p{Pc}[\\p{InEnclosedAlphanumerics}&&\\p{So}]])(?![\\pL\\pM\\p{Nd}\\p{Nl}\\p{Pc}[\\p{InEnclosedAlphanumerics}&&\\p{So}]])|(?<![\\pL\\pM\\p{Nd}\\p{Nl}\\p{Pc}[\\p{InEnclosedAlphanumerics}&&\\p{So}]])(?=[\\pL\\pM\\p{Nd}\\p{Nl}\\p{Pc}[\\p{InEnclosedAlphanumerics}&&\\p{So}]]))";

Amazing, but true.

Community
  • 1
  • 1
tchrist
  • 78,834
  • 30
  • 123
  • 180