4

I tried to simulate email validation without regular expression in java and wrote this code. I need help to improve efficiency of this code (or give me a better way to solve this issue please)

regular expression: ^[(a-zA-Z-0-9-\\_\\+\\.)]+@[(a-z-A-z)]+\\.[(a-zA-z)]{2,3}$

public static boolean isValidEmailAddress(String email) {

    boolean flag1 = false;
    boolean flag2=false;
    boolean flag3=false;
    int j=0;
    char[] emailChars = email.toCharArray();
    for (int i = 0; i < emailChars.length;) {
        char c = emailChars[i];
        if (Character.isLetterOrDigit(c)) {
                i++;
            if(flag1==true&&flag2==true){
                j++;
            }
        } else if (c == '@' && flag1==false&&flag2==false && flag3==false) {
            i++;
            flag1=true;
        } else if(c=='.' && flag1==true && flag2==false&&flag3==false){
            i++;
            flag2 = true;
        }
        if(j==2 || j==3){
            flag3=true;
        }
    }
    return flag1&&flag2&&flag3;

}
sina pouya
  • 69
  • 1
  • 1
  • 8

2 Answers2

11

Real world email validation is not so primitive task and it was already solved many times. I'd suggest you not to re-invent the wheel but use Apache Commons EmailValidator.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • +1 If you want to know the reason why regexp is a bad idea here, just have a look at the official (and correct) regexp to validate email addresses: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – Aaron Digulla Jul 30 '14 at 07:53
  • @AaronDigulla, thanks for link. Cool regex. However the author wrote that this regex is generated automatically, so I believe it can be a little bit optimized. But anyway as I mentioned it is not trivial at all. This is the reason that Apache commons EmailValidator uses several regular expressions and some additional logic written in java. – AlexR Jul 30 '14 at 07:58
  • i know this but just want to validate with pure java – sina pouya Jul 30 '14 at 14:58
  • EmailValidator is absolutely pure java. – AlexR Jul 30 '14 at 15:00
  • in this line it maybe uses regex i wouldn't use regex http://commons.apache.org/proper/commons-validator/apidocs/src-html/org/apache/commons/validator/routines/EmailValidator.html#line.114 – sina pouya Jul 30 '14 at 15:18
2
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator {

    private Pattern pattern;
    private Matcher matcher;

    private static final String EMAIL_PATTERN = 
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
        + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    public EmailValidator() {
        pattern = Pattern.compile(EMAIL_PATTERN);
    }

    /**
     * Validate hex with regular expression
     * 
     * @param hex
     *            hex for validation
     * @return true valid hex, false invalid hex
     */
    public boolean validate(final String hex) {

        matcher = pattern.matcher(hex);
        return matcher.matches();

    }
}