美文网首页&iOS
UITextField文字字数检验方法

UITextField文字字数检验方法

作者: 小僧有礼了 | 来源:发表于2019-02-15 16:25 被阅读0次

    邮箱校验

     NSString *emailRegex = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@._-%+-";
     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
     if ([emailTest evaluateWithObject:textField.text]) {
           
     }else{
           
     }
    
    或者
     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
     if ([emailTest evaluateWithObject:textField.text]) {
           
     }else{
           
     }
    

    中文检验

        NSString *regex =@"[\u4e00-\u9fa5]+";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    

    只允许输入字母数字

        NSString *regex =@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    

    中文输入时字数限制

    添加监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
    实现方法
    - (void)textChange:(NSNotification*)noti {
    
    // 拿到文本改变的 text field
    UITextField *textField = (UITextField *)noti.object;
    
    // 需要限制的长度
    NSUInteger maxLength = 0;
    if (textField == _companyTextField) {
        maxLength = 20;
        
    }
    
    if (textField == _jobTextField) {
        maxLength = 10;
    }
    if (maxLength == 0) return;
    
    // text field 的内容
    NSString *contentText = textField.text;
    
    // 获取高亮内容的范围
    UITextRange *selectedRange = [textField markedTextRange];
    // 这行代码 可以认为是 获取高亮内容的长度
    NSInteger markedTextLength = [textField offsetFromPosition:selectedRange.start toPosition:selectedRange.end];
    // 没有高亮内容时,对已输入的文字进行操作
    if (markedTextLength == 0) {
        // 如果 text field 的内容长度大于我们限制的内容长度
        if (contentText.length > maxLength) {
            // 截取从前面开始maxLength长度的字符串
            //            textField.text = [contentText substringToIndex:maxLength];
            // 此方法用于在字符串的一个range范围内,返回此range范围内完整的字符串的range
            NSRange rangeRange = [contentText rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
            textField.text = [contentText substringWithRange:rangeRange];
        }
    }
    }
    
    移除监听
    -(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    

    字数检验

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSMutableString *newtext = [NSMutableString stringWithString:textField.text];
    [newtext replaceCharactersInRange:range withString:string];
    
    
    /*
    if (textField == _companyTextField) {
        if ([newtext length] > 20) {
            EPShowSuccess(@"公司名称不能超过20字");
            return NO;
        }
    }
    
    if (textField == _jobTextField) {
        if ([newtext length] > 10) {
            EPShowSuccess(@"职位名称不能超过10字");
            return NO;
        }
    }
    
     */
    return YES;
    }
    

    谓词校验

    NSPredicate使用

    相关文章

      网友评论

        本文标题:UITextField文字字数检验方法

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