如何在用户输入字符时测试 UITextField 的字符输入并防止无效字符 [英] How to Test Character Input to UITextField as the User Enters Characters and Prevent Invalid Characters

查看:27
本文介绍了如何在用户输入字符时测试 UITextField 的字符输入并防止无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为 UITextField 设置键盘以使用十进制样式的数字.所以用户只能输入数字和一个小数.

First, I setup up the keyboard for the UITextField to use the number with decimal style. So the user can only enter numbers and a single decimal.

我想做的是在用户输入时测试输入并防止输入多个小数并将数字的小数部分限制为两位.我不想四舍五入这个数字,也不想将输入视为数字.我只是想防止用户在小数点右侧输入超过两位数.

What I want to do is test the input as the user enters it and prevent multiple decimals from being entered and limit the decimal portion of the number to two places. I do not want to round off the number nor even treat the input as a number. I simply want to prevent the user from entering more then two digits to the right of the decimal place.

推荐答案

该解决方案最终被证明是相当微不足道的.不幸的是,与此问题相关的许多问题和答案都是关于验证或格式化数值,而不是控制用户可以输入的内容.

The solution ultimately turned out to be fairly trivial. Unfortunately a lot of the questions and answers related to this question are about validating or formatting numeric values, not controlling what a user could input.

shouldChangeCharactersInRange 委托方法的以下实现是我的解决方案.与往常一样,正则表达式在这种情况下会摇摆不定.RegExLib.com 是有用的 RegEx 示例或解决方案的绝佳来源.我不是 RegEx 专家,总是很难将它们组合在一起,因此欢迎提出任何改进建议.

The following implementation of the shouldChangeCharactersInRange delegate method is my solution. As always, regular expressions rock in this situation. RegExLib.com is an excellent source for useful RegEx samples or solutions. I'm not a RegEx guru and always struggle a bit putting them together so any suggestions to improve it are welcome.

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

        NSString *expression = @"^([0-9]+)?(\.([0-9]{1,2})?)?$";

        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression 
                                                                               options:NSRegularExpressionCaseInsensitive 
                                                                                 error:nil];
        NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                            options:0
                                                              range:NSMakeRange(0, [newString length])];        
        if (numberOfMatches == 0)
            return NO;        
    }

    return YES;
}

上面的代码允许用户输入以下类型的值:1、1.1、1.11、.1、.11.请注意,此实现不会替换文本字段的字符串,这会导致递归.如果newString"与正则表达式不匹配,此解决方案只会拒绝用户输入的下一个字符.

The above code allows the user to input these kinds of values: 1, 1.1, 1.11, .1, .11. Notice that this implementation does not replace the text field's string, which would causes a recursion. This solution simply rejects the next character the user inputs if the 'newString' does not match the regex.

这篇关于如何在用户输入字符时测试 UITextField 的字符输入并防止无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆