import com.photon.Common.Exception.CustomerException;
import com.photon.Common.Exception.SimpleException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class PhoneCheckUtil {
/**
* ^ 匹配输入字符串开始的位置
* \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
* $ 匹配输入字符串结尾的位置
*/
public static final String CHINA_REGEX_EXP = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[0-9])|(18[0-9])|(19[1,8,9]))\\d{8}$";
public static final String HK_REGEX_EXP = "^(5|6|8|9)\\d{7}$";
public static void main(String[] args) {
String phoneNum = "";
System.out.println(isPhoneNum(phoneNum));
}
/**
* 校验是否为大陆号码或香港号码
*
* @param str
* @return 符合规则返回true
* @throws PatternSyntaxException
*/
public static boolean isPhoneNum(String str){
return isChinaPhoneNum(str);
}
/**
* 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
* 此方法中前三位格式有:
* 13+任意数
* 145,147,149
* 15+除4的任意数(不要写^4,这样的话字母也会被认为是正确的)
* 166
* 17+任意数
* 18+任意数
* 198,199
*
* @param str
* @return 正确返回true
* @throws PatternSyntaxException
*/
public static boolean isChinaPhoneNum(String str){
// ^ 匹配输入字符串开始的位置
// \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
// $ 匹配输入字符串结尾的位置
Pattern p = Pattern.compile(CHINA_REGEX_EXP);
Matcher m = p.matcher(str);
return m.matches();
}
/**
* 香港手机号码8位数,5|6|8|9开头+7位任意数
*
* @param str
* @return 正确返回true
* @throws PatternSyntaxException
*/
public static boolean isHkPhoneNum(String str) {
Pattern p = Pattern.compile(HK_REGEX_EXP);
Matcher m = p.matcher(str);
return m.matches();
}
}
手机号校验工具类
最新推荐文章于 2024-06-19 11:34:50 发布