//判斷姓名
-(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];
}
}
}