代碼如下:
+ (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; }