0

I am using java regex package to validate an email as below:

Pattern p = Pattern.compile("^[ _A-Za-z0-9-]+(\\.[ _A-Za-z0-9-]+)*@" 
                          + "[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,4})$");

This pattern works fine for accepting one email throw textbox. But, now I need a pattern that will validate multiple emails separated by commas.

Can any one tell me the pattern?

Tim Post
  • 33,371
  • 15
  • 110
  • 174
ThunderDragon
  • 613
  • 3
  • 13
  • 31
  • 10
    Or split on commas and validate each one individually. This also allows you to report which emails failed validation more easily. In any case, you'd use the regex, then a group of "(,[same regex])+". – Dave Newton Dec 13 '11 at 14:36
  • 1
    Splitting on commas first is going to be hugely more efficient than a single regex, if that's important to you. – DJClayworth Dec 13 '11 at 14:53
  • Your regexp seems like one that can easily be found on the net. While it is possibly among the better ones, it still is too strict to have all perfectly valid e-mails pass. Read [this](http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx) for example or look at [this](http://www.regexplanet.com/simple/index.html) site and validate _!def!xyz%abc@example.com_ with your pattern. Yes that is apparently a perfectly valid address. – Wivani Dec 13 '11 at 15:12
  • Aren't we supposed to mention [this](http://www.codinghorror.com/blog/2008/06/regular-expressions-now-you-have-two-problems.html) as well? :-) – Wivani Dec 13 '11 at 15:32
  • yeh, but how can it be possible? – ThunderDragon Dec 13 '11 at 15:51

2 Answers2

1

Something like the following:

String regex = "[ _A-Za-z0-9-]+(\\.[ _A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,4})";

Pattern p = Pattern.compile("^(?:" + regex + "\\s*,\\s* + ")*" + regex + "$");

Or, alternatively, first split the string using split("\\s*,\\s*"), then iterate over the array and validate each email address using your pattern.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

Based on your question history you seem to need this for a JSF <h:inputText> component. I strongly recommend to look for a different and more robust approach.

In JSF, validation should absolutely not be done inside a managed bean action method, but inside a fullworthy Validator implementation. Therein you can just check every mail address individually.

@FacesValidator("mailAddressesValidator")
public class MailAddressesValidator implements Validator {

    private static final Pattern PATTERN = Pattern.compile("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)");

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return;
        }

        String[] mailAddresses = (String[]) value;
        List<FacesMessage> messages = new ArrayList<FacesMessage>();        

        for (String mailAddress : mailAddresses) {
            if (!PATTERN.matcher(mailAddress).matches()) {
                messages.add(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid mail address format: " + mailAddress, null));
            }
        }

        if (!messages.isEmpty()) {
            throw new ValidatorException(messages);
        }
    }
}

Use it as follows:

<h:inputText value="#{bean.mailAddresses}" validator="mailAddressesValidator" />

Please note that the pattern is more lenient as to "invalid characters", because non-latin characters like Cyrillic, Hebrew, Sanskrit, Chinese, etc in an email address are valid! You can if you want always change it to a different pattern which should match an individual mail address as per the business requirements.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555