Apr 30 2011

Simple Validation-Framework for UITextField & UITextView -Fields

Published by at 5:26 PM under iPhone

Table of contents


Validation of GUI-Components on an iPhone/iPad (iDevice) is not a simple task. The most common (stupid) way is to use a specific validation-method for every field. This is much boilerplate code which i want to avoid. So i have written a simple validation framework only for the UITextField and UITextView -fields. Other components are following soon with a more advanced framework.

So, what can my simple validation framework do? First, take a look at these pictures (click on each for fullsize):

Some empty input fields

Some empty input fields

Now i insert some values and hit the “Validate”-Button.

Validation errors

Validation errors

You see a Popup-Window with a summary of the found validation errors. After click on ‘ok’ you see all fields with errors marked red.

Fields with validation errors are marked red

Fields with validation errors are marked red

How to use it

As the title of this posting said, it is very simple. First, drop the framework-class-files (header and implementation) into your project. These are:

  • ValidationModel
  • Validator
  • ValidatorTextFieldProtocol
  • MyColors
  • EnhancedTextField
  • EnhancedTextView

Then connect all of your UITextField and UITextView-fields with Outlets. After that you have to create for each of these fields a ValidationModel and store it into a NSMutableArray. You have to pass some parameters for the Validation. These are ‘Minimum char length‘, ‘Maximum char length‘ and ‘Empty field allowed‘. Additionally you can specify a special error message, shown when the field is empty, to tell the User why and what he has to enter.

- (void)viewDidLoad
  [super viewDidLoad];
  [self showTextView:self.view :comments];

  // Save all fields which you want to validate inside the 'propertiesList'
  propertiesList = [[NSMutableArray alloc] init];

  [propertiesList addObject:[[ValidationModel alloc] initWithValues:name :NO :@"You have to insert a very important value!" :2 :5]];
  [propertiesList addObject:[[ValidationModel alloc] initWithValues:street :YES :nil :0 :10]];
  [propertiesList addObject:[[ValidationModel alloc] initWithValues:comments :NO :nil :1 :5]];

Now connect the Button, which activates the validation (or saves the data), with an Action. In this method you will instantiate the Validation-Engine and call the Validation-Method with the (previously created) ValidationModels-List.

 * This method is called when the User hits the 'Validate'-Button. It now
 * validates the TextFields/TextViews, previously assigned in the 'viewDidLoad'.
- (IBAction)validateAction:(id)sender {
  // Initalize the Validator-Engine with standard error messages
  Validator *val = [[Validator alloc] initWithErrorMessages:@"You have to insert %@ chars as a minimum!" :@"You have insert to much chars. Maximum are %@ chars" :@"Empty field is not allowed!"];

  // Validate now! The return-value contains the error-messages with the corresponding fieldname
  NSMutableDictionary *resultErrorMap = [val validatePropertiesList:propertiesList];
  NSLog(@"*** Received Error messages: %i", [resultErrorMap count]);


That’s it! Your Validation-Framework is armed and ready for action! You can parse the ‘resultErrorMap‘ and show a splash screen with some infos about the errors (like in the first Screenshot above).


A working example for iOS 4.3 is available here (click)


This framework is very simple and easy to use. You can only validate the most common input-fields, but you have the ability to include other UITextField/UITextView-Implementations by using the ‘ValidatorTextFieldProtocol‘ (polymorphic stuff). I hope you can use it, drop a line in the comments what you think about it.

I am currently working on an advanced framework which uses plugins to customize the validation for the other GUI-Components of the UIKit-Framework. So stay tuned!

Technorati Tags: , , , , , , ,

3 responses so far

3 Responses to “Simple Validation-Framework for UITextField & UITextView -Fields”

  1. Bob sagt:

    Hi Siegrfried… How’s Roy? (I’m sorry!)

    I won’t use this framework but still, I wanted to take the time to tell you that you had a great idea. I’ve been working with asp.net for 8 years and now trying to also learn Objective-C and I have to say… That’s the kind of stuff missing!

    I was looking for an equivalent to the Asp.Net Field Validators (A control that wraps your inputs and that you can customize easily).

    There are some sorts of validators, for example:

    - Range Validator
    - Compare Validator
    - Regex Validator
    And a few others.

    If you think it could give you some ideas take a look at it here:

    - http://www.w3schools.com/aspnet/aspnet_refvalidationcontrols.asp
    - http://msdn.microsoft.com/en-us/library/aa479013.aspx
    - http://www.codeproject.com/Articles/67269/Exploring-ASP-NET-Validators

    While reading the last one, just keep in mind (if you don’t already know) that Ajax Control Toolkit is just used like an add-on but not required and is used to do client validation.

    So good luck with that!

  2. demosc sagt:

    Good project !!
    Please, put in github so the community can improve the code.

  3. There are a few more dsl combinators in hvac, but they all operate essentially the same. So now, this gets us partway there. However, it hasn’t addressed get/post params, cookies, sessions, databases, etc. at all yet (although there are combinators to match simple equality on get/post params too). Let’s add just get/post params for the next level of complexity. Now get/post params are used slightly differently, because they’re based generally on user input. So our criteria for them need to be different — there are optional fields that may not be filled in, “valid” values might not meet other criteria (too short, invalid dates, not a genuine email address etc.) and soforth. Furthermore we don’t generally want bad values to prevent matching a page, rather we want an error page that highlights *all* the errors. So we know we need another layer for this.