Search code examples
iosobjective-ciphoneios7uitextfield

UITextField shouldChangeCharactersInRange


I am implementing a textfield to input user passcode. I am trying to match the length of stored passcode with user entered passcode. When the length matches i try to validate passcode. If the passcode does not match or length gets greater than stored value then i try to clear the textfield (the else-if) but the last entered character still stays in text field.

Please propose what i am doing wrong or what is the proper way?

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
            enteredPasscode = [enteredPasscode stringByAppendingString:string];
            if(enteredPasscode.length == [[self.passcodeFromDB stringValue] length])
            {
                [self performMatch:enteredPasscode];
            }
            else if(enteredPasscode.length > [[self.passcodeFromDB stringValue] length]){
                self.passcodeField.text = @"";
                textField.text = @"";
                enteredPasscode = @"";
            }
        return YES;
    }

Solution

  • you need to return NO; when you set everything to @"". I just checked and it works for me