實名認證判斷身份證和姓名是否合法

//判斷姓名

-(void)checkName{

NSString * ptr = @"^[\u4E00-\u9FA5]*$";

NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",ptr];

if (![pred evaluateWithObject:_realname.text]||_realname.text.length<2||_realname.text.length>6) {

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"請輸入合法姓名!"] preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:action];

[self presentViewController:alert animated:YES completion:nil];

}else{

[self upload];

}

}

//判斷身份證號是否合法

-(void)checkIDcard

{

NSString *ptr = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",ptr];

if (_IDnum.text.length<=0) {

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"請輸入18位有效身份證號"] preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:action];

[self presentViewController:alert animated:YES completion:nil];

}else{

if ([pred evaluateWithObject:_IDnum.text]) {

if (_IDnum.text.length == 18) {

//將前17位加權(quán)因子保存在數(shù)組里

NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];

//這是除以11后,可能產(chǎn)生的11位余數(shù)、驗證碼,也保存成數(shù)組

NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];

//用來保存前17位各自乖以加權(quán)因子后的總和

NSInteger idCardWiSum = 0;

for(int i = 0;i < 17;i++)

{

NSInteger subStrIndex = [[_IDnum.text substringWithRange:NSMakeRange(i, 1)] integerValue];

NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];

idCardWiSum+= subStrIndex * idCardWiIndex;

}

//計算出校驗碼所在數(shù)組的位置

NSInteger idCardMod=idCardWiSum%11;

//得到最后一位身份證號碼

NSString * idCardLast= [_IDnum.text substringWithRange:NSMakeRange(17, 1)];

if (idCardMod == 2) {

if ([idCardLast isEqualToString:@"X"]) {

[self checkName];

}else{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"請輸入18位有效身份證號"] preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:action];

[self presentViewController:alert animated:YES completion:nil];

}

}else{

//用計算出的驗證碼與最后一位身份證號碼匹配,如果一致,說明通過,否則是無效的身份證號碼

if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])

{

[self checkName];

}else{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"請輸入18位有效身份證號"] preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:action];

[self presentViewController:alert animated:YES completion:nil];

}

}

}else{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"請輸入18位有效身份證號"] preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:action];

[self presentViewController:alert animated:YES completion:nil];

}

}else{

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"請輸入18位有效身份證號"] preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:action];

[self presentViewController:alert animated:YES completion:nil];

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容