JAVA Check For A Valid Email Address String

How to check for a valid email address string.

Example Code:

String email = “test@test.com”;
Pattern p = Pattern.compile(“.+@.+\\.[a-z]+”);
Matcher m = p.matcher(email);
boolean matchFound = m.matches();

if(matchFound){
System.out.println(“EMAIL OK”);
}else{
System.out.println(“EMAIL ERROR”);
}

About these ads
JAVA Check For A Valid Email Address String

8 thoughts on “JAVA Check For A Valid Email Address String

  1. sharanya says:

    this post is very useful… thank u…
    (import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    are also needed…. this info will be useful for the beginners like me)

  2. wwh says:

    Yet another solution (requires JavaMail):

    public static boolean isValidEmailAddress(String email)
    {
    try {
    new javax.mail.internet.InternetAddress(email, true);
    } catch (javax.mail.internet.AddressException e) {
    return false;
    }
    return true;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s