Trying to validate a comma-separated email list in the textbox with asp:RegularExpressionValidator
, see below:
A simple modification of @Donut's answer allows adjacent commas, all TLDs of two characters or more, and arbitrary whitespace between email addresses and commas.
^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,}(\s*,?\s*)*)+$
You will need to split and remove whitespace and empty strings on your side, but this should be an overall better user experience.
Examples of matched lists:
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.
The first answer which is selected as best matches the string like abc@xyz.comxyz@abc.com
which is invalid.
The following regex will work for comma separated email ids awesomely.
^([\w+-.%]+@[\w.-]+\.[A-Za-z]{2,4})(,[\w+-.%]+@[\w.-]+\.[A-Za-z]{2,4})*$
It will match single emailId, comma separated emailId but not if comma is missed.
First group will match string of single emailId. Second group is optionally required by '*' token i.e. either 0 or more number of such group but ',' is required to be at the beginning of such emailId which makes comma separated emailId to match to the above regex.
The solution that work for me is the following
^([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)(,([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+))*
The regex below is less restrictive and more appropriate for validating a manually-entered list of comma-separated email addresses. It allows for adjacent commas.
^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},*[\W]*)+$
Use the following regex, it will resolve your problem. The following regex will entertain post and pre spaces with comma too
/^((([a-zA-Z0-9_-.]+)@([a-zA-Z0-9_-.]+).([a-zA-Z\s?]{2,5}){1,25})(\s?,\s*?))$/