//判断身份证号码是否正确
+ (BOOL)checkIdCardNumber:(NSString *)IdCardNumber
{
IdCardNumber = [IdCardNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([IdCardNumber length] != 18) {
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";
//判断年份 1900-2009
NSString *year = @"((19[0-9][0-9])|(200[0-9]|201[1-5]))";
//闰年
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"];
//判断地区,前六位,35种可能
NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|71|8[12])[0-9]{4}";
NSString *regex = [NSString stringWithFormat:@"%@%@%@", area, yyyyMmdd , @"[0-9]{3}[0-9Xx]"];
NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if (![regexTest evaluateWithObject:IdCardNumber]) {
return NO;
}
//判断校验位
int summary = ([IdCardNumber substringWithRange:NSMakeRange(0, 1)].intValue + [IdCardNumber substringWithRange:NSMakeRange(10,1)].intValue) *7
+ ([IdCardNumber substringWithRange:NSMakeRange(1,1)].intValue + [IdCardNumber substringWithRange:NSMakeRange(11,1)].intValue) *9
+ ([IdCardNumber substringWithRange:NSMakeRange(2,1)].intValue + [IdCardNumber substringWithRange:NSMakeRange(12,1)].intValue) *10
+ ([IdCardNumber substringWithRange:NSMakeRange(3,1)].intValue + [IdCardNumber substringWithRange:NSMakeRange(13,1)].intValue) *5
+ ([IdCardNumber substringWithRange:NSMakeRange(4,1)].intValue + [IdCardNumber substringWithRange:NSMakeRange(14,1)].intValue) *8
+ ([IdCardNumber substringWithRange:NSMakeRange(5,1)].intValue + [IdCardNumber substringWithRange:NSMakeRange(15,1)].intValue) *4
+ ([IdCardNumber substringWithRange:NSMakeRange(6,1)].intValue + [IdCardNumber substringWithRange:NSMakeRange(16,1)].intValue) *2
+ [IdCardNumber substringWithRange:NSMakeRange(7,1)].intValue *1 + [IdCardNumber substringWithRange:NSMakeRange(8,1)].intValue *6
+ [IdCardNumber substringWithRange:NSMakeRange(9,1)].intValue *3;
NSInteger remainder = summary % 11;
NSString *chectBit = @"";
NSString *checkString = @"10X98765432";
chectBit = [checkString substringWithRange:NSMakeRange(remainder, 1)];
return [chectBit isEqualToString:[[IdCardNumber substringWithRange:NSMakeRange(17, 1)] uppercaseString]];
}
身份证格式判断 |