Java Email Validate isValidEmail(final String email)

Here you can find the source of isValidEmail(final String email)

Description

Indicates wether a given string is a valid email address

Email format is :

  • starts with A-Za-z or '-'
  • then has any number of A-Za-z (can be zero)
  • has any number of '.'

    License

    Open Source License

    Parameter

    Parameter Description
    email the string to validate

    Return

    true if the string is a valid email address, false otherwise

    Declaration

    public static boolean isValidEmail(final String email) 
    

    Method Source Code

    
    //package com.java2s;
    // under the terms of the GNU General Public License as published by the
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main {
        /**//from  ww w.  j  a v  a2  s.c  o  m
         * <p>
         * Indicates wether a given string is a valid email address
         * </p>
         * <p>
         * Email format is :
         * <li>starts with A-Za-z or '-'</li>
         * <li>then has any number of A-Za-z (can be zero)</li>
         * <li>has any number of '.' followed by one of more characters</li>
         * <li>has an '@'</li>
         * <li>has any number of A-Za-z0-9</li>
         * <li>has a '.' then 2 to 4 A-Za-z</li>
         * </p>
         * 
         * @param email the string to validate
         * @return <code>true</code> if the string is a valid email address,
         *         <code>false</code> otherwise
         */
        public static boolean isValidEmail(final String email) {
            final String expression = "^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")"
                    + "@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$";
            final Pattern pattern = Pattern.compile(expression);
    
            final Matcher matcher = pattern.matcher(email);
            return matcher.matches();
        }
    }
    

    Related

    1. isEmailValid(String emailAddress)
    2. isLegarEmail(String email)
    3. isMail(String email)
    4. isMobilesOrEmail(String str)
    5. isStrEmailAddress(String str)
    6. isValidEmail(final String email)
    7. isValidEmail(String email)
    8. isValidEmail(String email)
    9. isValidEmail(String email)