iOS_身份证号码校验

2017-09-13 20:47:13来源:CSDN作者:FlyingKuiKui人点击

分享
  • 校验身份证号码格式是否正确,正确返回YES
BOOL IsIdentityCard(NSString * _Nullable IDCardNumber){    if (IDCardNumber.length <= 0) {        return NO;    }    // 是否符合正则表达    NSString *regex2 = @"^(//d{14}|//d{17})(//d|[xX])$";    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];    BOOL regexFlag = [identityCardPredicate evaluateWithObject:IDCardNumber];    if (!regexFlag) {        return NO;    }    // 省份代码 身份证前两位    BOOL areasFlag = NO;    NSArray *areasArray =@[@"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"];    NSString *valueStart2 = [IDCardNumber substringToIndex:2];    for (NSString *temp in areasArray) {        if ([valueStart2 isEqualToString:temp]) {            areasFlag = YES;            break;        }    }    if (!areasFlag) {        return NO;    }    // 校验生日合法性    NSRegularExpression *regularExpression = nil;    NSInteger numberofMatch = 0;    int year = 0;    if (IDCardNumber.length == 15) {        year = [IDCardNumber substringWithRange:NSMakeRange(6,2)].intValue +1900;        if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {            regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];        }else {            regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];        }        numberofMatch = [regularExpression numberOfMatchesInString:IDCardNumber options:NSMatchingReportProgress range:NSMakeRange(0, IDCardNumber.length)];        if(numberofMatch > 0) {            return YES;        }else {            return NO;        }    }else{        year = [IDCardNumber substringWithRange:NSMakeRange(6,4)].intValue;        if (year %4 == 0 || (year %100 == 0 && year %4 == 0)) {            regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$" options:NSRegularExpressionCaseInsensitive error:nil];        }else{            regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$" options:NSRegularExpressionCaseInsensitive error:nil];        }        numberofMatch = [regularExpression numberOfMatchesInString:IDCardNumber options:NSMatchingReportProgress range:NSMakeRange(0, IDCardNumber.length)];        if (numberofMatch > 0) {            int Sum = ([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;            int Y = Sum % 11;            NSString *checkCode = @"10X98765432";            // 判断校验位            NSString *code = [checkCode substringWithRange:NSMakeRange(Y,1)];            if ([code isEqualToString:[IDCardNumber substringWithRange:NSMakeRange(17, 1)].uppercaseString]) {                return YES;            }else{                return NO;            }        }else{            return NO;        }    }}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台