校驗手機號和郵箱工具類(java)

可以直接拿來用

package com.imagedt.pawpaw.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 對手機號與郵箱的簡單校驗
 * @author mousejoo
 */
public class CheckUtil {
 
	/**
	 * 郵箱校驗
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		try {
			// 正常郵箱
			// /^\w+((-\w)|(\.\w))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
//                ^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$
			// 含有特殊 字符的 個人郵箱 和 正常郵箱
			// js: 個人郵箱
			// /^[\-!#\$%&'\*\+\\\.\/0-9=\?A-Z\^_`a-z{|}~]+@[\-!#\$%&'\*\+\\\.\/0-9=\?A-Z\^_`a-z{|}~]+(\.[\-!#\$%&'\*\+\\\.\/0-9=\?A-Z\^_`a-z{|}~]+)+$/
 
			// java:個人郵箱
			// [\\w.\\\\+\\-\\*\\/\\=\\`\\~\\!\\#\\$\\%\\^\\&\\*\\{\\}\\|\\'\\_\\?]+@[\\w.\\\\+\\-\\*\\/\\=\\`\\~\\!\\#\\$\\%\\^\\&\\*\\{\\}\\|\\'\\_\\?]+\\.[\\w.\\\\+\\-\\*\\/\\=\\`\\~\\!\\#\\$\\%\\^\\&\\*\\{\\}\\|\\'\\_\\?]+
 
			// 範圍 更廣的 郵箱驗證 “/^[^@]+@.+\\..+$/”
			final String pattern1 = "[\\w.\\\\+\\-\\*\\/\\=\\`\\~\\!\\#\\$\\%\\^\\&\\*\\{\\}\\|\\'\\_\\?]+@[\\w.\\\\+\\-\\*\\/\\=\\`\\~\\!\\#\\$\\%\\^\\&\\*\\{\\}\\|\\'\\_\\?]+\\.[\\w.\\\\+\\-\\*\\/\\=\\`\\~\\!\\#\\$\\%\\^\\&\\*\\{\\}\\|\\'\\_\\?]+";
 
			final Pattern pattern = Pattern.compile(pattern1);
			final Matcher mat = pattern.matcher(email);
			return mat.matches();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
 
	/**
	 * CJYFIXME蒐集號段時間:2017-11-28(這個之後的請自行添加) 手機號:目前全國有27種手機號段。
	 * 移動有19個號段:134(0-8)、135、136、137、138、139、147(147(數據卡))、148(物聯網)、150、151、152、
	 * 157、158、159、178、182、183、184、187、188、198。
	 * 聯通有11種號段:130、131、132、--145(數據卡)--、146(物聯網)、155、156、166、171、175、176、185、
	 * 186。 電信有7個號段:133、--1349--、149、153、173、177、180、181、189、199。 虛擬運營商:
	 * (1).移動:1703、1705、1706 (2).聯通:1704、1707、1708、1709、171
	 * (3).電信:1700、1701、1702 衛星通信:1349
	 * <p>
	 * 工業和信息化部公示了2017年第10批“電信網碼號資源使用證書”頒發結果,批准同意部分單位提出的電信網碼號資源有關申請,
	 * 其中三大運營商均獲得相關物聯網號段。 移動: (1).198(0-9)號段(公衆移動通信網號) (2).148(0-9)號段(物聯網業務專用號段)
	 * (3).1440(0-9)號段(物聯網網號) (4).(460)13(移動網絡識別碼) 聯通: (1).166(0-9)號段(公衆移動通信網號)
	 * (2).146(0-9)號段(物聯網業務專用號段) 電信: (1).1740(0-5)號段(衛星移動通信業務號)、
	 * (2).199(0-9)號段(公衆移動通信網號)、 (3).1410(0-9)號段(物聯網網號)、 (4).(460)59(移動網絡識別碼)
	 * 由於物聯網號段一般用在家用傢俱上,所以這裏不考慮物聯網號段,物聯網號碼的總位數是13或者14還沒搞清楚
	 * =========================================================================
	 * ======================
	 * 總結一下:虛擬運營商、數據卡、物聯網、衛星通信、移動網絡識別碼都不作爲正常使用的電話號碼,所以需要驗證的手機號如下:
	 * 130、131、132、133、134(0-8)、135、136、137、138、139 149
	 * 150、151、152、153、155、156、157、158、159 166、 173、175、176、177、178、
	 * 180、181、182、183、184、185、186、187、188、189 198、199
	 */
	private static final String REGEX_MOBILE = "(134[0-8]\\d{7})" + "|(" + "((13([0-3]|[5-9]))" + "|149"
			+ "|15([0-3]|[5-9])" + "|166" + "|17(3|[5-8])" + "|18[0-9]" + "|19[8-9]" + ")" + "\\d{8}" + ")";
 
	/**
	 * 判斷是否是手機號
	 * 
	 * @param tel
	 *            手機號
	 * @return boolean true:是 false:否
	 */
	public static boolean isMobile(String tel) {
		return Pattern.matches(REGEX_MOBILE, tel);
	}

    public static boolean isPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章