How to Test Character Input to UITextField as the User Enters Characters and Prevent Invalid Characters

前端 未结 6 1917
深忆病人
深忆病人 2020-12-02 16:22

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 i

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 16:50

    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.

    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;
    }
    

    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.

提交回复
热议问题