Entries Tagged 'UITextField' ↓

iPhone Form Submission Validation: UITextField Value Error Checking Required Fields

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];
}

Compare UITextField Input to String (iPhone Development)

When dealing with user form, I was looking for the best way to form check. Coming from a web background, I’m used to doing it with javascript/ajax requests. In an if elseif statement that activate a button with all input field are not !== “”

To access the inputed iphone text from a uitext field:

myUITextField.text

To compare that input text to a string

[myUITextField.text isEqualToString:@"This String"]

I’m starting to fall in love with apples objective-c in the xcode environment, Apple got it right.

When the the form checking is complete we will post the code.