美文网首页
身份证键盘与验证

身份证键盘与验证

作者: Miss_QL | 来源:发表于2018-02-27 10:47 被阅读31次

1、自定义身份证键盘

#pragma mark - textfield delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == _cardTextField) {
        [self performSelector:@selector(addXButtonToKeyboard) withObject:nil afterDelay:0.5];
    } else {
        [self removeButtonFromKeyboard];
    }
}
 
- (void)removeButtonFromKeyboard {
    int cnt=[[UIApplication sharedApplication] windows].count;
    UIWindow * keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:cnt-1];
    [[keyboardWindow viewWithTag:8] removeFromSuperview];
}
 
// 自定义键盘:身份证键盘(数字键盘左下角改为X)
- (void)addXButtonToKeyboard {
    [self addButtonToKeyboardWithSelector:@selector(xButton) normal:[UIImage imageNamed:@""] highlight:[UIImage imageNamed:@""]];
}
 
- (void) xButton {
    _cardTextField.text=[_cardTextField.text stringByAppendingString:@"X"];
}
 
- (void)addButtonToKeyboardWithSelector:(SEL)sel normal:(UIImage *)nimg highlight:(UIImage *)himg {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.tag = 8;
    doneButton.frame = CGRectMake(0, 0, self.view.frame.size.width/3, 55);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setTitle:@"X" forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont systemFontOfSize:23];
    [doneButton setImage:nimg forState:UIControlStateNormal];
    [doneButton setImage:himg forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    int cnt=[[UIApplication sharedApplication] windows].count;
    UIWindow* keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:cnt-1];
    doneButton.frame = CGRectMake(0, keyboardWindow.frame.size.height-53, self.view.frame.size.width/3, 55);
    [keyboardWindow addSubview:doneButton];
}

2、验证身份证合法性

/**
 * 功能:验证身份证是否合法
 * 参数:输入的身份证号
 */
+ (BOOL)Chk18PaperId:(NSString *)sPaperId {
    //判断位数
    if ([sPaperId length] < 15 ||[sPaperId length] > 18) return NO;
    NSString *carid = sPaperId;
    long lSumQT =0;
    //加权因子
    int R[] ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
    //校验码
    unsigned char sChecker[11]={'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'};
    
    //将15位身份证号转换成18位
    NSMutableString *mString = [NSMutableString stringWithString:sPaperId];
    if ([sPaperId length] == 15) {
        [mString insertString:@"19" atIndex:6];
        long p = 0;
        const char *pid = [mString UTF8String];
        for (int i=0; i<=16; i++) {
            p += (pid[i]-48) * R[i];
        }
        int o = p%11;
        NSString *string_content = [NSString stringWithFormat:@"%c",sChecker[o]];
        [mString insertString:string_content atIndex:[mString length]];
        carid = mString;
    }
    //判断地区码
    NSString * sProvince = [carid substringToIndex:2];
    if (![self areaCode:sProvince]) return NO;
    //判断年月日是否有效
    int strYear = [[self getStringWithRange:carid Value1:6 Value2:4] intValue];
    int strMonth = [[self getStringWithRange:carid Value1:10 Value2:2] intValue];
    int strDay = [[self getStringWithRange:carid Value1:12 Value2:2] intValue];
    
    NSTimeZone *localZone = [NSTimeZone localTimeZone];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeZone:localZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",strYear,strMonth,strDay]];
    if (date == nil) return NO;
    const char *PaperId  = [carid UTF8String];
    //检验长度
    if( 18 != strlen(PaperId)) return -1;
    //校验数字
    for (int i=0; i<18; i++){
        if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i) ) return NO;
    }
    //验证最末的校验码
    for (int i=0; i<=16; i++){
        lSumQT += (PaperId[i]-48) * R[i];
    }
    if (sChecker[lSumQT%11] != PaperId[17] ) return NO;
    return YES;
}

相关文章

网友评论

      本文标题:身份证键盘与验证

      本文链接:https://www.haomeiwen.com/subject/rxypxftx.html