添加校验单参数是否是正确的手机号的方法

--ValidatorUtils.java 添加新方法
This commit is contained in:
剑雨
2025-05-15 22:47:57 +08:00
parent 0509ddb129
commit aa251aa011
@@ -2,6 +2,8 @@ package xiaozhi.common.validator;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.springframework.context.i18n.LocaleContextHolder;
@@ -45,4 +47,23 @@ public class ValidatorUtils {
throw new RenException(constraint.getMessage());
}
}
/**
* 手机号正则表达式
*/
private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
/**
* 校验单参数是否是正确的手机号
* @param phone 手机号
* @return boolean
*/
public static boolean isValidPhone(String phone) {
if (phone == null || phone.isEmpty()) {
return false;
}
Pattern pattern = Pattern.compile(PHONE_REGEX);
Matcher matcher = pattern.matcher(phone);
return matcher.matches();
}
}