電話號碼格式驗證工具類
public class PhoneFormatCheckUtils {
/**
* 大陸號碼或香港號碼均可
*/
public static boolean isPhoneLegal(String str)throws PatternSyntaxException {
return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
}
/**
* 大陸手機號碼11位數(shù),匹配格式:前三位固定格式+后8位任意數(shù)
* 此方法中前三位格式有:
* 13+任意數(shù)
* 15+除4的任意數(shù)
* 18+除1和4的任意數(shù)
* 17+除9的任意數(shù)
* 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位數(shù),5|6|8|9開頭+7位任意數(shù)
*/
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();
}
}