自定义注解-手机号验证注解

注解

java 复制代码
package com.XX.assess.annotation;

import com.XX.assess.util.MobileValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

/**
 * 手机号校验注解
 * @author super
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Documented
// 校验规则
@Constraint(validatedBy = MobileValidator.class)
public @interface Mobile {

    boolean required() default true;

    //信息,抛出的是BindException,前端页面接收的话,我们要进行异常的捕获
    String message() default "手机号码格式错误";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}

校验规则

java 复制代码
package com.XX.assess.util;

import cn.hutool.core.util.StrUtil;
import com.XX.assess.annotation.Mobile;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义校验规则
 * @author super
 */
public class MobileValidator implements ConstraintValidator<Mobile,String> {

    /**
     * 是否必填,默认不必填
     */
    private boolean required = false;

    /**
     * 初始化,获取是否必填
     * @param constraintAnnotation
     */
    @Override
    public void initialize(Mobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    /**
     * 判断是否校验手机号
     * @param value
     * @param context
     * @return
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 必填,则去校验手机号格式
        if (required){
            return ValidatorUtil.Mobile(value);
        }else {
            // 如果不必填,手机号为空,返回ture
            if (StrUtil.isEmpty(value)){
                return true;
            }else {
                // 否则校验手机号格式
                return ValidatorUtil.Mobile(value);
            }
        }
    }
}

校验方法

java 复制代码
package com.XX.assess.util;

import cn.hutool.core.util.StrUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 校验工具类
 */
public class ValidatorUtil {

    private static final Pattern mobile_pattern = Pattern.compile("[1]([3-9])[0-9]{9}$");

    public static boolean Mobile(String mobile) {
        if (StrUtil.isEmpty(mobile)){
            return false;
        }
        Matcher matcher = mobile_pattern.matcher(mobile);
        return matcher.matches();
    }
}
相关推荐
彭于晏6891 分钟前
Android广播
android·java·开发语言
程序员-珍19 分钟前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
弱冠少年27 分钟前
websockets库使用(基于Python)
开发语言·python·numpy
长天一色28 分钟前
C语言日志类库 zlog 使用指南(第五章 配置文件)
c语言·开发语言
一般清意味……39 分钟前
快速上手C语言【上】(非常详细!!!)
c语言·开发语言
卑微求AC40 分钟前
(C语言贪吃蛇)16.贪吃蛇食物位置随机(完结撒花)
linux·c语言·开发语言·嵌入式·c语言贪吃蛇
2401_857297911 小时前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
技术无疆1 小时前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
福大大架构师每日一题1 小时前
23.1 k8s监控中标签relabel的应用和原理
java·容器·kubernetes
金灰1 小时前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5