Regex for validating multiple E-Mail-Addresses
This is your original expression, changed so that it allows several emails separated by semicolon and (optionally) spaces besides the semicolon. It also allows a single email address that doesn't end in semicolon.
This allows blank entries (no email addresses). You can replace the final * by + to require at least one address.
(([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*;\s*|\s*$))*
If you need to allow comma, apart from semicolon, you can change this group:
(\s*;\s*|\s*$)
by this one:
(\s*(;|,)\s*|\s*$)
Important note: as states in the comment by Martin, if there are additional text before or after the correct email address list, the validation will not fail. So it would work as an "email searcher". To make it work as a validator you need to add ^
at the beginning of the regex, and $
at the end. This will ensure that the expression matches all the text. So the full regex would be:
^(([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)(\s*;\s*|\s*$))*$
You can add an extra \s*
after the ^
to tolerate blanks at the beginning of the list, like this. I.e. include ^\s*
instead of simply ^
The expression already tolerates blanks at the end as is.
Old post - needed the same RegEx. The accepted answer did not work for me, however, this did.
^(|([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$
Retrieved from this post, however, the accepted answer did not work either, but the Regex on the link WITHIN the post did.
[email protected]
- validates
[email protected];[email protected]
- validates
[email protected];
- does not validate
empty string
- validates
If you want to validate against an empty string, then remove the |
at the beginning of the regex