Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField Validation visual feedback

i have a uiTextField that i am validating input on, when i have invalid input what are some appropriate ways to show that the input is invalid? is there any built in mechanism for showing invalid input states?

like image 944
Aran Mulholland Avatar asked Dec 15 '09 11:12

Aran Mulholland


2 Answers

It's pretty easy to add an 'warning' image to the left-hand side of a UITextField to indicate that the field needs a value.

Try this:

UITextField* field = .... your text field ...

if ( fails_validation ) {
  field.leftViewMode = UITextFieldViewModeAlways;
  UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
  imageView.image = [UIImage imageNamed:@"warning.png"];
  imageView.contentMode = UIViewContentModeScaleAspectFit;
  field.leftView = imageView;
} else {
  field.leftViewMode = UITextFieldViewModeNever;
  field.leftView = nil;
}
like image 73
Ben Clayton Avatar answered Oct 23 '22 05:10

Ben Clayton


The best two options I've found so far are TextFieldValidator and US2FormValidator. With the caveat that I have only used the former, here's my take on each.

TextFieldValidator

  • ↑ Simple (just one class!)
  • ↑ Offers default UI with additional customization possible
  • ↑ Handles multiple validations on a single field
  • ↓ Doesn't handle text views
  • ↓ Slightly awkward name…ValidatedTextField, for example, would be more accurate :)

US2FormValidator

  • ↑ Handles text fields and text views
  • ↑ Importable as a framework, including CocoaPods support
  • ↑ Handles multiple validations on a single field
  • ↓ No default UI

If you just need something implemented quickly, TextFieldValidator may be all you need. If you must have validated UITextViews, US2FormValidator is the way to go.

like image 25
clozach Avatar answered Oct 23 '22 05:10

clozach