package com.nq.utils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
public final class PhoneUtil {
|
|
|
|
/**
|
|
* 严格大陆手机号(11位):有效运营商号段
|
|
* 13x | 14[5-9] | 15[0-35-9] | 16[2567] | 17[0-8] | 18x | 19[0-35-9]
|
|
*/
|
|
private static final Pattern STRICT_MOBILE = Pattern.compile(
|
|
"^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$");
|
|
|
|
private PhoneUtil() {
|
|
}
|
|
|
|
/** 规范为 11 位大陆手机号,非法返回 null */
|
|
public static String normalizeChinaMobile(String phone) {
|
|
if (StringUtils.isBlank(phone)) {
|
|
return null;
|
|
}
|
|
String digits = phone.trim().replaceAll("[^0-9]", "");
|
|
if (digits.startsWith("86") && digits.length() == 13) {
|
|
digits = digits.substring(2);
|
|
}
|
|
if (digits.length() != 11 || !digits.startsWith("1")) {
|
|
return null;
|
|
}
|
|
return digits;
|
|
}
|
|
|
|
public static boolean isValidChinaMobile(String phone) {
|
|
String normalized = normalizeChinaMobile(phone);
|
|
return normalized != null && STRICT_MOBILE.matcher(normalized).matches();
|
|
}
|
|
}
|