Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Email validation using regex

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 250
    Comment on it

    Email validation using regex

    The Email Id such as adam.sandler@findnerd.com is made up of local-part, an @ symbol and then a domain part. Hence, Email Id is of the form : local_part@domain
    *TLD = Top Level Domain

    1. Some Valid Emails

      1. findnerd@yahoo.com, findnerd-100@yahoo.com, findnerd.100@yahoo.com
      2. findnerd222@mkyong.com, findnerd.100@findnerd .com.au
      3. findnerd@1.com, findnerd@gmail.com.com
      4. findnerd+100@gmail.com, findnerd-100@yahoo-test.com

    2. Some Invalid Emails

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


    Here is the Class which validates Email Id using regex:

    public class EmailValidator {
    
        private Pattern pattern;
        private Matcher matcher;
    
        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,})$";
    
        public EmailValidator() {
            pattern = Pattern.compile(EMAIL_PATTERN);
        }
    
        /**
         * Validate hex with regular expression
         * 
         * @param email
         *            hex for validation
         * @return true valid hex, false invalid hex
         */
        public boolean validate(final String email) {
    
            matcher = pattern.matcher(email);
            return matcher.matches();
    }
    }
    

    And in your Class you can validate the Email like this:

    String emailID = findnerd@.gmail.com; // It is a invalid email
    EmailValidator emailValidator = new EmailValidator();
    if(emailValidator.validate(emailID)){
    {
    // Email is valid, do something
    }
    else{
    // Email is invalid, do something
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: