validator snippets

Validating an email address in Java

Tagged email, validator, regexp, java, rfc 2822  Languages java

The source of the regexp is this site: Email unlimited. According to Wikipedia the regexp on the source page validates the email address according to RFC 2822 - Internet Message Format. I have still to write a comprehensive test suite, but the tests I do have for this validator pass.

public class EmailValidator {
  public static boolean validate(final String emailAddress) {
    if ( emailAddress == null ) return false;
    String patternStr = "^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\\.[a-zA-Z](-?[a-zA-Z0-9])*)+$";
    Pattern emailPattern = Pattern.compile(patternStr);
    return emailPattern.matcher(emailAddress).matches();