ID card number verification, ID card number verification
Previously, I used regular expressions to verify the ID card number, but sometimes the wrong ID card number can also pass the front-end verification. Later I found that this method is good and I posted the code to you:
// ID card number verification + (BOOL) validateIdentityCard :( NSString *) cardNo {if (cardNo. length! = 18) {return NO;} NSArray * codeArray = [NSArray arrayWithObjects: @ "7", @ "9", @ "10", @ "5 ", @ "8", @ "4", @ "2", @ "1", @ "6", @ "3", @ "7", @ "9 ", @ "10", @ "5", @ "8", @ "4", @ "2", nil]; NSDictionary * checkCodeDic = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: @ "1", @ "0", @ "X", @ "9", @ "8", @ "7", @ "6", @ "5 ", @ "4", @ "3", @ "2", nil] forKeys: [NSArray arrayWithObjects: @ "0", @ "1", @ "2 ", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "1 0 ", nil]; ns1_* scan = [ns1_scannerwithstring: [cardNo substringToIndex: 17]; int val; BOOL isNum = [scan scanInt: & val] & [scan isAtEnd]; if (! IsNum) {return NO;} int sumValue = 0; for (int I = 0; I <17; I ++) {sumValue + = [[cardNo substringWithRange: NSMakeRange (I, 1)] intValue] * [[codeArray objectAtIndex: I] intValue];} NSString * strlast = [checkCodeDic objectForKey: [NSString stringWithFormat: @ "% d ", sumValue % 11]; if ([strlast islast tostring: [cardNo substringWithRange: NSMakeRange (17, 1)] uppercaseString]) {return YES;} return NO ;}