二代身份證除了位數(shù)和各位表達(dá)的含義有限制外,還有一個(gè)校驗(yàn)和的問題鞋吉,這篇文章就是來解決這個(gè)問題识腿。網(wǎng)絡(luò)上應(yīng)該有對應(yīng)的Java代碼,我在這里這是把Java代碼轉(zhuǎn)換成了oc的代碼叠萍,供大家使用芝发。已經(jīng)經(jīng)過測試,驗(yàn)證正確苛谷。
- (BOOL)validateIdentityCard:(NSString *)identityCard
{
BOOL flag;
if (identityCard.length <= 0) {
flag = NO;
return flag;
}
flag = [identityCardPredicate evaluateWithObject:identityCard];
flag = flag && [self isRealIDCode:identityCard];
return flag;
}
- (BOOL)isRealIDCode:(NSString *)realCode {
if (realCode.length < 18) {
return false;
}
NSArray *ds = @[@7, @9, @10, @5, @8, @4, @2, @1, @6, @3, @7, @9, @10, @5, @8, @4, @2];
NSInteger total = 0;
for (NSInteger i = 0; i < 17; i++) {
NSRange currentRange = NSMakeRange(i, 1);
NSString *currentCode = [realCode substringWithRange:currentRange];
NSInteger currentCodeInteger = [currentCode integerValue];
NSInteger dsi = [ds[i] integerValue];
total += currentCodeInteger * dsi;
}
total = total % 11;
NSArray *ends = @[@"1", @"0", @"x", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
NSString *end = ends[total];
if ([end isEqualToString:[[realCode substringWithRange:NSMakeRange(17, 1)] lowercaseString]]) {
return YES;
}
return NO;
}