dengzedong
2024-12-24 aa0382e44311f9f7e62a688c8fcaa9c69a512e0f
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.common.validation;
H 2
3 import cn.hutool.core.text.CharSequenceUtil;
4 import cn.hutool.core.util.PhoneUtil;
5
6 import javax.validation.ConstraintValidator;
7 import javax.validation.ConstraintValidatorContext;
8
9 public class TelephoneValidator implements ConstraintValidator<Telephone, String> {
10
11     @Override
12     public void initialize(Telephone annotation) {
13     }
14
15     @Override
16     public boolean isValid(String value, ConstraintValidatorContext context) {
17         // 如果手机号为空,默认不校验,即校验通过
18         if (CharSequenceUtil.isEmpty(value)) {
19             return true;
20         }
21         // 校验手机
22         return PhoneUtil.isTel(value) || PhoneUtil.isPhone(value);
23     }
24
25 }