Java Email Validate isValidEmailAddress(String addr)

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

Description

is Valid Email Address

License

Apache License

Declaration

public static boolean isValidEmailAddress(String addr) 

Method Source Code

//package com.java2s;
/*//from  ww  w .  ja  va  2s .c  o  m
 * Copyright 2012 Donghyuck, Son
 *
 * 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.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static Pattern basicAddressPattern;
    private static Pattern validUserPattern;
    private static Pattern domainPattern;
    private static Pattern ipDomainPattern;
    private static Pattern tldPattern;

    public static boolean isValidEmailAddress(String addr) {
        if (addr == null)
            return false;
        addr = addr.trim();
        if (addr.length() == 0)
            return false;
        Matcher matcher = basicAddressPattern.matcher(addr);
        if (!matcher.matches())
            return false;
        String userPart = matcher.group(1);
        String domainPart = matcher.group(2);
        matcher = validUserPattern.matcher(userPart);
        if (!matcher.matches())
            return false;
        matcher = ipDomainPattern.matcher(domainPart);
        if (matcher.matches()) {
            for (int i = 1; i < 5; i++) {
                String num = matcher.group(i);
                if (num == null)
                    return false;
                if (Integer.parseInt(num) > 254)
                    return false;
            }

            return true;
        }
        matcher = domainPattern.matcher(domainPart);
        if (matcher.matches()) {
            String tld = matcher.group(matcher.groupCount());
            matcher = tldPattern.matcher(tld);
            return tld.length() == 3 || matcher.matches();
        } else {
            return "localhost".equals(domainPart);
        }
    }
}

Related

  1. isValidEmail(String email)
  2. isValidEmail(String emailAddress)
  3. isValidEmail(String eMailAddress)
  4. IsValidEmail(String input)
  5. isValidEmailAddr(String str)
  6. isValidEmailAddress(String aEmailAddress)
  7. isValidEmailAddress(String email)
  8. isValidEmailAddress(String email)
  9. isValidEmailAddress(String email)