码迷,mamicode.com
首页 > 移动开发 > 详细

ios严格检验身份证号码有效性

时间:2015-08-02 01:02:50      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:ios   身份证校验   

代码如下:

+ (BOOL)checkIDCard:(NSString *)sPaperId {
    //判断位数
    if (sPaperId.length != 15 && sPaperId.length != 18) {
        return NO;
    }
    NSString *carid = sPaperId;
    long lSumQT = 0 ;
    //加权因子
    int R[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
    //校验码
    unsigned char sChecker[11] = {'1','0','X','9','8','7','6','5','4','3','2'};
    //将15位身份证号转换为18位
    NSMutableString *mString = [NSMutableString stringWithString:sPaperId];
    if (sPaperId.length == 15) {
        [mString insertString:@"19" atIndex:6];
        long p =0;
        //        const char *pid = [mString UTF8String];
        for (int i =0; i<17; i++)
        {
            NSString * s = [mString substringWithRange:NSMakeRange(i, 1)];
            p += [s intValue] * R[i];
            //            p += (long)(pid-48) * R;//
            
        }
        int o = p%11;
        NSString *string_content = [NSString stringWithFormat:@"%c",sChecker[o]];
        [mString insertString:string_content atIndex:[mString length]];
        carid = mString;
    }
    //判断地区码
    NSString *sProvince = [carid substringToIndex:2];
    NSLog(@"sProvince = %@",sProvince);
    if (![self isAreaCode:sProvince]) {
        return NO ;
    }
    //判断年月日是否有效
    //年份
    int strYear = [[VUtils getStringWithRange:carid Value1:6 Value2:4] intValue];
    //月份
    int strMonth = [[VUtils getStringWithRange:carid Value1:10 Value2:2] intValue];
    //日
    int strDay = [[VUtils getStringWithRange:carid Value1:12 Value2:2] intValue];
    NSTimeZone *localZone = [NSTimeZone localTimeZone];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeZone:localZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",strYear,strMonth,strDay]];
    if (date == nil) {
        return NO;
    }
    [carid uppercaseString];
    const char *PaperId = [carid UTF8String];
    //检验长度
    if (18!=strlen(PaperId)) {
        return NO;
    }
    //校验数字
    NSString * lst = [carid substringFromIndex:carid.length-1];
    char di = [carid characterAtIndex:carid.length-1];
    
    if (!isdigit(di)) {
        if ([lst isEqualToString:@"X"]) {
        }else{
            return NO;
        }
    }
    //验证最末的校验码
    lSumQT = 0;
    for (int i = 0; i<17; i++){
        NSString * s = [carid substringWithRange:NSMakeRange(i, 1)];
        lSumQT += [s intValue] * R[i];
    }
    if (sChecker[lSumQT%11] != PaperId[17]) {
        return NO;
    }
    return YES;
}


+ (NSArray *)provinceArr {
    NSArray *pArr = @[
                      
                      @"11",//北京市|110000,
                      
                      @"12",//天津市|120000,
                      
                      @"13",//河北省|130000,
                      
                      @"14",//山西省|140000,
                      
                      @"15",//内蒙古自治区|150000,
                      
                      @"21",//辽宁省|210000,
                      
                      @"22",//吉林省|220000,
                      
                      @"23",//黑龙江省|230000,
                      
                      @"31",//上海市|310000,
                      
                      @"32",//江苏省|320000,
                      
                      @"33",//浙江省|330000,
                      
                      @"34",//安徽省|340000,
                      
                      @"35",//福建省|350000,
                      
                      @"36",//江西省|360000,
                      
                      @"37",//山东省|370000,
                      
                      @"41",//河南省|410000,
                      
                      @"42",//湖北省|420000,
                      
                      @"43",//湖南省|430000,
                      
                      @"44",//广东省|440000,
                      
                      @"45",//广西壮族自治区|450000,
                      
                      @"46",//海南省|460000,
                      
                      @"50",//重庆市|500000,
                      
                      @"51",//四川省|510000,
                      
                      @"52",//贵州省|520000,
                      
                      @"53",//云南省|530000,
                      
                      @"54",//西藏自治区|540000,
                      
                      @"61",//陕西省|610000,
                      
                      @"62",//甘肃省|620000,
                      
                      @"63",//青海省|630000,
                      
                      @"64",//宁夏回族自治区|640000,
                      
                      @"65",//新疆维吾尔自治区|650000,
                      
                      @"71",//台湾省(886)|710000,
                      
                      @"81",//香港特别行政区(852)|810000,
                      
                      @"82",//澳门特别行政区(853)|820000
                      
                      @"91",//国外
                      ];
    return pArr;
}

+ (BOOL)isAreaCode:(NSString *)province {
    //在provinceArr中找
    NSArray * arr = [VUtils provinceArr];
    int a = 0;
    for (NSString * pr in arr) {
        if ([pr isEqualToString:province]) {
            a ++;
        }
    }
    if (a == 0) {
        return NO;
    } else {
        return YES;
    }
}

+ (NSString *)getStringWithRange:(NSString *)str Value1:(int)v1 Value2:(int)v2 {
    NSString * sub = [str substringWithRange:NSMakeRange(v1, v2)];
    return sub;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

ios严格检验身份证号码有效性

标签:ios   身份证校验   

原文地址:http://blog.csdn.net/xiaokfc/article/details/47193387

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!