Regex for validating multiple E-Mail-Addresses

I got a Regex that validates my mail-addresses like this:

([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})(\]?)

This works perfectly fine, but only allows one e-mail to be entered. Now I wanted to extend that and allow multiple mail-addresses to be added (just like MS Outlook, for example) with a semicolon as a mail-splitter.

[email protected];[email protected];[email protected]

Now I've searched and found this one:

([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}(;|$))

This works on one point, but sadly requires a semicolon at the end of a mail:

[email protected];

This is not what I want when the user only enters one e-mail.

How can I extend my regex above (the first one) to allow multiple mail-addresses to be added while let them be splitted through a semicolon?


Solution 1:

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.

Solution 2:

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

Solution 3:

Please try this

^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4};?)+$

Solution 4:

Why not just split on the semicolon and then validate each potential email address using your existing regexp ? Writing one huge regexp is going to be very difficult and a maintenance nightmare, I suspect.

Solution 5:

In an old regex book they stated that you cannot write a regex to match all valid email addresses (although you can come close).

Here is a website dealing with regex and email addresses.

I would recommend that you split the string at ; and , boundaries and check each email address separately for being valid/invalid with your regex.