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.