Java Email Validate isEmail(final String email)

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

Description

Is given email format correct?

License

Apache License

Parameter

Parameter Description
email a parameter

Return

true:ok, false: error.

Declaration

public static boolean isEmail(final String email) 

Method Source Code


//package com.java2s;
/*/* w w  w.  ja  v  a 2s  .  c om*/
 * Copyright [2011] [C.H Li http://code.google.com/p/idocbox-common/]
 * Licensed to the Chunhui Li(C.H Li) under one or more contributor license agreements.  
 * See the NOTICE file distributed with this work for additional information 
 * regarding copyright ownership.
 *
 * C.H Li licenses this file to You 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.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Is given email format correct?
     * 
     * @param email
     * @return true:ok, false: error.
     */
    public static boolean isEmail(final String email) {
        boolean isMatched = false;
        String check = "^([a-z0-9A-Z]+[-|//.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?//.)+[a-zA-Z]{2,}$";
        isMatched = isMatched(email, check);
        return isMatched;
    }

    /**
     * Is given string's format correct?
     * 
     * @param str
     *            string.
     * @param regExp
     *            regular expression.
     * @return true:ok, false: error.
     */
    public static boolean isMatched(final String str, final String regExp) {
        boolean isMatched = false;
        Pattern regex = Pattern.compile(regExp);
        Matcher matcher = regex.matcher(str);
        isMatched = matcher.matches();
        return isMatched;
    }
}

Related

  1. getFirstEmailAddr(String s)
  2. getSafeMailAddr(String mailAddr)
  3. getSmtpPort(String email)
  4. isEmail(CharSequence email)
  5. isEmail(CharSequence input)
  6. isEmail(final String s)
  7. isEmail(final String text)
  8. isEmail(String correo)
  9. isEmail(String data)