package com.zkj.zw.common.core.validate;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
/**
* @author: hzq
* @date: 2022/5/27
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {IsMobileValidator.class})
public @interface IsMobile {
// 默认错误消息
String message() default "请输入正确的手机号码";
// 分组
Class<?>[] groups() default {};
// 负载
Class<? extends Payload>[] payload() default {};
}
package com.zkj.zw.common.core.validate;
import cn.hutool.core.util.StrUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* 手机号码检验
*
* @author: hzq
* @date: 2022/4/27
*/
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {
// ^ 匹配输入字符串开始的位置
// \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
// $ 匹配输入字符串结尾的位置
private static final Pattern ch = Pattern.compile("^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])|(18[0-9])|(19[8,9]))\\d{8}$");
private static final Pattern hx = Pattern.compile("^(5|6|8|9)\\d{7}$");
@Override
public void initialize(IsMobile constraintAnnotation) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StrUtil.isBlank(value)) return true;
return isChinaPhoneLegal(value) || isHKPhoneLegal(value);
}
/**
* 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
* 此方法中前三位格式有:
* 13+任意数
* 145,147,149
* 15+除4的任意数(不要写^4,这样的话字母也会被认为是正确的)
* 166
* 17+3,5,6,7,8
* 18+任意数
* 198,199
*/
public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
return ch.matcher(str).matches();
}
/**
* 香港手机号码8位数,5|6|8|9开头+7位任意数
*/
public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {
return hx.matcher(str).matches();
}
}
package com.zkj.zw.common.core.validate;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
/**
* @author: hzq
* @date: 2022/5/27
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {IsMobileValidator.class})
public @interface IsPhone {
// 默认错误消息
String message() default "请输入正确的座机号码";
// 分组
Class<?>[] groups() default {};
// 负载
Class<? extends Payload>[] payload() default {};
}
package com.zkj.zw.common.core.validate;
import cn.hutool.core.util.StrUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* 手机号码检验
*
* @author: hzq
* @date: 2022/4/27
*/
public class IsPhoneValidator implements ConstraintValidator<IsPhone, String> {
// ^ 匹配输入字符串开始的位置
// \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
// $ 匹配输入字符串结尾的位置
private static final Pattern ch = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");//带区号
private static final Pattern hx = Pattern.compile("^[1-9][0-9]{5,8}$");//不带区号
@Override
public void initialize(IsPhone constraintAnnotation) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StrUtil.isBlank(value)) return true;
return isWithAreaCode(value) || isNotAreaCode(value);
}
public static boolean isWithAreaCode(String str) throws PatternSyntaxException {
return ch.matcher(str).matches();
}
public static boolean isNotAreaCode(String str) throws PatternSyntaxException {
return hx.matcher(str).matches();
}
}
package com.zkj.zw.common.core.validate;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
/**
* @author: hzq
* @date: 2022/5/27
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {IsBirthdayValidator.class})
public @interface IsBirthday {
// 默认错误消息
String message() default "请输入正确的出生日期";
// 分组
Class<?>[] groups() default {};
// 负载
Class<? extends Payload>[] payload() default {};
}
package com.zkj.zw.common.core.validate;
import cn.hutool.core.util.StrUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* 生日校验
*
* @author: hzq
* @date: 2022/4/27
*/
public class IsBirthdayValidator implements ConstraintValidator<IsBirthday, String> {
// ^ 匹配输入字符串开始的位置
// \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
// $ 匹配输入字符串结尾的位置
private static final Pattern br = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");
@Override
public void initialize(IsBirthday constraintAnnotation) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StrUtil.isBlank(value)) return true;
return isBirthday(value);
}
public static boolean isBirthday(String str) throws PatternSyntaxException {
boolean matches = br.matcher(str).matches();
if (!matches) return false;
try {
LocalDate birthday = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
return str.equals(birthday.toString());
} catch (Exception e) {
// ignored
// 解析失败
}
return false;
}
}