自封装验证手机号码、邮箱格式、身份证号的工具

如果在开发过程中,不停地写重复的验证代码是很让人头痛的也不利于后期的代码维护,为了减少代码的可维护性,提高代码的复用性。编写了一个验证用户信息的工具类。



import org.springframework.stereotype.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class RegexValidateUtil {
    /**
     * 邮箱验证
     * @author: 陈先生
     * @date 2021/4/27 10:21
     */
    public  boolean checkEmail(String email){
        boolean flag = false;
        try{
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        }catch(Exception e){
            flag = false;
        }
        return flag;
    }
    /**
     * 验证手机号码
     * @param phone
     * @return
     */
    public  boolean checkMobilePhone(String phone){
        boolean flag = false;
        try{
            Pattern regex = Pattern.compile("^1(3[0-9]|5[0-3,5-9]|7[1-3,5-8]|8[0-9])\\d{8}$");
            Matcher matcher = regex.matcher(phone);
            flag = matcher.matches();
        }catch(Exception e){
            flag = false;
        }
        return flag;
    }

    /**
     * 身份证件号校验
     * @author: 陈先生
     * @date 2021/4/27 10:20
     */
    public  boolean isIDNumber(String IDNumber) {
            if (IDNumber == null || "".equals(IDNumber)) {
                return false;
            }
            // 定义判别用户身份证号的正则表达式(15位或者18位,最后一位可以为字母)
            String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
                    "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
            //假设18位身份证号码:41000119910101123X  410001 19910101 123X
            //^开头
            //[1-9] 第一位1-9中的一个      4
            //\\d{5} 五位数字           10001(前六位省市县地区)
            //(18|19|20)                19(现阶段可能取值范围18xx-20xx年)
            //\\d{2}                    91(年份)
            //((0[1-9])|(10|11|12))     01(月份)
            //(([0-2][1-9])|10|20|30|31)01(日期)
            //\\d{3} 三位数字            123(第十七位奇数代表男,偶数代表女)
            //[0-9Xx] 0123456789Xx其中的一个 X(第十八位为校验值)
            //$结尾

            //假设15位身份证号码:410001910101123  410001 910101 123
            //^开头
            //[1-9] 第一位1-9中的一个      4
            //\\d{5} 五位数字           10001(前六位省市县地区)
            //\\d{2}                    91(年份)
            //((0[1-9])|(10|11|12))     01(月份)
            //(([0-2][1-9])|10|20|30|31)01(日期)
            //\\d{3} 三位数字            123(第十五位奇数代表男,偶数代表女),15位身份证不含X
            //$结尾


            boolean matches = IDNumber.matches(regularExpression);

            //判断第18位校验值
            if (matches) {

                if (IDNumber.length() == 18) {
                    try {
                        char[] charArray = IDNumber.toCharArray();
                        //前十七位加权因子
                        int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                        //这是除以11后,可能产生的11位余数对应的验证码
                        String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                        int sum = 0;
                        for (int i = 0; i < idCardWi.length; i++) {
                            int current = Integer.parseInt(String.valueOf(charArray[i]));
                            int count = current * idCardWi[i];
                            sum += count;
                        }
                        char idCardLast = charArray[17];
                        int idCardMod = sum % 11;
                        if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                            return true;
                        } else {
                            return false;
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                    }
                }

            }
            return matches;
        }
}

测试使用

正确案例
@SpringBootTest
class DemoApplicationTests {

    @Test
    public static void main(String[] args) {
        String email = "text@123.com";
        boolean b = new RegexValidateUtil().checkEmail(email);
        if (!b) {
            throw new IllegalArgumentException("该邮箱格式填写不正确,请核对后重试");
        }else{
            System.out.println("测试邮箱格式成功");
        }
    }
}

在这里插入图片描述

失败案例
@SpringBootTest
class DemoApplicationTests {

    @Test
    public static void main(String[] args) {
        String email = "1231666161.com";
        boolean b = new RegexValidateUtil().checkEmail(email);
        if (!b) {
            throw new IllegalArgumentException("该邮箱格式填写不正确,请核对后重试");
        }else{
            System.out.println("测试邮箱格式成功");
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平平常常一般牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值