(1)判断一下是否是手机号
(2)字符串为空判断
(3)身份证号判断
- (BOOL)isIdCardNumber:(NSString *)IDNumber {
NSMutableArray *IDArray = [NSMutableArray array];
for (int i = 0; i <18; i++) {
NSRange range = NSMakeRange(i, 1);
NSString *subString = [IDNumber substringWithRange:range];
[IDArray addObject:subString];
}
// 系数数组
NSArray *coefficientArray = @[@7, @9, @10, @5, @8, @4, @2, @1, @6, @3, @7, @9, @10, @5, @8, @4, @"2"];
// 余数数组
NSArray *remainderArray = @[@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
// 每一位身份证号码和对应系数相乘之后相加所得的和
int sum = 0;
for (int i = 0; i <17; i++) {
int coefficient = [coefficientArray[i] intValue];
int ID = [IDArray[i] intValue];
sum += coefficient * ID;
}
// 这个和除以11的余数对应的数
NSString *str = remainderArray[(sum % 11)];
// 身份证号码最后一位
NSString *string = [IDNumber substringFromIndex:17];
// 如果这个数字和身份证最后一位相同,则符合国家标准,返回YES
return [strisEqualToString:string];
}