如何实时检查文本字段输入?

时间:2011-12-13 03:26:22

标签: ios cocoa-touch uitextfield

我正在为我的文本域编写验证,我发现了一些有趣的东西,我是否可以检查我在文本字段中实时输入了多少位数。我的文本字段输入必须是8位数字。因此,当我达到8位时,我想将文本字段内的文本更改为绿色,而当不是时,我想更改颜色。

我该怎么做?请帮助我,提前谢谢。

7 个答案:

答案 0 :(得分:25)

使用-textField:shouldChangeCharactersInRange:replacementString:可能是一个糟糕的解决方案,因为它会在文本字段更新之前触发。当您想要在键盘自动更新文本字段之前更改文本字段的文本时,可能应该使用此方法。在这里,您可能只想在编辑值更改时使用目标 - 操作配对:

[textField addTarget:self action:@selector(checkTextField:) forControlEvents:UIControlEventEditingChanged];

然后,在- (void)checkTextField:(id)sender中,试试这个:

UITextField *textField = (UITextField *)sender;
if ([textField.text length] == 8) {
    textField.textColor = [UIColor greenColor]; // No cargo-culting please, this color is very ugly...
} else {
    textField.textColor = [UIColor blackColor];
    /* Must be done in case the user deletes a key after adding 8 digits,
       or adds a ninth digit */
}

答案 1 :(得分:4)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

//    [textField2 setText:[textField1.text stringByReplacingCharactersInRange:range withString:string]];
    NSLog(@"%@",searchStr);
    return YES;
}

答案 2 :(得分:3)

使用UITextFieldDelegate。特别是这个功能

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

您可以从here ..

获取示例代码链接

答案 3 :(得分:2)

[textField addTarget:self action:@selector(checkTextField) forControlEvents:UIControlEventEditingChanged];

试试吧!

答案 4 :(得分:1)

UITextField设置委托并实施方法– textField:shouldChangeCharactersInRange:replacementString:详细信息和示例位于UITextFieldDelegate Protocol Reference,但这是一个简单的示例:

- (BOOL)textField:(UITextField *)textField 
shouldChangeCharactersInRange:(NSRange)range 
replacementString:(NSString *)string
{
        NSString *text = [textField text];
        // NOT BACKSPACE
        if ([string length] || text.length + string.length < 8) {
            return YES;
        } else if (text.length + string.length > 8) {
            return NO;
        } else {
                // DO SOMETHING FOR LENGTH == 8
                return YES;
        }
}   

答案 5 :(得分:0)

更新Swift 4.0

 txtFieldAdd.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

现在打电话给你的功能。在我的情况下,这将启用保存按钮

@objc func textFieldDidChange(_ textField: UITextField) {
    if (txtFieldAdd.text?.isEmpty)! {
        btnSave.isEnabled = false
    }
    else {
        btnSave.isEnabled = true
    }
}

希望它有所帮助。

答案 6 :(得分:0)

这是无需设置文本字段的委托即可获得实时验证的方式:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    // Creating the future string. The incoming string can be a single character,
    // empty (on delete) or many characters when the user is pasting text.

    let futureString: String = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)

    // Determine if the change is allowed, update form, etc.
    let allowChange = <#Your Validation Code#>

    return allowChange
}

将此代码添加到文本字段的指定委托中。很简单。