手機號校驗
/**
* 大陸手機號碼11位數,匹配格式:前三位固定格式+後8位任意數
* 此方法中前三位格式有:
* 13+任意數
* 15+除4的任意數
* 18+除1和4的任意數
* 17+除9的任意數
* 147
*/
public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(str);
return m.matches();
}
/**
* 香港手機號碼8位數,5|6|8|9開頭+7位任意數
*/
public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {
String regExp = "^(5|6|8|9)\\d{7}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(str);
return m.matches();
}
其他參數類型正則表達式
/**
* 正則表達式:驗證用戶名
*/
public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";
/**
* 正則表達式:驗證密碼
*/
public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
/**
* 正則表達式:驗證手機號
*/
public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
/**
* 正則表達式:驗證郵箱
*/
public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
/**
* 正則表達式:驗證漢字
*/
public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
/**
* 正則表達式:驗證身份證
*/
public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
/**
* 正則表達式:驗證URL
*/
public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
/**
* 正則表達式:驗證IP地址
*/
public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";