代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | + (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; } |