iPhone Form Submission Validation: UITextField Value Error Checking Required Fields

119

An iPhone Application we recently produced had a order form on it. We needed to create a function that would run through all our UItextFields, extract their values, loop through, alert the user if a UITextField wasn’t completed, and execute a function (our ajax request) if all checks out.

What ‘s involved in this function

  • alertview – a pop up that lets the user know they missed a required field
  • array – a list of all the UITextField values
  • int – a variable that increment everytime time a UITextField Checks out
  • for loop – Loops through the Array
  • if statement – Check the array count to the increment count

This function assumes you know a bit about the iphone, have declared and linked your IBOutlet UITextFields, and have linked your submit button in the Interface Builder to  -(void)textFieldCheck:(id) sender; (which would be declared in your header file).

-(void)textFieldCheck:(id) sender {
        // Declare your Alert,  NSArray, increment int
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please Fill in All Required Fields." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        NSArray *fieldArray;
        int i = 0;

        // Load up our field array with the UITextField Values
        fieldArray = [[NSArray arrayWithObjects:
                  [NSString stringWithFormat:@"%@",orderName.text],
                  [NSString stringWithFormat:@"%@",orderPhone.text],
                  [NSString stringWithFormat:@"%@",orderAddress.text],
                  [NSString stringWithFormat:@"%@",orderEmail.text],
                  [NSString stringWithFormat:@"%@",orderQty.text], nil] retain];

        // loop through the array, alert if text field is empty, and break the the loop, other wise increment i  
        for (NSString *fieldText in fieldArray){
                NSLog(fieldText); // make sure all is reading correctly in the console
                if([fieldText isEqualToString:@""]){
                        [alert show];
                        break; // break out!!
                }
                i++;
        }
       
        // check that all the field were passed (i == array.count) if so execute
        if(i == [[NSNumber numberWithInt: fieldArray.count] intValue]){
                NSLog(@"Its all Good"); // some console fun
                [self sendOrder]; //where you execute your request function            
        }
       
        // release it all
        [alert release];
        [fieldArray release];
}



3 comments ↓

If you found this post useful click the share this button. Contribute below by adding a comment, no registration is required.

  • Brian says on 01.16.10 at 10:27 pm comment #1

    I am trying to call this within a IBAction. How do I call it correctly?

    - (IBAction)addAction:(id)sender
    {
    // The add button was clicked, handle it here
    NSLog(@”UIButton was clicked”);

    [self textFieldCheck];
    }

  • kiwijohn says on 06.02.10 at 8:54 pm comment #2

    where do you put this exit to ensure it is executed regardless of how user crashes out of screen?

  • Brandt says on 02.16.11 at 11:16 pm comment #3

    Local declaration of ‘i’ hides instance variable.

Leave a Comment