Simple and non-confusing e-mail confusion in Java String

advertisements

I am trying to find a simple method to check to see if a user's input meets a couple criteria for an email address. I've read through many threads on this subject and most seem to want to validate the email address too. I'm not trying to build some super duper email address validator/checker. I'm trying to build a method that checks for these things:

  1. The string entered by the user contains the '@' sign.
  2. There are at least two characters before the '@' sign.
  3. There is a '.' after the at sign followed by only three characters. The domain name can be as long as needed, but the string must end with "._ _ _". As in ".com" or ".net"...

I understand that this is not an all encompassing email address checker. That's not what I want though. I want just something this simple. I know that this is probably a routine question but I can't figure it out even after reading all of the seriously crazy ways of validating an email address.

This is the code I have so far: (Don't worry I already know it's pretty pathetic.... )

public static void checkEmail()
{
    validEmail(emailAddresses);
    if(validEmail(emailAddresses))
    {

    }
}

public static boolean validEmail(String email) {
    return email.matches("[A-Z0-9._%+-][A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{3}");
}


Not perfect, but gets the job done.

static boolean validEmail(String email) {
    // editing to make requirements listed
    // return email.matches("[A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{2,4}");
    return email.matches("[A-Z0-9._%+-][A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{3}");
}

void checkEmails() {
    for(String email : emailAddresses) {
        if(validEmail(email)) {
             // it's a good email - do something good with it
        }
        else {
             // it's a bad email - do something... bad to it? sounds dirty...
        }
    }
}