• iOS 身份证验证


    - (void)onClickButton:(id) sender{
    if (tmp_txt.text.length == 15 || tmp_txt.text.length == 18) {                
                    NSString *emailRegex = @"^[0-9]*$";
                    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
                    bool sfzNo = [emailTest evaluateWithObject:[tmp_txt.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
                    
                    if (tmp_txt.text.length == 15) {
                        if (!sfzNo) {
                            [self HUDShow:@"请输入正确的身份证号" delay:1.5];                       
                            return NO;
                        }
                    }
                    else if (tmp_txt.text.length == 18) {                    
                        bool sfz18NO = [Global checkIdentityCardNo:tmp_txt.text];
                        if (!sfz18NO) {
                            [self HUDShow:@"请输入正确的身份证号" delay:1.5];                       
                            return NO;
                        }
                    }
                }else{
                    [self HUDShow:@"请输入正确的身份证号" delay:1.5];               
                    return NO;
                }
    }
    
    
    
    #pragma mark - 身份证识别
    +(BOOL)checkIdentityCardNo:(NSString*)cardNo
    {
        if (cardNo.length != 18) {
            return  NO;
        }
        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 = 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;
        }
        return  NO;
    }
  • 相关阅读:
    Row Cache lock Problem
    AIX操作系统上安装Oracle数据库必不可少的几项检查工作
    如何使用MOS风格的代码背景?
    在Ubuntu 10上使用DLink DWA 130无线网卡
    PL/SQL Developer View SQL功能的一个Bug
    11g新特性SQL PLUS 错误日志
    生病了。。。
    ORA00600: [7005], [192]内部错误一例
    Linux:vmware下ubuntu更换网卡后无法识别网卡
    Linux:LFS:第一天:今天开始学习,计划7天时间
  • 原文地址:https://www.cnblogs.com/yswdarren/p/3559423.html
Copyright © 2020-2023  润新知