0

Possible Duplicate:
Verify email in Java

I'm trying to do a very simple email validation. But the problem is when I try something like tom@@@@gmail.com it returns true.

Here are the codes:

public static boolean validateEmail(String email){
        boolean isValidEmail = false;
       // Input the string for validation
       // String email = "xyz@.com";
       // Set the email pattern string
       Pattern p = Pattern.compile(".+@.+\\.[a-z]+");

       // Match the given string with the pattern
       Matcher m = p.matcher(email);

       // check whether match is found
       boolean matchFound = m.matches();

       StringTokenizer st = new StringTokenizer(email, ".");
       String lastToken = null;
       while (st.hasMoreTokens()) {
          lastToken = st.nextToken();
       }

       if (matchFound && lastToken.length() >= 2
          && email.length() - 1 != lastToken.length()) {


          // validate the country code
          isValidEmail = true;
       }
       else isValidEmail = false;

    return isValidEmail;
}

Please help. Thanks in advance.

Community
  • 1
  • 1
NinjaBoy
  • 3,715
  • 18
  • 54
  • 69

5 Answers5

6

.+@.+ will match anything, including @, followed by @, followed by anything, including @. Use [^@]+@[^@]+ instead.

Or cease from reinventing the wheel, grab Apache Commons and use its EmailValidator.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
5

You can use the class javax.mail.internet.InternetAddress from the Javamail API (there is a validate method)

barjak
  • 10,842
  • 3
  • 33
  • 47
  • How am I gonna import that? I tried but its not in my default java library. Im so new to this. Please help. – NinjaBoy Nov 24 '11 at 16:20
  • It is in the Java EE distribution. – Hunter McMillen Nov 24 '11 at 16:31
  • @ChickenBoy you can download this library on Oracle's website. Just look for "javamail". If you use maven, you can add the dependency on your pom.xml directly (http://mvnrepository.com/artifact/javax.mail/mail/1.4.4). – barjak Nov 24 '11 at 17:14
2

This is a solved problem in numerous libraries. Do not reimplement your own.

Getting this 100% correct in regexps is much, much harder than you think it is.

kittylyst
  • 5,640
  • 2
  • 23
  • 36
2

This site suggests that the following pattern matches RFC 5322 and covers most email addresses used today:

Pattern p = Pattern.compile("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", Pattern.CASE_INSENSITIVE);

It worked for a few quick tests that I made.

Consider storing the compiled pattern in a constant for improved performance.

barfuin
  • 16,865
  • 10
  • 85
  • 132
1

It should be Pattern p = Pattern.compile(".+@{1}+\\.[a-z]+");

BUT:

This my answer above is wrong exactly like the answer @larsmans, because:

this(comment)me@demo.com is a valid email.

You have to read the RFC: https://www.rfc-editor.org/rfc/rfc5322

In fact, use a good library, as apache-commons.

Community
  • 1
  • 1
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • 1
    An application validating an e-mail address does not have the obligation to validate all variations on e-mail address formats just because they are permitted in an Internet message. Also, if the address will be used for sending e-mail with SMTP, bear in mind that the e-mail address format permitted by SMTP is much more restricted and does not include comments (or for that matter, anything but the mailbox name, at sign, and FQDN). – MetaEd Nov 25 '11 at 01:48
  • Where do you get these information? Is there another RFC? – Christian Kuetbach Nov 25 '11 at 07:31
  • SMTP has gone through a number of revisions. RFC 821 is the latest which has been designated a standard. The latest *draft* standard for SMTP is RFC 5321. – MetaEd Nov 26 '11 at 19:50