IOS ID card verification detailed introduction and sample code

  • 2021-09-11 21:35:34
  • OfStack

IOS ID card verification

Basic knowledge of ID card:

ID card is the national identity number, and there is a regular number. Here, the ID card verification rules are introduced in detail. The project often needs to check the ID card. We first understand some basic knowledge and then analyze the code

Resident ID card number, according to the provisions of citizenship number in National Standard GB 11643-1999 of the People's Republic of China, citizenship number is a characteristic combination code, which consists of 107-digit body code and 1-digit check code. From left to right, the arrangement order is: 6-digit address code, 8-digit birth date code, 3-digit sequence code and 1-digit check code. Resident ID card is a valid certificate legally issued by the state to prove citizens' personal identity.

Structure and form

1. Structure of numbers

The citizenship number is a characteristic combination code, which consists of a 107-bit digital ontology code and a 1-bit check code. From left to right, the arrangement order is: 6-digit address code, 8-digit birth date code, 3-digit sequence code and 1-digit check code.

2. Address code

The administrative division code indicating the county (city, flag and district) where the permanent residence of the coding object is located shall be implemented according to GB/T2260.

3. Date of Birth Code

Indicates the year, month and day of the birth of the coded object, which is implemented according to GB/T7408. There is no separator between the year, month and day codes.

4. Sequence code

Indicates the sequence number assigned to people born in the same year, the same month and the same day within the area identified by the same address code. The odd number of the sequence code is assigned to men and the even number is assigned to women.

5. Check code

The check code calculated according to the previous 107-bit digital code and ISO7064: 1983. MOD11-2 check code.

Address code

North China: Beijing 110,000, Tianjin 120,000, Hebei 130,000, Shanxi 140,000, Inner Mongolia Autonomous Region 150,000,

Northeast China: 210,000 in Liaoning Province, 220,000 in Jilin Province and 230,000 in Heilongjiang Province.

East China: 310,000 in Shanghai, 320,000 in Jiangsu, 330,000 in Zhejiang, 340,000 in Anhui, 350,000 in Fujian, 360,000 in Jiangxi and 370,000 in Shandong.

Central China: 410,000 in Henan Province, 420,000 in Hubei Province and 430,000 in Hunan Province.

South China: 440,000 in Guangdong Province, 450,000 in Guangxi Zhuang Autonomous Region and 460,000 in Hainan Province.

Southwest China: 500,000 in Chongqing, 510,000 in Sichuan Province, 520,000 in Guizhou Province, 530,000 in Yunnan Province and 540,000 in Tibet Autonomous Region.

Northwest China: 610,000 in Shaanxi Province, 620,000 in Gansu Province, 630,000 in Qinghai Province, 640,000 in Ningxia Hui Autonomous Region and 650,000 in Xinjiang Uygur Autonomous Region.

Special Areas: Taiwan (886) 710,000, Hong Kong Special Administrative Region (852) 810,000, Macao Special Administrative Region (853) 820,000

The digital coding rules of the address code in the Chinese mainland resident ID card number are as follows:

The 1st and 2nd places refer to provinces (autonomous regions, municipalities directly under the Central Government and special administrative regions).

The 3rd. 4th digit indicates the city (the summary code of the municipal districts and counties under the prefecture-level cities, autonomous prefectures, leagues and municipalities directly under the Central Government). Among them, 01-20 and 51-70 represent provinces and municipalities directly under the Central Government; 21-50 denotes regions (autonomous prefectures, leagues).

The 5th and 6th digits indicate counties (municipal districts, county-level cities and flags). 01-18 means county-level cities under the jurisdiction of municipal districts or regions (autonomous prefectures and leagues); 21-80 denotes counties (flags); 81-99 means county-level cities directly under the provincial government.

Birth date code

(The 7th digit to the 104th digit of the ID card number) indicates the year, month and day of the birth of the coded object, in which the year is represented by 4 digits, and there is no separator between the year, month and day. For example, May 11, 1981 is expressed as 19810511.

Sequential code

(ID card number 105 to 107 digits) within the area identified by the address code, the sequence number assigned to the person born in the same year, month and day. Among them, the 107th odd number is given to men and the even number is given to women

Check code

As the check code of the tail number, It is calculated by the number compilation unit according to the formula of Unification 1, If someone's tail number is 0-9, X will not appear, but if the tail number is 10, X must be used instead, because if 10 is used as the tail number, the person's ID card will become 19 digits, and the 19-digit number violates the national standard, and the computer application system in China does not recognize the 19-digit ID card number. X is the Roman numeral 10. Replacing 10 with X can ensure that citizens' ID cards meet the national standards.

Calculation method of ID card check code

1. Multiply the previous 17 digits of ID number by different coefficients. The coefficients from 1st to 107th are 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2, respectively.
2. Add these 17 digits to the result of multiplication of coefficients.
3. What is the remainder by adding and dividing by 11?
4. The remainder may only have 11 numbers: 0-1-2-3-4-5-6-7-8-9-10. The numbers of the last ID cards corresponding to them are 1-0-X-9-8-7-6-5-4-3-2. (That is, the remainder 0 corresponds to 1, the remainder 1 corresponds to 0, and the remainder 2 corresponds to X...)
5. If the remainder is 3, 9 will appear on the 18th digit of the ID card. If the corresponding number is 2, the last one digit number of the ID card is the Roman numeral x.

For example, the ID number of a man is "53010219200508011x". Let's see if this ID card is a legal ID card.
First we get that the product of the first 17 bits "(5*7) + (3*9) + (0*10) + (1*5) + (0*8) + (2*4) + (1*2) + (9*1) + (2*6) + (0*3) + (0*7) + (5*9) + (0*10) + (8*5) + (0*8) + (1*4) + (1*2)" is 189. Finally, through the corresponding rules, we can know that the check code corresponding to remainder 2 is X. Therefore, it can be judged that this is a correct ID number.

With the above basic knowledge points, the following specific see 1 under the implementation code

18-digit ID card verification


-(BOOL)checkUserIDCard:(NSString *)userID 
{ 
  // The length is not 18 All of them are excluded  
  if (userID.length!=18) { 
    return NO; 
  } 
   
  // Check format  
  NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$"; 
  NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2]; 
  BOOL flag = [identityCardPredicate evaluateWithObject:userID]; 
   
  if (!flag) { 
    return flag;  // Format error  
  }else { 
    // Correct format depends on judging whether it is legal or not  
    // Will be preceded 17 Bit weighting factors are stored in an array  
    NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"]; 
     
    // This is divided by 11 After, may produce 11 Bit remainder, verification code, also saved as array  
    NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"]; 
     
    // Before saving 17 The sum of each bit after weighting factor  
    NSInteger idCardWiSum = 0; 
    for(int i = 0;i < 17;i++){ 
      NSInteger subStrIndex = [[userID substringWithRange:NSMakeRange(i, 1)] integerValue]; 
      NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue]; 
      idCardWiSum+= subStrIndex * idCardWiIndex; 
    } 
     
    // Calculate the position of the array where the check code is located  
    NSInteger idCardMod=idCardWiSum%11; 
     
    // Get the last 1 Digit ID number  
    NSString * idCardLast= [userID substringWithRange:NSMakeRange(17, 1)]; 
     
    // If equal to 2 The check code is 10 , ID number last 1 Bit should be X 
    if(idCardMod==2){ 
      if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]){ 
        return YES; 
      }else{ 
        return NO; 
      } 
    }else{ 
      // Use the calculated verification code and the final 1 Bit ID number matches, if 1 To, explain that it is passed, otherwise it is an invalid ID number  
      if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]]){ 
        return YES; 
      }else{ 
        return NO; 
      } 
    } 
  } 
} 

15-digit and 18-digit ID card verification


// Regular matching of user ID number 15 Or 18 Bit  
-(BOOL)validateIDCardNumber:(NSString *)value { 
 
  value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
  NSInteger length =0; 
  if (!value) { 
    return NO; 
  }else { 
    length = value.length; 
    // Not satisfied 15 Bit sum 18 Bit, that is, the ID card is wrong  
    if (length !=15 && length !=18) { 
      return NO; 
    } 
  } 
  //  Province code  
  NSArray *areasArray = @[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"]; 
   
  //  Detect Province Identity Administrative Region Code  
  NSString *valueStart2 = [value substringToIndex:2]; 
  BOOL areaFlag =NO; // Identify whether the province code is correct  
  for (NSString *areaCode in areasArray) { 
    if ([areaCode isEqualToString:valueStart2]) { 
      areaFlag =YES; 
      break; 
    } 
  } 
   
  if (!areaFlag) { 
    return NO; 
  } 
   
  NSRegularExpression *regularExpression; 
  NSUInteger numberofMatch; 
   
  int year =0; 
  // Divide into 15 Bit, 18 Check the ID card  
  switch (length) { 
    case 15: 
      // Get the number corresponding to the year  
      year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900; 
     
      if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) { 
        // Create a regular expression  NSRegularExpressionCaseInsensitive Case-insensitive pattern  
        regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$" 
          options:NSRegularExpressionCaseInsensitive error:nil];// Test the legality of the date of birth  
      }else { 
        regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$" 
        options:NSRegularExpressionCaseInsensitive error:nil];// Test the legality of the date of birth  
      } 
      // Matching Strings Using Regular Expressions  NSMatchingReportProgress: Called after finding the longest matching string block Callback  
      numberofMatch = [regularExpression numberOfMatchesInString:value 
                                options:NSMatchingReportProgress 
                                 range:NSMakeRange(0, value.length)]; 
       
      if(numberofMatch >0) { 
        return YES; 
      }else { 
        return NO; 
      } 
    case 18: 
      year = [value substringWithRange:NSMakeRange(6,4)].intValue; 
      if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) { 
        regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$" options:NSRegularExpressionCaseInsensitive error:nil];// Test the legality of the date of birth  
      }else { 
        regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$" options:NSRegularExpressionCaseInsensitive error:nil];// Test the legality of the date of birth  
      } 
      numberofMatch = [regularExpression numberOfMatchesInString:value 
                                options:NSMatchingReportProgress 
                                 range:NSMakeRange(0, value.length)]; 
       
       
      if(numberofMatch >0) { 
        //1 Calculation method of check code   ID card number 17 The digits are multiplied by different coefficients. From the first 1 Bit to number 107 The coefficients of bits are: 7 - 9 - 10 - 5 - 8 - 4 - 2 - 1 - 6 - 3 - 7 - 9 - 10 - 5 - 8 - 4 - 2 . Put this 17 The result of multiplication of bits and coefficients is added.  
         
        int S = [value substringWithRange:NSMakeRange(0,1)].intValue*7 + [value substringWithRange:NSMakeRange(10,1)].intValue *7 + [value substringWithRange:NSMakeRange(1,1)].intValue*9 + [value substringWithRange:NSMakeRange(11,1)].intValue *9 + [value substringWithRange:NSMakeRange(2,1)].intValue*10 + [value substringWithRange:NSMakeRange(12,1)].intValue *10 + [value substringWithRange:NSMakeRange(3,1)].intValue*5 + [value substringWithRange:NSMakeRange(13,1)].intValue *5 + [value substringWithRange:NSMakeRange(4,1)].intValue*8 + [value substringWithRange:NSMakeRange(14,1)].intValue *8 + [value substringWithRange:NSMakeRange(5,1)].intValue*4 + [value substringWithRange:NSMakeRange(15,1)].intValue *4 + [value substringWithRange:NSMakeRange(6,1)].intValue*2 + [value substringWithRange:NSMakeRange(16,1)].intValue *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3; 
         
        //2 Divide by the sum added 11 Let's see what the remainder is. The remainder can only be 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 This 11 Number  
        int Y = S %11; 
        NSString *M =@"F"; 
        NSString *JYM =@"10X98765432"; 
        M = [JYM substringWithRange:NSMakeRange(Y,1)];// 3 Getting the check bit  
        //4 : Testing ID Parity bit of  
        if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) { 
          return YES; 
        }else { 
          return NO; 
        } 
         
      }else { 
        return NO; 
      } 
    default: 
      return NO; 
  } 
} 

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


Related articles: