May Yang May Yang - 1 year ago 60
Objective-C Question

Require a UITextField to take 5 digits

I have a UITextField set up to show a number pad. How do I require the user to enter exactly 5 digits?

I poked around and saw I should use

but I'm not quite understanding how to implement that.

Zil Zil
Answer Source

I'd just use this when the user leaves the textfield/validates with a button

if ([myTextField.text length] != 5){

//Show alert or some other warning, like a red text

 //Authorized text, proceed with whatever you are doing

Now if you want to count the chars WHILE the user is typing, you might use in viewDidLoad

[myTextfield addTarget: self action@selector(textfieldDidChange:) forControlEvents:UIControlEventsEditingChanged]

 //This happens every time the textfield changes

Please make sure to ask questions in the comments if you need more help :)