Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField input validation in iOS 7

What is the approppriate way in iOS 7 to deal with text input field validation? Is there an alternative to showing the dialog?

I do not have much experience with iOS apps (I'm more of an android fan) and I would like it to be similar to android's:

mEditText.setError("Error string");

Closest thing I found is this library, but I would like to know if there is something native in iOS (if possible, I would like to avoid using 3rd party libs).

Thanks in advance.

like image 845
micromanc3r Avatar asked Oct 14 '13 13:10

micromanc3r


2 Answers

I'm not aware of something as simple as the android example you mention. But once you have determined that a value is invalid as per @Herre_84, you have a few things you can do that don't involve bringing up an alert dialog.

A common thing to do is to use the UITextField's overlay views to indicate that the value is in error. There are overlay views available to you on the left and the right sides of the UITextField. You can put images in them that indicate the valid status of the input as in the answer to this question. More details are in the official Apple documentation.

Another less common thing you can do is use the attributedText property of UITextField to indicate that the text is not valid. For example, you could make incorrect text a different color. TSValidatedTextField demonstrates how to do that.

Finally, you could use a completely separate view (perhaps appearing near your UITextField) that you update with validity information. You'd have to wire up the two views, probably via a shared parent view controller, to effect the communication between them.

like image 90
Travelling Man Avatar answered Oct 02 '22 08:10

Travelling Man


There is no standard or specific "iOS7" way of validating text. You could however implement your own "validation framework" (similar to the one you are pointing to in the link you added) by using UITextfield's own delegate methods like

  • - (void)textFieldDidEndEditing:(UITextField *)textField, which indicates that the user is done editing (eg when he dismissed the keyboard or pressed the "Done" button)
  • - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string, which is triggered when when a new character is entered in the UITextField.

Based on these events, you could evaluate the current NSString in the UITextField using it's text property, and adapt your UI accordingly.

like image 36
Herre Avatar answered Oct 02 '22 08:10

Herre