工具类,判断手机号,身份证号,银行卡号

import java.util.regex.Matcher;
import java.util.regex.Pattern;
//工具类,判断手机号,身份证号,银行卡号
public class ValidateUtil {
    public ValidateUtil() {
    }

    public static boolean isMobileNum(String mobile) {
        Pattern pattern = Pattern.compile("^((1(3|4|5|6|7|8|9)[0-9]))\\d{8}$");
        Matcher matcher = pattern.matcher(mobile);
        return matcher.matches();
    }

    public static boolean isUserIDCard(String idcard) {
        Pattern pattern = Pattern.compile("^(\\d{6})(19|20)(\\d{2})(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])(\\d{3})(\\d|X|x)?$");
        Matcher matcher = pattern.matcher(idcard);
        if (idcard.length() == 18 && matcher.matches()) {
            String[] idSplit = idcard.split("");
            if ("X".equals(idSplit[17])) {
                idSplit[17] = "x";
            }

            int[] factor = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
            String[] parity = new String[]{"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"};
            int sum = 0;
            int ai = false;
            int wi = false;

            for(int i = 0; i < 17; ++i) {
                int ai = Integer.valueOf(idSplit[i]);
                int wi = factor[i];
                sum += ai * wi;
            }

            return idSplit[17].equals(parity[sum % 11]);
        } else {
            return false;
        }
    }

    public static boolean checkBankCard(String bankCard) {
        if (bankCard.length() >= 15 && bankCard.length() <= 19) {
            char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
            if (bit == 'N') {
                return false;
            } else {
                return bankCard.charAt(bankCard.length() - 1) == bit;
            }
        } else {
            return false;
        }
    }

    public static char getBankCardCheckCode(String nonCheckCodeBankCard) {
        if (nonCheckCodeBankCard != null && nonCheckCodeBankCard.trim().length() != 0 && nonCheckCodeBankCard.matches("\\d+")) {
            char[] chs = nonCheckCodeBankCard.trim().toCharArray();
            int luhmSum = 0;
            int i = chs.length - 1;

            for(int j = 0; i >= 0; ++j) {
                int k = chs[i] - 48;
                if (j % 2 == 0) {
                    k *= 2;
                    k = k / 10 + k % 10;
                }

                luhmSum += k;
                --i;
            }

            return luhmSum % 10 == 0 ? '0' : (char)(10 - luhmSum % 10 + 48);
        } else {
            return 'N';
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值