iOS 校验身份证号码是否正确

首先在UITextField代理中

- (void)textFieldDidEndEditing:(UITextField *)textField方法里设置

- (void)textFieldDidEndEditing:(UITextField *)textField;{
    if (self.IDCardNoField == textField) {
        NSCharacterSet *setToRemove = [[ NSCharacterSet characterSetWithCharactersInString:@"0123456789X"]
                                       invertedSet ];
        self.IDCardNoField.text  = [[self.IDCardNoField.text  componentsSeparatedByCharactersInSet:setToRemove] componentsJoinedByString:@""];
    }
}

这会让你在编辑结束后将非省份证号包含的字符消除掉

判断身份证号是否合法

百度上搜到的方法是,但判断强度很鸡肋
- (BOOL)isCorrect:(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 = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil];
    // 余数数组
    NSArray *remainderArray = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil];
    // 每一位身份证号码和对应系数相乘之后相加所得的和
    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
    if ([str isEqualToString:string]) {
        return YES;
    } else {
        return NO;
    }
}
下面附上我的方法(在原有的基础上加了)
- (BOOL)isCorrect:(NSString *)IDNumber
{
    if (IDNumber.length<18) {
        return NO;
    }
    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 = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil];
    // 余数数组
    NSArray *remainderArray = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil];
    // 每一位身份证号码和对应系数相乘之后相加所得的和
    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
    if ([str isEqualToString:string]) {
        
        NSString *birthdayYer = [IDNumber substringWithRange:NSMakeRange(6, 4)];
        NSString *birthdayMan = [IDNumber substringWithRange:NSMakeRange(10, 2)];
        NSString *birthdayDay = [IDNumber substringWithRange:NSMakeRange(12, 2)];
        if ([birthdayYer intValue]<1900||[birthdayMan intValue]==0||[birthdayMan intValue]>12||[birthdayDay intValue]==1||[birthdayDay intValue]>31) {
            return NO;
        }
        
        
        return YES;
        
    } else {
        return NO;
    }
}

!注意!注意!注意!注意(正确返回YES)
如果提交表单时检验 帖下面代码

    if (![self checkCardNo:self.IDCardNoField.text]) {
        [self showHudStringAlert:@"请输入正确银行卡号"];
        return;
    }

谢谢!

[上一篇]:iOS UITextField输入身份证号设置

[下一篇]:iOS 让UITextField输入银行卡号设置并校验






我的专题:

iOS开发

Mac汉化(游戏/软件)

你可能感兴趣的:(iOS 校验身份证号码是否正确)