Jackson Wang
Jackson Wang

Reputation: 61

User input in textfield

I have three textfields where the user enter their info. If the info format is not correct I will display a warning message in red color, then let them type in the new info. I tried to use a while loop to wait for their new info then go through the same if statements. If their new info is correct then once the button is pressed, we go to another storyboard. It didn't work for some reason. Can you tell me what I can do to display the warning message then wait for new input without going to the second storyboard just yet?

Upvotes: 0

Views: 75

Answers (2)

Abhinav
Abhinav

Reputation: 38162

While loop? No way!

What you are looking for is implementation of one of the following UITextFieldDelegate methods:

To put validation up-front as user type a character:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

To put validation once user is done typing:

- (void)textFieldDidEndEditing:(UITextField *)textField
- (BOOL)textFieldShouldReturn:(UITextField *)textField

Go through this Apple Documentation to understand it in detail.

Upvotes: 2

Joseph Duty
Joseph Duty

Reputation: 795

sounds like this should solve your question: Listen to a value change of my text field Set a delegate for your field and have the delegate implement an onChange method where you validate the input.

Upvotes: 0

Related Questions