3

I followed a blog post here to use a custom validator to validate a list of emails. However, the Regex expression in the article:

Regex emailRegEx = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
                       RegexOptions.IgnoreCase);

allows this email address through:

"APL@domain.com 123@domain.com"

which is clearly invalid.

How can I change it to flag this as invalid?

Note: When you use the core RegEx Validator with the SAME regex expression it DOES catch that email, so perhaps it is a problem with the matching options?

Thanks

Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
Rodney
  • 5,417
  • 7
  • 54
  • 98

1 Answers1

2
Regex emailRegEx = new Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.IgnoreCase);

It looks like you need to terminate your regular expression. Check the '^' at the start and the "$" at the end of the expression above.

Wil P
  • 3,341
  • 1
  • 20
  • 20
  • +1 this is the answer. The reason the RegularExpressionValidator control works for the OP is because these anchors are assumed, and they match correctly as a result. However, the Regex class assumes nothing, so they must be specified explicitly. – Ahmad Mageed Oct 21 '09 at 03:15