IOS ID card verification
Basics of ID card:
The ID card is the identity number of the national, and the numbering has certain rules. Here we introduce the ID card verification rules in more detail. The ID card is often required to be checked in the project. We first understand some basic knowledge and then analyze the code.
The resident ID number is based on the provisions on citizen ID numbers in the National Standard GB 11643-1999 of the People's *. The citizen ID number is a characteristic combination code, consisting of a seventeen-digit digital ontology code and a one-digit verification code. The arrangement order is from left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit verification code. The resident ID card is a valid document statutory national statutory proof of the personal identity of a citizen.
Structure and form
1. The structure of the number
The citizenship identity number is a feature combination code, consisting of a seventeen-digit ontology code and a one-digit verification code. The arrangement order is from left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit verification code.
2. Address code
The administrative division code of the county (city, banner, district) where the permanent residence of the coding object is located shall be implemented in accordance with the provisions of GB/T2260.
3. Date of birth code
It indicates the year, month and day of the birth of the encoded object. It shall be executed according to the provisions of GB/T7408. There is no separator between the year, month and day codes.
4. Sequence code
It indicates that within the area identified by the same address code, the sequence number assigned to people born in the same year, month, and day. The odd number of the sequence code is assigned to men and even numbers are assigned to women.
5. Verification code
According to the first seventeen digit numerical codes, the verification code calculated according to the ISO7064:1983.MOD11-2 verification code.
Address code
North China: Beijing | 110,000, Tianjin | 120,000, Hebei Province | 130,000, Shanxi Province | 140,000, Inner * Autonomous Region | 150,000,
Northeastern region: Liaoning Province | 210,000, Jilin Province | 220,000, Heilongjiang Province | 230,000,
East China: Shanghai | 310000, Jiangsu Province | 320000, Zhejiang Province | 330000, Anhui Province | 340000, Fujian Province | 350000, Jiangxi Province | 360000, Shandong Province | 370000,
Central China Region: Henan Province | 410000, Hubei Province | 420000, Hunan Province | 430000,
South China: Guangdong Province | 440,000, Guangxi Zhuang Autonomous Region | 450,000, Hainan Province | 460,000,
Southwest region: Chongqing City | 500000, Sichuan Province | 510000, Guizhou Province | 520000, Yunnan Province | 530000, Tibet Autonomous Region | 540000,
Northwest region: Shaanxi Province | 610000, Gansu Province | 620000, Qinghai Province | 630000, Ningxia Hui Autonomous Region | 640000, * Uygur Autonomous Region | 650000,
Special Region: * (886) | 710,000, * Special Administrative Region (852) | 810,000, Macao Special Administrative Region (853) | 820,000
The numerical encoding rules for address codes in the ID number of mainland Chinese residents are:
The first and second ranks represent provinces (autonomous regions, municipalities directly under the central government, special administrative regions).
The third and fourth digits represent the city (summary codes of prefecture-level cities, autonomous prefectures, alliances and municipalities directly under the central government). Among them, 01-20, 51-70 represents a provincial municipality; 21-50 represents a region (autonomous prefecture, alliance).
The fifth and sixth digits represent counties (city districts, county-level cities, banners). 01-18 means that the city's district or region (autonomous prefectures, alliances) has a county-level city; 21-80 means the county (banner); 81-99 means the provincial-level city.
Birthday code
(The seventh to the fourteenth digits of the ID card number) represents the year, month and day of the birth of the encoded object. The year is represented by four digits, and there is no separator between the year, month and day. For example: On May 11, 1981, it was represented by 19810511.
Sequence code
(15th to 17th digits of the ID number) The sequence number is assigned to the person born in the same year, month and day within the area identified by the address code. The seventeenth odd number is given to men and even number is given to women
Verification code
The verification code for the last number is calculated by the number compilation unit according to a unified formula. If the last number of someone is 0-9, X will not appear, but if the last number is 10, then X must be used instead, because if 10 is used as the last number, then the person's ID card will become 19 digits, and the 19 digit number violates national standards, and China's computer application system does not recognize the 19 digit ID card number. Ⅹ is the 10 of the Roman numeral. Replacing 10 with X can ensure that the citizen's ID card meets national standards.
Calculation method of ID card verification code
1. Multiply the 17-digit number of the previous ID number by different coefficients. The coefficients from the first position to the seventeenth position are: 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2.
2. Add the result of multiplying these 17-digit numbers and coefficients.
3. Use the addition and divide by 11 to see what the remainder is?
4. The remainder may only have 11 numbers: 0-1-2-3-4-5-6-7-8-9-10. The number of the last ID card corresponding to it is 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. From the above, you know that if the remainder is 3, the 18th digit of the ID card will appear 9. If the corresponding number is 2, the last number of the ID card is the Roman numeral x.
For example: a man’s ID number is [53010219200508011x], let’s see if this ID card is a legal ID card.
First we get the sum of 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, and then divide 189 by 11 is 189/11=17----2, that is, the rest of the numbers are 2. Finally, through the corresponding rules, you can know that the verification code corresponding to remainder 2 is X. Therefore, it can be determined that this is the correct ID number.
With the above basic knowledge points, let’s take a look at the implementation code in detail below
18-digit ID card verification
-(BOOL)checkUserIDCard:(NSString *)userID { //Exclude any length of 18 if (!=18) { return NO; } //Calibration 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 { //The format is correct and it is legal to determine whether it is legal. //Save the first 17-bit weighting factors in the array NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"]; //This is the 11-bit remainder and verification code that may be generated after dividing by 11, and is also saved into an array NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"]; // Used to save the sum of the first 17 bits after the 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 location of the array where the verification code is located NSInteger idCardMod=idCardWiSum%11; //Get the last ID number NSString * idCardLast= [userID substringWithRange:NSMakeRange(17, 1)]; //If it is equal to 2, it means that the verification code is 10, and the last digit of the ID number should be X if(idCardMod==2){ if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]){ return YES; }else{ return NO; } }else{ //Match the calculated verification code with the last ID number. If it is consistent, it means that it is passed, otherwise it is an invalid ID number. if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]]){ return YES; }else{ return NO; } } } }
15-bit and 18-bit ID card verification
//Regularly match the user ID number 15 or 18 digits-(BOOL)validateIDCardNumber:(NSString *)value { value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSInteger length =0; if (!value) { return NO; }else { length = ; //Not satisfied with 15-bit and 18-bit, that is, the ID card error 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 the provincial identity administrative region code NSString *valueStart2 = [value substringToIndex:2]; BOOL areaFlag =NO; //Identify 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; //Split into 15-digit and 18-digit ID cards for verification 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 regular expression NSRegularExpressionCaseInsensitive: a 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 } //Use regular expressions to match strings NSMatchingReportProgress: Call the block callback after finding the longest matching string. numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, )]; 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, )]; if(numberofMatch >0) { //1: Calculation method of verification code: The 17-digit number of ID card numbers are multiplied by different coefficients respectively. The coefficients from the first position to the seventeenth position are: 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2. Add the result of multiplying these 17-digit numbers and coefficients. 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: Use the addition and divide by 11 to see what the remainder is? The remainder can only have 11 numbers: 0-1-2-3-4-5-6-7-8-9-10 int Y = S %11; NSString *M =@"F"; NSString *JYM =@"10X98765432"; M = [JYM substringWithRange:NSMakeRange(Y,1)];// 3: Get the check bit //4: Check the ID's check bit if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) { return YES; }else { return NO; } }else { return NO; } default: return NO; } }
Thank you for reading, I hope it can help you. Thank you for your support for this site!