Java Email Validate isValidEmailAddress(String emailAddress)

Here you can find the source of isValidEmailAddress(String emailAddress)

Description

Validates that an email address is a valid email address.

License

Apache License

Parameter

Parameter Description
emailAddress The email address to be validated.

Return

Returns false if the email address is null, whitespace only, or not a valid email address; otherwise, true is returned.

Declaration

public static boolean isValidEmailAddress(String emailAddress) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 The Regents of the University of California
 * //from  w w w .ja va  2  s .c  o  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.regex.Pattern;

public class Main {
    private static final Pattern EMAIL_PATTERN = Pattern
            .compile("^([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

    /**
     * Validates that an email address is a valid email address.
     * 
     * @param emailAddress The email address to be validated.
     * 
     * @return Returns false if the email address is null, whitespace only, or
     *          not a valid email address; otherwise, true is returned.
     */
    public static boolean isValidEmailAddress(String emailAddress) {
        if (isEmptyOrWhitespaceOnly(emailAddress)) {
            return false;
        }

        return EMAIL_PATTERN.matcher(emailAddress).matches();
    }

    /**
     * Checks for a null or empty (zero-length or all whitespace) String.
     * 
     * A method with the same signature and behavior as this one exists in the MySQL JDBC code. That method is not used outside 
     * of the data layer of this application in order to avoid unnecessary dependencies i.e., AW utility classes should not
     * depend on a third-party data access lib.
     * 
     * @return true if the String is null, empty, or all whitespace
     *         false otherwise
     */
    public static boolean isEmptyOrWhitespaceOnly(String string) {

        return null == string || "".equals(string.trim());

    }
}

Related

  1. isValidEmailAddress(String email)
  2. isValidEmailAddress(String email)
  3. isValidEmailAddress(String email)
  4. isValidEmailAddress(String email)
  5. isValidEmailAddress(String email)
  6. isValidEmailAddress(String emailAddress)
  7. isValidEmailAddress(String emailAddress)
  8. isValidEmailAddress(String emailAddress)
  9. isValidEmailAddress(String emailAddress)