public static boolean ckeckIDNoFormat(String IDNo) {
        if(null == IDNo || "".equals(IDNo.trim())) {
            return false;
        }
        String regx = "(^[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}$)";
        Pattern p = Pattern.compile(regx);
        Matcher m = p.matcher(IDNo);
        boolean matches = false;
        matches = m.matches();
        
        if (matches) {

            if (IDNo.length() == 18) {
                try {
                    char[] charArray = IDNo.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 {
                        System.out.println("身份证"+IDNo+"最后一位:" + String.valueOf(idCardLast).toUpperCase() + 
                                "错误,正确的应该是:" + idCardY[idCardMod].toUpperCase());
                        return false;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("异常:" + IDNo);
                    return false;
                }
            }
        }
        return matches;
    }

身份证正则校验&加权验证最后一位

相关文章:

  • 2021-12-22
  • 2021-05-24
  • 2021-12-23
  • 2021-12-23
  • 2021-12-23
  • 2021-12-23
  • 2021-05-16
  • 2021-06-01
猜你喜欢
  • 2021-07-19
  • 2022-01-08
  • 2021-12-17
  • 2021-11-27
  • 2021-10-06
相关资源
相似解决方案