Trying to validate a comma-separated email list in the textbox with asp:RegularExpressionValidator
, see below:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ErrorMessage="Wrong email format (separate multiple email by comma [,])" ControlToValidate="txtEscalationEmail"
Display="Dynamic" ValidationExpression="([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)" ValidationGroup="vgEscalation"></asp:RegularExpressionValidator>
It works just fine when I test it at http://regexhero.net/tester/, but it doesn't work on my page.
Here's my sample input:
[email protected],[email protected]
I've tried a suggestion in this post, but couldn't get it to work.
p.s. I don't want a discussion on proper email validation
[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times.
A common usage is for defining sub-addresses. Example: The user 'joe' with the mail address '[email protected]' can set up mail addresses like '[email protected]' if he needs more then one address. The comma is used in address header fields to separate email addresses from each other.
This Regex will allow emails with spaces after the commas.
^[\W]*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4}[\W]*,{1}[\W]*)*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4})[\W]*$
Playing around with this, a colleague came up with this RegEx that's more accurate. The above answer seems to let through an email address list where the first element is not an email address. Here's the update which also allows spaces after the commas.
Try this:
^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},?)+$
Adding the +
after the parentheses means that the preceding group can be present 1 or more times.
Adding the ^
and $
means that anything between the start of the string and the start of the match (or the end of the match and the end of the string) causes the validation to fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With