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

查看:99
本文介绍了如何在用户输入字符和防止无效字符时测试字符输入到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天全站免登陆