Practical tips for iOS development-regular check of identity cards

Source: Internet
Author: User

Practical tips for iOS development-regular check of identity cards

Description

    • In the development of iOS project may need to use the identity card verification, generally in the development of a lot of people are direct Baidu to online swing related to the regular expression and check code, but the online crazy paste copy of the verification code itself may not be accurate, there may be risks, For example, from January 1, 2013 onwards to stop the use of 15-digit ID card, online identification of the general support of 15-digit number.
    • In the development process, similar processing, still need some screening ability, of course, also consider the actual situation of their own projects. This article posted a recent project to use the ID check code to facilitate the people in need of "cautious" access.

First, the rules

The following is the iOS ID check rules, for the 6th is debatable, according to the truth that the first two digits of the year of birth is 20 should also be reasonable. If you want to verify the applicant needs to be over 18 years of age, need to be checked, should not be placed in the ID card check inside.
    1. The length must be 18 bits, the first 17 digits must be a number, and the 18th bit can be a number or X (the check is case insensitive)
    2. The first two bits must be one of the following 35 scenarios: 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
    3. 7th to 14th place of birth date. The 7th to 10th place is the year of birth; 11 to 12 digits represents the month, the range is 01~12;13 to 14 is a valid date, such as the month is 04, the range should be 01~30
    4. The 17th digit represents the gender, must be 0 or 1,0 to indicate the female, 1 indicates the male
    5. The 18th bit algorithm for the first 17 bits is as follows:
      • sum = (n1 + n11) * 7 + (n2 + n12) * 9 + (n3 + n13) * + (N4 + n14) * 5 + (N5 + n15) * 8 + (N6 + N16) * 4 + (N7 + n17) * 2 + N8 + N9 * 6 + N10 * 3, where N1 represents 1 digits, other similar
      • Divide the sum by 11 to see what the remainder is, and there may only be 0 1 2 3 4 5 6 7 8 9 10 of these 11 digits. The number of the last identity card corresponding to the respective is 1 0 X 9 8 7 6 5 4 3 2
    6. The 7th digit must be 1, the 8th digit must be 9, i.e.: the first two digits of the year of birth must be 19

Second, the code example:

For this code that needs to be used in many places, it can be written using a tool class or a classification. Here, the code for the checksum is written in a tool class.

VerifyRegexTool.h File Code:

 1 #import <Foundation/Foundation.h> 2 3 @interface verifyregextool:nsobject 4 5 + (BOOL) Verifyisnotempty: (nsstr   ing *) str;    Verify that it is not empty 6 7 + (BOOL) Verifytext: (NSString *) text Withregex: (NSString *) regex; Regular Validation 8 9 + (BOOL) Verifyidcardnumber: (NSString *) value;   Verify ID card + (BOOL) Verifycardnumberwithsoldier: (NSString *) value;  Verify the Military ID or police officer Card (Verifyidcardhadadult) (BOOL): (NSString *) card; Verify that the ID card is adult and less than 100 years old * * * This method does not do ID verification, please ensure that the correct ID is passed in (BOOL) Verifyidcardmorethanpointdate: (NSString *) card Withnumber: (nsinteger) Number withaddtimeinterval: (nstimeinterval) interval withdatetype: (datetype) Datetype; Verify that the identity card plus the specified number of days is greater than the type of the specified # + (BOOL) verifyidcardlessthanpointdate: (NSString *) card withnumber: (Nsinteger)  Number withaddtimeinterval: (nstimeinterval) interval withdatetype: (datetype) Datetype;   Verify that the ID is less than the type of the specified number (NSString *) Getidcardbirthday: (NSString *) card;   Get the ID card of birthday * * * This method does not do ID verification, please ensure that the correct ID card is passed + (Nsinteger) Getidcardsex: (NSString *) card; Get identityGender (1 male 0 female) * * * * This method does not do ID verification, please ensure that the correct ID card is passed @end 

VERIFYREGEXTOOL.M File Code:

  1 #import "VerifyRegexTool.h" 2 3 @implementation Verifyregextool 4 5//Verify that it is not empty 6 + (BOOL) Verifyisnotempty: (nsstr  ing *) STR 7 {8 if (!STR) return NO; 9 str = [str stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]; if (![ STR isequaltostring:@ ""]) {15} 16} 17 18//regular validation 19 + () (BOOL) Verifytext: (NSString *) text Withregex: (NSString *) regex [[Text Stringbytrimmingcharactersinset:[n Scharacterset Whitespacecharacterset]] Ismatchedbyregex:regex]; 22} 23 24//Verify ID 25//must meet the following rules 26//1. The length must be 18 bits, the first 17 digits must be a number, and the 18th digit can be a number or x 27//2. The first two bits must be one of the following: 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 28//3. 7th to 14th place of birth date. The 7th to 10th place is the year of birth; 11 to 12 bits represents the month, and the range is 01-12;13 to 14 bits valid for date 29//4. The 17th place is gender, even the female, the singular means male 30//5. The 18th bit is the first 17 bits of the parity 31///algorithm as follows: 32//(1) checksum = (n1 + n11) * 7 + (n2 + n12) * 9 + (n3 + n13) * + (N4 + n14) * 5 + (N5 + N15) * 8 + (N6 + N16) * 4 + (N7 + n17) * 2 + n8 + N9 * 6 + N10 * 3, where n value, indicating number of digits 33//(2) remainder = checksum% 11 34//(3) If remainder is 0, the check digit should be 1, the remainder of the 1 to 10 check digit should be the string "0x98765432" (not including the semicolon) of the value of the remainder of the number of bits (such as the residue equals 3, the check bit should be 9) 35//6. The first two digits of the year of birth must be 19 or + + (BOOL) Verifyidcardnumber: (NSString *) value of the PNS {value = [value Stringbytrimmingcharactersinse T:[nscharacterset Whitespaceandnewlinecharacterset]]; if ([value length]! =) {+-return NO; NSString} *MMDD = @ "(((0[13578]|1[02)) (0[1-9]|[ 12][0-9]|3[01]) | ((0[469]|11) (0[1-9]|[ 12][0-9]|30)) | (02 (0[1-9]|[ 1][0-9]|2[0-8])) "; NSString *LEAPMMDD = @ "0229"; NSString *year = @ "(19|20) [0-9]{2}"; NSString *leapyear = @ "(19|20) (0[48]|[ 2468][048]| [13579] [26]) "; NSString *YEARMMDD = [NSString stringwithformat:@ "%@%@", year, MMDD]; NSString *LEAPYEARMMDD = [NSString stringwithformat:@ "%@%@", Leapyear, LEAPMMDD]; NSString *YYYYMMDD = [NSString stringwithformat:@ "((%@) | ( %@)| (%@)) ", YEARMMDD, LEAPYEARMMDD, @" 20000229 "]; NSString *area = @ "(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[ 7-9]1) [0-9]{4} "; NSString *regex = [NSString stringwithformat:@ "%@%@%@", area, YYYYMMDD, @ "[0-9]{3}[0-9xx]"]; Wuyi nspredicate *regextest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex]; if (![ Regextest Evaluatewithobject:value]) {si return NO; n} Summary int = ([value Substringwithrange:nsma Kerange (0,1)].intvalue + [value substringwithrange:nsmakerange (10,1)].intvalue) *7 + ([value substringwithr Ange:nsmakerange (].intvalue) + [value Substringwithrange:nsmakerange (11,1)].intvalue) *9 + ([value subst Ringwithrange:nsmakerange (2,1)].intvalue + [value substringwithrange:nsmakerange (12,1)].intvalue) *10 + ([v             Alue Substringwithrange:nsmakerange (3,1)].intvalue + [value substringwithrange:nsmakerange (13,1)].intValue) 60 + ([value Substringwithrange:nsmakerange (4,1)].intvalue + [value SubstriNgwithrange:nsmakerange (14,1)].intvalue) *8 + ([value Substringwithrange:nsmakerange (5,1)].intvalue + [Valu E Substringwithrange:nsmakerange (15,1)].intvalue) + ([value Substringwithrange:nsmakerange (6,1)].intValu e + [value Substringwithrange:nsmakerange (16,1)].intvalue) * * + [value Substringwithrange:nsmakerange (7,1)] . intvalue * + [value substringwithrange:nsmakerange (8,1)].intvalue *6 + [value Substringwithrange:nsmakera Nge (9,1)].intvalue; Nsinteger remainder = Summary% 11; NSString *checkbit = @ ""; NSString *checkstring = @ "10x98765432"; Checkbit = [checkstring substringwithrange:nsmakerange (remainder,1)];//judgment check digit return [Checkbit Isequaltostri Ng:[[value Substringwithrange:nsmakerange (17,1)] uppercasestring]; 70} 71 72//Verify the Officer ID or Police badge 73//must be one of the two formats below 74//format one: 4 to 20 digits 75//Format two: greater than or equal to 10 bits and less than or equal to 20 bits (Chinese by two bits), and meet the following rules 76//1) must have "word" two words 77 2) "character" preceded by at least one character 78//3) "Word first" is 4 digits above the number + (BOOL) verifYcardnumberwithsoldier: (NSString *) value (nsstring) *s1 = @ "^\\d*$"; NSString *s2 = @ "^.{ 1,} word "\\d{4,}$"; //nsstring *s3 = @ "^ ([a-za-z0-9\\u4e00-\\u9fa5]) *$"; Stringbytrimmingcharactersinset:[nscharacterset value = [value: Whitespaceandnewlinecharacterset]];  if ([Verifyregextool verifytext:value withregex:s1]) {NSString *S11 = @ "^\\d{4,20}$"; [Value ISMATCHEDBYREGEX:S11]; Lengthusingchinesecharactercountbytwo:value} else if ([self-] >= && [self Lengthusi Ngchinesecharactercountbytwo:value] (<=) {[Value ismatchedbyregex:s2]; Urn NO; 94} (Nsuinteger) Lengthusingchinesecharactercountbytwo: (NSString *) string{$ Nsuinteger count = 0; 98 fo R (Nsuinteger i = 0; i< string.length; ++i) {if ([string characteratindex:i] <) {Cou        NT++;101} else {102 count + = 2;103 }104}105 return count;106}107 108//Verify ID card is adult and less than 100 years old * * * This method does not do ID verification, please ensure that the correct ID is passed 109 + (BOOL) Verifyidcardhadadu   LT: (NSString *) card110 {111 NSString *birtday = [Verifyregextool Getidcardbirthday:card]; Year * * Month * * Day 112//converted to ****-**-**113 birtday = [Birtday stringbyreplacingoccurrencesofstring:@ "Year" withstring:@ "-"]; Birtday = [Birtday stringbyreplacingoccurrencesofstring:@ "Month" withstring:@ "-"];115 birtday = [Birtday StringByR     eplacingoccurrencesofstring:@ "Day" withstring:@ "];116 birtday = [NSString stringwithformat:@"%@ 00:00 ", birtday];117  NSDate *datebirtday = [NSDate datefromstring:birtday];118 nsinteger year = [[NSDate Date] Getintervaltime:datebirtday   Withdatetype:datetypeyear]; Get the number of years 119 if (year >= && year <) {121 return yes;122} else {123 Retu  RN no;124}125 126}127 128//Verify ID plus whether the specified number of days is greater than the type of the specified # 129 + (BOOL) verifyidcardmorethanpointdate: (NSString *) card Withnumber: (nsinteGER) Number withaddtimeinterval: (nstimeinterval) interval withdatetype: (datetype) datetype {nsstring *birtday = [Ver]   Ifyregextool Getidcardbirthday:card]; Year * * Month * * Day 131//converted to ****-**-**132 birtday = [Birtday stringbyreplacingoccurrencesofstring:@ "Year" withstring:@ "-"]; 133 Birtday = [Birtday stringbyreplacingoccurrencesofstring:@ "Month" withstring:@ "-"];134 birtday = [Birtday StringByR     eplacingoccurrencesofstring:@ "Day" withstring:@ "];135 birtday = [NSString stringwithformat:@"%@ 00:00 ", birtday];136 NSDate *datebirtday = [NSDate datefromstring:birtday];137 nsdate *today = [NSDate datefromstringwithouttime:[[nsdate Date] strdate]];138 nsdate *pointdate = [Today datebyaddingtimeinterval:interval];139 the day after//tempdate is number  Period 141 NSDate *tempdate = [pointdate dateaddnumber:-number withdatetype:datetype]; Critical Date 142 if ([datebirtday earlierdate:tempdate] = = Datebirtday) {//If birthday + days specified heavy rain tempdate, then 143 return yes;14    4} else {145     return no;146}147}148 149//Verify the ID plus the specified number of days is less than the type of the specified numbers (BOOL) Verifyidcardlessthanpointdate: (NSString *) car D Withnumber: (nsinteger) Number withaddtimeinterval: (nstimeinterval) interval withdatetype: (datetype) dateType151 {   NSString *birtday = [Verifyregextool Getidcardbirthday:card]; Year * * Month * * Day 153//converted to ****-**-**154 birtday = [Birtday stringbyreplacingoccurrencesofstring:@ "Year" withstring:@ "-"]; 155 Birtday = [Birtday stringbyreplacingoccurrencesofstring:@ "Month" withstring:@ "-"];156 birtday = [Birtday StringByR     eplacingoccurrencesofstring:@ "Day" withstring:@ "];157 birtday = [NSString stringwithformat:@"%@ 00:00 ", birtday];158 NSDate *datebirtday = [NSDate datefromstring:birtday];159 nsdate *today = [NSDate datefromstringwithouttime:[[nsdate      Date] strdate]];160 nsdate *pointdate = [Today datebyaddingtimeinterval:interval];161 162//tempdate is number after 163  NSDate *tempdate = [pointdate dateaddnumber:-number withdatetype:datetype]; //Critical Date 164 if ([tempdate earlierdate:datebirtday] = = tempdate) {//if birthday + specified number of days is less than tempdate, then 165 return yes;166 } else {167 return no;168}169}170 171//Get ID for Birthday * * * This method does not do ID check, please ensure that the correct ID is passed 172 + (NSString *) Getidcardbir Thday: (NSString *) Card {173 card = [Card Stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacte rset]];174 if ([card length]! =) {175 return nil;176}177 nsstring *birthady = [NSString stringwith format:@ "%@ year%@ month%@ Day", [card Substringwithrange:nsmakerange (6,4)], [card Substringwithrange:nsmakerange (10,2)], [card Substringwithrange:nsmakerange (12,2)]];178 return birthady;179}180 181//Get Identity card gender (1 male 0 female) * * * * This method does not do the identity check, Make sure the correct ID is passed in 182 + (Nsinteger) Getidcardsex: (NSString *) Card {183 card = [Card Stringbytrimmingcharactersinset:[nscharac         Terset whitespaceandnewlinecharacterset]];184 Nsinteger defaultvalue = 0;185 if ([card length]! = 18) {186 return defaultvalue;187}188 NsintegER number = [[Card Substringwithrange:nsmakerange (16,1)] integervalue];189 if (number% 2 = = 0) {//even for female Eturn 0;191} else {192 return 1;193}194}195 196 @end

Example of an identity check call:

1    if (![ Verifyregextool verifyidcardnumber:self. Identitycardnotextfield.text]) {   //Verify the ID card is true 2         [self showmessagehud:@] Please enter the correct ID number! "Withtimeinterval:kshowmessagetime];3         return;4     }

Practical tips for iOS development-regular check of identity cards

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.