Wednesday, December 21, 2011

Email validatation with regular expression.

Email Regular Expression Pattern
 
^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@
[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

Description
^   #start of the line
  [_A-Za-z0-9-]+ #  must start with string in the bracket [ ], must contains one or more (+)
  (   #  start of group #1
    \\.[_A-Za-z0-9-]+ #     follow by a dot "." and string in the bracket [ ], must contains one or more (+)
  )*   #  end of group #1, this group is optional (*)
    @   #     must contains a "@" symbol
     [A-Za-z0-9]+       #        follow by string in the bracket [ ], must contains one or more (+)
      (   #    start of group #2 - first level TLD checking
       \\.[A-Za-z0-9]+  #      follow by a dot "." and string in the bracket [ ], must contains one or more (+)
      )*  #    end of group #2, this group is optional (*)
      (   #    start of group #3 - second level TLD checking
       \\.[A-Za-z]{2,}  #      follow by a dot "." and string in the bracket [ ], with minimum length of 2
      )   #    end of group #3
$   #end of the line


Whole combination is means, email address must start with “_A-Za-z0-9-” , optional follow by “.[_A-Za-z0-9-]“, and end with a “@” symbol. The email’s domain name must start with “A-Za-z0-9″, follow by first level Tld (.com, .net) “.[A-Za-z0-9]” and optional follow by a second level Tld (.com.au, .com.my) “\\.[A-Za-z]{2,}”, where second level Tld must start with a dot “.” and length must equal or more than 2 characters.

Java Regular Expression Example

Here’s a Java example to show the use of regex to validate an email address.

/*
 * restTestApp - EmailValidator.java, Dec 21, 2011 11:44:50 PM
 *

 */
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The Class EmailValidator.
 * @author Rajakrishna V. Reddy
 */
public class EmailValidator
{
   
    /** The pattern. */
    private Pattern pattern;
   
    /** The matcher. */
    private Matcher matcher;
   
    /** The Constant EMAIL_PATTERN. */
    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,})$";
   
    /**
     * Instantiates a new email validator.
     */
    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();
    }
   
    public static void main(String[] args)
    {
        final String email = "yahoo.com@yahoo.com";
        final EmailValidator emailValidator = new EmailValidator();
        System.out.println("Is a Valid Email: "+emailValidator.validate(email));
    }
}

Emails that match:

1. “mkyong@yahoo.com”, “mkyong-100@yahoo.com”,”mkyong.100@yahoo.com”
2. “mkyong111@mkyong.com”, “mkyong-100@mkyong.net”,”mkyong.100@mkyong.com.au”
3. “mkyong@1.com”, “mkyong@gmail.com.com”

Emails that doesn’t match:

1. “mkyong” – must contains “@” symbol
2. “mkyong@.com.my” – tld can not start with dot “.”
3. “mkyong123@gmail.a” – “.a” is not a valid tld, last tld must contains at least two characters
4. “mkyong123@.com” – tld can not start with dot “.”
5. “mkyong123@.com.com” – tld can not start with dot “.”
6. “.mkyong@mkyong.com” – email’s first character can not start with dot “.”
7. “mkyong()*@gmail.com” – email’s is only allow character, digit, underscore and dash
8. “mkyong@%*.com” – email’s tld is only allow character and digit
9. “mkyong..2002@gmail.com” – double dots “.” are not allow
10. “mkyong.@gmail.com” – email’s last character can not end with dot “.”
11. “mkyong@mkyong@gmail.com” – double “@” is not allow
12. “mkyong@gmail.com.1a” -email’s tld which has two characters can not contains digit

Unit Test – Result

Here’s the unit test result.
Email is valid : mkyong@yahoo.com , true
Email is valid : mkyong-100@yahoo.com , true
Email is valid : mkyong.100@yahoo.com , true
Email is valid : mkyong111@mkyong.com , true
Email is valid : mkyong-100@mkyong.net , true
Email is valid : mkyong.100@mkyong.com.au , true
Email is valid : mkyong@1.com , true
Email is valid : mkyong@gmail.com.com , true
Email is valid : mkyong , false
Email is valid : mkyong@.com.my , false
Email is valid : mkyong123@gmail.a , false
Email is valid : mkyong123@.com , false
Email is valid : mkyong123@.com.com , false
Email is valid : .mkyong@mkyong.com , false
Email is valid : mkyong()*@gmail.com , false
Email is valid : mkyong@%*.com , false
Email is valid : mkyong..2002@gmail.com , false
Email is valid : mkyong.@gmail.com , false
Email is valid : mkyong@mkyong@gmail.com , false
Email is valid : mkyong@gmail.com.1a , false
PASSED: ValidEmailTest([Ljava.lang.String;@1a626f)
PASSED: InValidEmailTest([Ljava.lang.String;@1975b59)
 
===============================================
    com.mkyong.regex.EmailValidatorTest
    Tests run: 2, Failures: 0, Skips: 0
===============================================
 
===============================================
mkyong
Total tests run: 2, Failures: 0, Skips: 0
===============================================
 

Reference

  1. http://en.wikipedia.org/wiki/E-mail_address
  2. http://tools.ietf.org/html/rfc2822#section-3.4.1
 

2 comments:

  1. This piece of writing will help the internet users for setting up new blog or even a blog from start to end.

    ReplyDelete
  2. Thanks for sharing your thoughts about %meta_keyword%. Regards

    ReplyDelete