Java id card verification method example details

  • 2020-06-19 10:27:50
  • OfStack

Java ID card verification method example details

Id number verification 1, the structure of the number The citizenship number is a feature combination code, consisting of 107 digit body code and 1 digit check code. From left to right: 6-digit address code,

8-digit birth date code, 3-digit sequence code and 1-digit check code. 2. Address code (first 6 digits)

The administrative division code of the county (city, flag, district) where the coded object's permanent residence is located shall be implemented in accordance with the provisions of GB/T2260. 3. Date of birth Code (bits 7 to 104)

Represents the year, month and day of the encoding object's birth, which is executed according to the provisions of GB/T7408, and there is no separator between the codes of year, month and day. 4. Sequence code (105th to 107th bits)

Refers to the serial number of people born in the same year, the same month and the same day within the area identified by the same 1 address code. The odd number of the sequential code is assigned to males and the even number to females. 5. Check code (108th digit)

(1) Weighted summation formula S = Sum(Ai * Wi), i = 0... 16. First, the weight sum of the first 17 digits Ai: represents the number value of the ID card number at position i: represents the weighted factor Wi: 7 9 10 5 8 4 2 16 3 7 9 10 5 8 4

2 (2) Calculate the module Y = mod(S, 11) (3) Get the corresponding check code Y: 0 1 2 3 4 5 6 7 8 9 10 check code: 10 X 9 8 7 6 4 3 2 through the module

Example code:


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IDCard {
  /***********************************  Id card Verification begins  ****************************************/
  /**
   *  Id card Number Verification  1 Number structure   The citizenship number is the characteristic combination code, by 107 Bit digital body code and 1 Bit check code composition. The order from left to right is: 6 Bit number address code, 
   * 8 The digit birth date code, 3 Digit sequence code and 1 Bit digit check code.  2 , address code ( before 6 Digits) 
   *  Represents the county where the encoding object is located ( City, flag, district ) The administrative division code, press GB/T2260 Implementation of the regulations.  3 , Date of birth (No 7 Places to 104 A) 
   *  Represents the year, month, and day of the encoding object's birth GB/T7408 The year, month and day codes are executed without a delimiter.  4 , sequence code (no 105 Places to 107 A) 
   *  Said in the same 1 Within the area indicated by the address code, the serial number of a person born in the same year, the same month and the same day,   Odd Numbers of sequential codes are assigned to men, and even Numbers to women.  5 , check code (no 108 Digits) 
   *  ( 1 ) 107 The weighted sum formula of bit-digit ontology code  S = Sum(Ai * Wi), i = 0, ... , 16  , the former first 17 The sum of the weights of the digits 
   * Ai: According to the first i The numeric value of the ID card number on the location  Wi: According to the first i The weighted factor of position  Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4
   * 2  ( 2 ) mode  Y = mod(S, 11)  ( 3 ) get the corresponding check code through the module  Y: 0 1 2 3 4 5 6 7 8 9 10  Check code : 1 0
   * X 9 8 7 6 5 4 3 2
   */

  /**
   *  Function: Valid verification of ID card 
   * 
   * @param IDStr
   *       Id number 
   * @return  Valid: Return ""  Invalid: Return String information 
   */
  public static String IDCardValidate(String IDStr) {
    String errorInfo = "";//  Log error messages 
    String[] ValCodeArr = { "1", "0", "x", "9", "8", "7", "6", "5", "4",
        "3", "2" };
    String[] Wi = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7",
        "9", "10", "5", "8", "4", "2" };
    String Ai = "";
    // ================  Length of number  15 or 18 position  ================
    if (IDStr.length() != 15 && IDStr.length() != 18) {
      errorInfo = " Id card number length should be 15 or 18 position ";
      return errorInfo;
    }
    // =======================(end)========================

    // ================  digital   Everything except the last is a number  ================
    if (IDStr.length() == 18) {
      Ai = IDStr.substring(0, 17);
    } else if (IDStr.length() == 15) {
      Ai = IDStr.substring(0, 6) + "19" + IDStr.substring(6, 15);
    }
    if (isNumeric(Ai) == false) {
      errorInfo = " Id card 15 All digit Numbers should be digits  ; 18 Bit number divided by last 1 Outside the digit, it should be a number. ";
      return errorInfo;
    }
    // =======================(end)========================

    // ================  Whether the date of birth is valid  ================
    String strYear = Ai.substring(6, 10);//  year 
    String strMonth = Ai.substring(10, 12);//  in 
    String strDay = Ai.substring(12, 14);//  in 
    if (isDate(strYear + "-" + strMonth + "-" + strDay) == false) {
      errorInfo = " Id card birthday invalid. ";
      return errorInfo;
    }
    GregorianCalendar gc = new GregorianCalendar();
    SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
    try {
      if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150
          || (gc.getTime().getTime() - s.parse(
              strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
        errorInfo = " Id date of birth is not valid. ";
        return errorInfo;
      }
    } catch (NumberFormatException e) {
      e.printStackTrace();
    } catch (java.text.ParseException e) {
      e.printStackTrace();
    }
    if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {
      errorInfo = " Id card month invalid ";
      return errorInfo;
    }
    if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {
      errorInfo = " Id card date invalid ";
      return errorInfo;
    }
    // =====================(end)=====================

    // ================  Area codes are valid  ================
    Hashtable<?, ?> h = GetAreaCode();
    if (h.get(Ai.substring(0, 2)) == null) {
      errorInfo = " Id area code error. ";
      return errorInfo;
    }
    // ==============================================

    // ================  Determine the final 1 The value of a  ================
    int TotalmulAiWi = 0;
    for (int i = 0; i < 17; i++) {
      TotalmulAiWi = TotalmulAiWi
          + Integer.parseInt(String.valueOf(Ai.charAt(i)))
          * Integer.parseInt(Wi[i]);
    }
    int modValue = TotalmulAiWi % 11;
    String strVerifyCode = ValCodeArr[modValue];
    Ai = Ai + strVerifyCode;

    if (IDStr.length() == 18) {
      if (Ai.equals(IDStr) == false) {
        errorInfo = " Id invalid, not a legal ID number ";
        return errorInfo;
      }
    } else {
      return "YES";
    }
    // =====================(end)=====================
    return "YES";
  }

  /**
   *  Function: Set locale code 
   * 
   * @return Hashtable  object 
   */
  private static Hashtable<String, String> GetAreaCode() {
    Hashtable<String, String> hashtable = new Hashtable<String, String>();
    hashtable.put("11", " Beijing ");
    hashtable.put("12", " tianjin ");
    hashtable.put("13", " hebei ");
    hashtable.put("14", " shanxi ");
    hashtable.put("15", " Inner Mongolia ");
    hashtable.put("21", " liaoning ");
    hashtable.put("22", " Ji Lin ");
    hashtable.put("23", " heilongjiang ");
    hashtable.put("31", " Shanghai ");
    hashtable.put("32", " jiangsu ");
    hashtable.put("33", " zhejiang ");
    hashtable.put("34", " anhui ");
    hashtable.put("35", " fujian ");
    hashtable.put("36", " jiangxi ");
    hashtable.put("37", " shandong ");
    hashtable.put("41", " henan ");
    hashtable.put("42", " hubei ");
    hashtable.put("43", " hunan ");
    hashtable.put("44", " guangdong ");
    hashtable.put("45", " guangxi ");
    hashtable.put("46", " hainan ");
    hashtable.put("50", " chongqing ");
    hashtable.put("51", "4 sichuan ");
    hashtable.put("52", " guizhou ");
    hashtable.put("53", " yunnan ");
    hashtable.put("54", " Tibet ");
    hashtable.put("61", " shaanxi ");
    hashtable.put("62", " gansu ");
    hashtable.put("63", " qinghai ");
    hashtable.put("64", " ningxia ");
    hashtable.put("65", " xinjiang ");
    hashtable.put("71", " Taiwan ");
    hashtable.put("81", " Hong Kong ");
    hashtable.put("82", " Macau ");
    hashtable.put("91", " foreign ");
    return hashtable;
  }

  /**
   *  Function: Determines whether a string is a number 
   * 
   * @param str
   * @return
   */
  private static boolean isNumeric(String str) {
    Pattern pattern = Pattern.compile("[0-9]*");
    Matcher isNum = pattern.matcher(str);
    if (isNum.matches()) {
      return true;
    } else {
      return false;
    }
  }

  /**
   *  Function: Determines whether a string is in date format 
   * 
   * @param str
   * @return
   */
  public static boolean isDate(String strDate) {
    Pattern pattern = Pattern
        .compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?{1}quot;);
    Matcher m = pattern.matcher(strDate);
    if (m.matches()) {
      return true;
    } else {
      return false;
    }
  }

  /**
   * @param args
   * @throws ParseException
   */
  @SuppressWarnings("static-access")
  public static void main(String[] args) {
    // String IDCardNum="210102820826411";
    // String IDCardNum="210102198208264114";
    while (true) {
      Scanner input = new Scanner(System.in);
      String n = input.nextLine();
      if (n.equals("N") || n.equals("n")) {
        break;
      }
      String IDCardNum = input.nextLine();

      IDCard cc = new IDCard();
      System.out.println(cc.IDCardValidate(IDCardNum));
    }
    // System.out.println(cc.isDate("1996-02-29"));
  }
  /***********************************  End of ID card verification  ****************************************/

}

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: