Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with ASP.NET Custom Validator for a Comma Separated List of Email Addresses

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:

"[email protected] [email protected]"

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

like image 325
Rodney Avatar asked Dec 30 '25 15:12

Rodney


1 Answers

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.

like image 183
Wil P Avatar answered Jan 01 '26 03:01

Wil P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!