1

I would like to ask if it is possible to have validation constraint on my text field.

For example, my application allows users to register as new member. They are required to set a new username and password. However, they are only allow to enter 6-10 digits of characters for username and password.

So i would like to know, how can i set this constraint on xcode? Thus, my application will validate the data that is entered and then return with an alert box or proceed to the next step.

Thank you so much in advance.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jasmine
  • 75
  • 1
  • 1
  • 6
  • possible duplicate of [iPhone SDK: Set Max Character length TextField](http://stackoverflow.com/questions/433337/iphone-sdk-set-max-character-length-textfield) – mmmmmm Dec 21 '11 at 13:57

2 Answers2

1

Have a look at the UITextFieldDelegate

You may perform validation using one or a combination of the delegate methods. Probably you want to use textFieldDidEndEditing:

Edit: To restrict the type or number of characters they may enter; look at textField:shouldChangeCharactersInRange:replacementString:

lorean
  • 2,150
  • 19
  • 25
1

Someone asked a similar question, the solution was by making your own function to solve the problem:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 25) ? NO : YES;
}
Community
  • 1
  • 1
Gabriel
  • 3,039
  • 6
  • 34
  • 44