判断是第几代身份证(第一代15位, 第二代18位)

判断是第几代身份证(第一代15位, 第二代18位)

if (cardId.length() == 15 || cardId.length() == 18) {
if (!this.cardCodeVerifySimple(cardId)) {
error.put("cardId", "15位或18位身份证号码不正确");
} else {
if (cardId.length() == 18 && !this.cardCodeVerify(cardId)) {
error.put("cardId", "18位身份证号码不符合国家规范");
}
}
} else {
error.put("cardId", "身份证号码长度必须等于15或18位");
}

正则校验身份证是否符合第一代第二代标准

private boolean cardCodeVerifySimple(String cardcode) {
//第一代身份证正则表达式(15位)
String isIDCard1 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
//第二代身份证正则表达式(18位)
String isIDCard2 ="^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[A-Z])$";

//验证身份证
if (cardcode.matches(isIDCard1) || cardcode.matches(isIDCard2)) {
return true;
}
return false;
}

验证第二代身份证是否符合国家规范

private boolean cardCodeVerify(String cardcode) {
int i = 0;
String r = "error";
String lastnumber = "";

i += Integer.parseInt(cardcode.substring(0, 1)) * 7;
i += Integer.parseInt(cardcode.substring(1, 2)) * 9;
i += Integer.parseInt(cardcode.substring(2, 3)) * 10;
i += Integer.parseInt(cardcode.substring(3, 4)) * 5;
i += Integer.parseInt(cardcode.substring(4, 5)) * 8;
i += Integer.parseInt(cardcode.substring(5, 6)) * 4;
i += Integer.parseInt(cardcode.substring(6, 7)) * 2;
i += Integer.parseInt(cardcode.substring(7, 8)) * 1;
i += Integer.parseInt(cardcode.substring(8, 9)) * 6;
i += Integer.parseInt(cardcode.substring(9, 10)) * 3;
i += Integer.parseInt(cardcode.substring(10,11)) * 7;
i += Integer.parseInt(cardcode.substring(11,12)) * 9;
i += Integer.parseInt(cardcode.substring(12,13)) * 10;
i += Integer.parseInt(cardcode.substring(13,14)) * 5;
i += Integer.parseInt(cardcode.substring(14,15)) * 8;
i += Integer.parseInt(cardcode.substring(15,16)) * 4;
i += Integer.parseInt(cardcode.substring(16,17)) * 2;
i = i % 11;
lastnumber =cardcode.substring(17,18);
if (i == 0) {
r = "1";
}
if (i == 1) {
r = "0";
}
if (i == 2) {
r = "x";
}
if (i == 3) {
r = "9";
}
if (i == 4) {
r = "8";
}
if (i == 5) {
r = "7";
}
if (i == 6) {
r = "6";
}
if (i == 7) {
r = "5";
}
if (i == 8) {
r = "4";
}
if (i == 9) {
r = "3";
}
if (i == 10) {
r = "2";
}
if (r.equals(lastnumber.toLowerCase())) {
return true;
}
return false;
}

 

原文链接:,转发请注明来源!

Leave a Reply

You must be logged in to post a comment.

  • 1 Responses to “判断是第几代身份证(第一代15位, 第二代18位)”
    • zlmind

      private static final int[] ID_INT_ARR_0 = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
      private static final int[] ID_INT_ARR_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      private static final int[] ID_INT_ARR_2 = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };

      /**
      * 校验身份证号
      * @param cardId
      * @return
      */
      public static boolean checkIdCard(String cardId) {
      if(StringUtil.isEmpty(cardId) || cardId.length() != 18){
      return false;
      }
      cardId = cardId.toUpperCase();

      int sum = 0;
      for (int i = 0; i < ID_INT_ARR_0.length; i++) {
      // 2.将这17位数字和系数相乘的结果相加。
      sum += Character.digit(cardId.charAt(i), 10) * ID_INT_ARR_0[i];
      }
      // 3.用加出来和除以11,看余数是多少?
      int mod = sum % 11;
      // 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2。
      String matchDigit = "";
      for (int i = 0; i < ID_INT_ARR_1.length; i++) {
      int j = ID_INT_ARR_1[i];
      if (j == mod) {
      matchDigit = String.valueOf(ID_INT_ARR_2[i]);
      if (ID_INT_ARR_2[i] > 57) {
      matchDigit = String.valueOf((char) ID_INT_ARR_2[i]);
      }
      }
      }
      return matchDigit.equals(cardId.substring(cardId.length() - 1));

      }

      私信