Search code examples
validationinputuitextfieldios7

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.


Solution

  • 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.