Home > Java > JAVA Check For A Valid Email Address String

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
Categories: Java Tags: , ,
  1. sharanya
    May 6, 2009 at 10:00 pm | #1

    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. August 31, 2009 at 11:36 am | #2

    Simple and short – I will try and use this code in my next email project.

    Thanks for sharing :)

  3. wwh
    September 20, 2009 at 4:33 am | #3

    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;
    }

  4. Milind
    May 30, 2011 at 6:01 am | #4

    Perfect validation for the ‘E-mail’. Thanks for this post… :)

  5. ve
    June 17, 2011 at 11:34 am | #5

    tnx a lot…………………………itz superb!!

  6. vvvvvvvvvvvvvv
    June 17, 2011 at 11:35 am | #6

    tnx a lot

  7. John
    February 19, 2013 at 4:59 pm | #7

    Simple awsum….:)

  8. PRAVEENKUMAR
    March 7, 2013 at 3:46 pm | #8

    oh! god thanks to every one for your solution,
    evry thing is working properly

  1. No trackbacks yet.

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

Follow

Get every new post delivered to your Inbox.