IOS development tips-regular verification of ID cards

Source: Internet
Author: User

IOS development tips-regular verification of ID cards
Note: ID card verification may be required in many places during ios project development. Generally, during development, many people directly go to Baidu to change the related regular expressions and verification code, however, the verification code uploaded and copied on the Internet may not be accurate and may be risky. For example, if you stop using 15-digit ID cards from January 1, January 1, 2013, online id card verification generally supports 15-digit numbers. In the development process, when performing similar processing, you still need some screening capabilities. Of course, you also need to consider the actual situation of your project. This article posts the ID card verification code used in recent projects to help people who need it "cautiously. I. The rules below are the iOS ID card verification rules. It is worth noting that the first two digits of the Year of birth are 20 reasonable. If you want to verify that the applicant must be at least 18 years old, you need to check the applicant's ID card. The length must be 18 bits, the first 17 bits must be digits, and the first two bits can be digits or X (Case Insensitive during verification) must be one of the following 35 cases:, 91 7th-14th birthdate. The Year of birth is between 7th and 10th, and the period from 11 to 12 indicates the month in the range of 01 to 12 ~ 12; the values from 13 to 14 are valid dates. For example, if the month is 04, the value range is 01 to 14 ~ 30 17th bits indicate gender. The value must be 0 or 1. 0 indicates female. 1 indicates that male 18th bits represent the first 17 bits. The algorithm is as follows: Sum = (n1 + n11) * 7 + (n2 + n12) * 9 + (n3 + n13) * 10 + (n4 + n14) * 5 + (n5 + n15) * 8 + (n6 + n16) * 4 + (n7 + n17) * 2 + n8 + limit * 6 + n10 * 3, where n1 represents a number. For others, divide the sum by 11 to see the remainder, the remainder may only contain the 11 numbers 0 1 2 3 4 5 6 7 8 9 10. The numbers corresponding to the last ID card respectively are 1 0X9 8 7 6 5 4 3 2 7th digits must be 1 and 8th digits must be 9, that is: the first two digits of the Year of birth must be 19. Example code: You can use a tool class or a category to write the code that needs to be used in many places. The verification code is written in a tool class. VerifyRegexTool. h file code: 1 # import <Foundation/Foundation. h> 2 3 @ interface VerifyRegexTool: NSObject 4 5 + (BOOL) verifyIsNotEmpty :( NSString *) str; // verify whether it is not empty 6 7 + (BOOL) verifyText :( NSString *) text withRegex :( NSString *) regex; // Regular Expression verification 8 9 + (BOOL) verifyIDCardNumber :( NSString *) value; // verify ID card 10 11 + (BOOL) verifyCardNumberWithSoldier :( NSString *) value; // verify the military or police ID 12 13 + (BOOL) verifyIDCardHadAdult :( NS String *) card; // ID card verification is not performed in the method of verifying whether the ID card is an adult and less than 100 years old ***. Make sure that the entered ID card is correct for 14 15 + (BOOL) verifyIDCardMoreThanPointDate :( NSString *) card withNumber :( NSInteger) number withAddTimeInterval :( NSTimeInterval) interval withDateType :( DateType) dateType; // verify whether the ID card is larger than the specified number of days in Type 16 17 + (BOOL) verifyIDCardLessThanPointDate :( NSString *) card withNumber :( NSInteger) number withAddTimeInterval :( NSTimeInterval) interval withDat EType :( DateType) dateType; // verify whether the ID card is less than the specified number type 18 19 20 + (NSString *) getIDCardBirthday :( NSString *) card; // do not check the ID card for the birthday *** of your ID card. Make sure that the correct ID card 21 + (NSInteger) getIDCardSex :( NSString *) card is passed in; // obtain the gender of the ID card (1 male 0 female) ***** this method does not do ID card verification, please ensure that the entered ID card is correct 22 23 @ end VerifyRegexTool. m file code: 1 # import "VerifyRegexTool. h "2 3 @ implementation VerifyRegexTool 4 5 // verify whether it is not empty 6 + (BOOL) verifyIsNotEmpty :( NSString *) str 7 {8 If (! Str) return NO; 9 10 str = [str stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]; 11 if (! [Str isEqualToString: @ ""]) {12 return YES; 13} else {14 return NO; 15} 16} 17 18 // Regular Expression verification 19 + (BOOL) verifyText :( NSString *) text withRegex :( NSString *) regex 20 {21 return [[text character: [NSCharacterSet whitespaceCharacterSet] isMatchedByRegex: regex]; 22} 23 24 // verify ID card 25 // The following rules must be met 26 // 1. the length must be 18 digits, the first 17 digits must be digits, and the first 18th digits can be digits or X 27 // 2. the first two must be one of the following conditions: 11,12, 13,14, 15,21, 31, 7th, 14th, to birthdate. The year from 7th to 10th is the year of birth. The 11 to 12 digits indicate the month, and the range is 01-12. The 13 to 14 digits indicate the valid date 29 // 4. the second digit represents gender, the double digit represents female, and the singular digit represents male 30 // 5. 18th bits are the first 17 bits 31 // The algorithm is as follows: 32 // (1) checksum = (n1 + n11) * 7 + (n2 + n12) * 9 + (n3 + n13) * 10 + (n4 + n14) * 5 + (n5 + n15) * 8 + (n6 + n16) * 4 + (n7 + n17) * 2 + n8 + limit * 6 + n10 * 3, where n indicates the digit 33 // (2) remainder = checksum % 11 34 // (3) if the remainder is 0, the check bit should be 1, and the remainder is 1 to 10. The check bit should be the remainder digit value of the string "0X98765432" (excluding semicolons) (for example, the remainder is 3, check bit should be 9) 35 // 6. the first two digits of the Year of birth must be 1 9 or 20 36 + (BOOL) verifyIDCardNumber :( NSString *) value 37 {38 value = [value stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]; 39 if ([value length]! = 18) {40 return NO; 41} 42 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]) "; 43 NSString * leapMmdd = @ "0229"; 44 NSString * year = @ "(19 | 20) [0-9] {2 }"; 45 NSString * leapYear = @ "(19 | 20) (0 [48] | [2468] [048] | [13579] [26])"; 46 NSString * yearMmdd = [NSString stringWithFormat: @ "% @", year, mmdd]; 47 NSString * leapyearMmdd = [NSString stringWithFormat: @ "% @", leapYear, leapMmdd]; 48 NSString * yyyyMmdd = [NSString stringWithFormat: @ "(% @) | (% @) | (% @) ", yearMmdd, leapyearMmdd, @" 20000229 "]; 49 NSString * area = @ "(1 [1-5] | 2 [1-3] | 3 [1-7] | 4 [1-6] | 5 [0-5- 4] | 6 [1-5] | 82 | [7-9] 1) [0-9] {4} "; 50 NSString * regex = [NSString stringWithFormat: @" % @ ", area, yyyyMmdd, @ "[0-9] {3} [0-9Xx]"]; 51 52 NSPredicate * regexTest = [NSPredicate predic AteWithFormat: @ "self matches % @", regex]; 53 if (! [RegexTest evaluateWithObject: value]) {54 return NO; 55} 56 int summary = ([value substringWithRange: NSMakeRange (0, 1)]. intValue + [value substringWithRange: NSMakeRange (10, 1)]. intValue) * 7 57 + ([value substringWithRange: NSMakeRange (1, 1)]. intValue + [value substringWithRange: NSMakeRange (11,1)]. intValue) * 9 58 + ([value substringWithRange: NSMakeRange (2, 1)]. intValue + [value substringWithRange: NSMak ERange (12, 1)]. intValue) * 10 59 + ([value substringWithRange: NSMakeRange (3, 1)]. intValue + [value substringWithRange: NSMakeRange (13, 1)]. intValue) * 5 60 + ([value substringWithRange: NSMakeRange (4, 1)]. intValue + [value substringWithRange: NSMakeRange (14,1)]. intValue) * 8 61 + ([value substringWithRange: NSMakeRange (5, 1)]. intValue + [value substringWithRange: NSMakeRange (15,1)]. intValue) * 4 62 + ([value su BstringWithRange: NSMakeRange (6, 1)]. intValue + [value substringWithRange: NSMakeRange (16,1)]. intValue) * 2 63 + [value substringWithRange: NSMakeRange (7,1)]. intValue * 1 + [value substringWithRange: NSMakeRange (8, 1)]. intValue * 6 64 + [value substringWithRange: NSMakeRange (9,1)]. intValue * 3; 65 NSInteger remainder = summary % 11; 66 NSString * checkBit = @ ""; 67 NSString * checkString = @ "10X98765432"; 68 c HeckBit = [checkString substringWithRange: NSMakeRange (remainder, 1)]; // judge the checkBit 69 return [checkBit isEqualToString: [[value substringWithRange: NSMakeRange (17,1)] uppercaseString]; 70} 71 72 // verify the military or police ID 73 // it must be one of the following two formats // format 1: 4 to 20 digits 75 // Format 2: if the value is greater than or equal to 10 digits and less than or equal to 20 digits (two Chinese characters), and the following rules are met: 76 // 1) the value must be "wordnumber) there must be at least one character before the word "78 // 3)" character "followed by four or more digits 79 + (BOOL) verifyCardNumberWithSoldier :( NSString *) value 80 {8 1 NSString * s1 = @ "^ \ d * $"; 82 NSString * s2 = @ "^. {1,} characters \ d {4,} $ "; 83 // NSString * s3 = @" ^ ([A-Za-z0-9 \ u4e00-\ u9fa5]) * $ "; 84 value = [value stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]; 85 if ([VerifyRegexTool verifyText: value withRegex: s1]) {86 NSString * s11 = @ "^ \ d {4, 20} $"; 87 return [value isMatchedByRegex: s11]; 88} else if ([self lengthUsingChineseChar ActerCountByTwo: value]> = 10 89 & [self lengthUsingChineseCharacterCountByTwo: value] <= 20) {90 return [value isMatchedByRegex: s2]; 91} 92 93 return NO; 94} 95 96 + (NSUInteger) lengthUsingChineseCharacterCountByTwo :( NSString *) string {97 NSUInteger count = 0; 98 for (NSUInteger I = 0; I <string. length; ++ I) {99 if ([string characterAtIndex: I] <256) {100 count ++; 101} else {102 count + = 2; 103} 104} 105 return count; 106} 107 108 // ID card verification is not performed in this method to verify whether the ID card is an adult and less than 100 years old, make sure that the entered correct ID card is 109 + (BOOL) verifyIDCardHadAdult :( NSString *) card110 {111 NSString * birtday = [VerifyRegexTool getIDCardBirthday: card]; // *****-** 112 birtday = [birtday stringByReplacingOccurrencesOfString: @ "year" withString: @ "-"]; 114 birtday = [birtday stringByReplacingOccurrencesOfString: @ "month" withString: @ "-"]; 115 birtday = [birtday stringByReplacingOccurrencesOfString: @ "wit" withString: @" "]; 116 birtday = [NSString stringWithFormat: @" % @ 00:00 ", birtday]; 117 NSDate * dateBirtday = [NSDate dateFromString: birtday]; 118 NSInteger year = [[NSDate date] getIntervalTime: dateBirtday withDateType: DateTypeYear]; // obtain the number of years 119 120 if (year> = 18 & year <100) {121 return YES; 122} else {123 return NO; 124} 125 126} 127 128 // Verify that the ID card plus the specified number of days is greater than the specified number type 129 + (BOOL) verifyIDCardMoreThanPointDate :( NSString *) card withNumber :( NSInteger) number withAddTimeInterval :( NSTimeInterval) interval withDateType) dateType {130 NSString * birtday = [VerifyRegexTool getIDCardBirthday: card]; // *****-** 131 birtday = [birtday stringByReplacingOccurrencesOfString: @ "year" withString: @ "-"]; 133 birtday = [birtday st Expiration: @ "month" withString: @ "-"]; 134 birtday = [birtday stringByReplacingOccurrencesOfString: @ "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: interva L]; 139 140 // date after tempDate is number 141 NSDate * tempDate = [pointDate dateAddNumber:-number withDateType: dateType]; // critical date 142 if ([dateBirtday earlierDate: tempDate] = dateBirtday) {// If the birthday + specifies the number of days of heavy rain tempdate, use 143 return YES; 144} else {145 return NO; 146} 147} 148 149 // verify whether the ID card plus the specified number of days is less than the type of the specified number 150 + (BOOL) verifyIDCardLessThanPointDate :( NSString *) card withNumber :( NSInteger) number withAddTimeInterval :( NSTimeInterval) interval withDateType :( DateType) dateType151 {152 NSString * birtday = [VerifyRegexTool getIDCardBirthday: card]; // *****-** 153 birtday = [birtday stringByReplacingOccurrencesOfString: @ "year" withString: @ "-"]; 155 birtday = [birtday stringByReplacingOccurrencesOfString: @ "month" withString: @ "-"]; 156 birtday = [birtday hour: @ "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 // Date 163 NSDate * tempDate = [pointDate dateAddNumber:-number withDateType: dateType] After tempDate is set to number; // critical date 164 if ([tempDate EarlierDate: dateBirtday] = tempDate) {// If the birthday + specified days are less than tempdate, return YES; 165} else {166 return NO; 168} 169} 170 171 // The ID card verification is not performed in this method. Make sure that the entered ID card is 172 + (NSString *) getIDCardBirthday :( NSString *) card {173 card = [card stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]; 174 if ([card length]! = 18) {175 return nil; 176} 177 NSString * birthady = [NSString stringWithFormat: @ "% @ ", [card substringWithRange: NSMakeRange (6, 4)], [card substringWithRange: NSMakeRange (178)], [card substringWithRange: NSMakeRange ()]; return birthady; 179} 180 181 // The gender (1 male 0 female) of the ID card is not verified in this *** method. please ensure that the entered correct ID card is 182 + (NSInteger) getIDCardSex :( NSString *) card {183 card = [card stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]; 184 NSInteger defaultValue = 0; 185 if ([card length]! = 18) {186 return defaultValue; 187} 188 NSInteger number = [[card substringWithRange: NSMakeRange (189)] integerValue]; if (number % 2 = 0) {// the even number is female 190 return 0; 191} else {192 return 1; 193} 194} 195 196 @ end ID card verification call example: 1 if (! [VerifyRegexTool verifyIDCardNumber: self. IdentityCardNOTextfield. text]) {// verify the authenticity of the ID card 2 [self showMessageHUD: @ "enter the correct ID card number! "WithTimeInterval: kShowMessageTime]; 3 return; 4}

Related Article

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.