22、[|22、[ iOS ] 身份证号校验

/** * 功能:身份证号校验 */ // ------确定 - (void)authClick {if (![self checkData]) { return; }; // 写验证成功后的代码 } - (BOOL)checkData { MBProgressHUD *mbhud = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:mbhud]; mbhud.delegate = self; mbhud.yOffset = - 50; mbhud.mode = MBProgressHUDModeText; // 姓名 if (_name.text.length == 0) { mbhud.labelText = @"请输入姓名"; [mbhud show:YES]; [mbhud hide:YES afterDelay:1]; return NO; }// 身份证 if (_identity.text.length == 0) { mbhud.labelText = @"请输入身份证号码"; [mbhud show:YES]; [mbhud hide:YES afterDelay:1]; return NO; }if (_identity.text.length == 15 || _identity.text.length == 18) { NSString *emailRegex = @"^[0-9]*$"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; bool sfzNo = [emailTest evaluateWithObject:[_identity.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; if (_identity.text.length == 15) { if (!sfzNo) { mbhud.labelText = @"请输入正确的身份证号"; [mbhud show:YES]; [mbhud hide:YES afterDelay:1]; return NO; } } else if (_identity.text.length == 18) { bool sfz18NO = [self checkIdentityCardNo:_identity.text]; if (!sfz18NO) { mbhud.labelText = @"请输入正确的身份证号"; [mbhud show:YES]; [mbhud hide:YES afterDelay:1]; return NO; } } }else{ mbhud.labelText = @"请输入正确的身份证号"; [mbhud show:YES]; [mbhud hide:YES afterDelay:1]; return NO; }return YES; } #pragma mark - 身份证识别 - (BOOL)checkIdentityCardNo:(NSString*)cardNo { if (cardNo.length != 18) { returnNO; } NSArray* codeArray = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2", nil]; NSDictionary* checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil]forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]]; NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:17]]; int val; BOOL isNum = [scan scanInt:&val] && [scan isAtEnd]; if (!isNum) { return NO; } int sumValue = https://www.it610.com/article/0; for (int i =0; i<17; i++) { sumValue+=[[cardNo substringWithRange:NSMakeRange(i , 1) ] intValue]* [[codeArray objectAtIndex:i] intValue]; }NSString* strlast = [checkCodeDic objectForKey:[NSString stringWithFormat:@"%d",sumValue%11]]; if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(17, 1)]uppercaseString]]) { return YES; } returnNO; }

    推荐阅读