0

I would like to validate UITextField and display an UIAlertView if there is an error.

Previously I used the code below. It works perfectly with 2 UITexfields but with many UITextfield it's not possible.

Any tips how to display an UIAlertView to validate my UITexfields ?

- (void)processFieldEntries {

    NSString *usernameString = usernameField.text;
    NSString *passwordString = passwordField.text;
    NSString *firstNameString = firstNameField.text;
    NSString *nameString = nameField.text;
    NSString *cityString = cityField.text;

    NSString *errorText = @"Enter";
    NSString *usernameBlankText = @" a username";
    NSString *passwordBlankText = @" a password";
    NSString *prenomBlankText = @" a firstname";
    NSString *nomBlankText = @" a name";
    NSString *joinText = @", ";

    BOOL textError = NO;

    if (identifiantString.length == 0 || motDePasseString.length == 0 || prenomString.length == 0 || nomString.length == 0 || idPoubelle.length == 0) {
        textError = YES;

        if (usernameString.length == 0) {
            errorText = [errorText stringByAppendingString:usernameBlankText];
        }

        if (usernameString.length == 0 || passwordString.length == 0) {
            if (usernameString.length == 0) { 
                errorText = [errorText stringByAppendingString:joinText];
            }
            errorText = [errorText stringByAppendingString:passwordBlankText];
        }
    } 

    if (textError) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:errorText message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alertView show];
        return;
    }
}
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Michel
  • 100
  • 10

2 Answers2

0

I would do something like this:

- (void)processFieldEntries {
    NSString *usernameString = usernameField.text;
    NSString *passwordString = passwordField.text;
    NSString *firstNameString = firstNameField.text;
    NSString *nameString = nameField.text;
    NSString *cityString = cityField.text;

    NSMutableArray *errors = [NSMutableArray array];

    if (usernameString.length == 0) {
        [errors addObject:@"a username"];
    }

    if (passwordString.length == 0) {
        [errors addObject:@"a password"];
    }

    if (firstNameString.length == 0) {
        [errors addObject:@"a first name"];
    }

    if (nameString.length == 0) {
        [errors addObject:@"a name"];
    }

    if (errors.count) {
        NSString *message = [NSString stringWithFormat:@"Enter %@", [errors componentsJoinedByString:@", "]];

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Errors" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
}

Of course this doesn't scale real well. Consider creating an array of text field and error messages. Then you can iterate the array instead of writing a long list of if statements.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

You can also do some additional checking as the user enters info, because, for example Name should not contain digits or other symbols other than letters.

A nice way you can do is to implement the UITextFieldDelegate protocol from the View Controller class.

Then set the tag property for each UITextField object.

The protocol has this useful method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

which is invoked each time you time something in any of the text boxes.

Then you can check:

switch (textfield.tag)
{
 case TEXT_FIELD_FOR_FIRST_NAME_TAG:
 {
  if (!validateFirstNameFunction) { /* Display some error */ }

  break;
 }

 case TEXT_FIELD_FOR_LAST_NAME_TAG:
 {
  if (!validateLastNameFunction) { /* Display some error */ }

  break;
 }

 // And so on...

}

Try to separate functionality, which will make code cleaner and more readable.

Hope it brings some value to you as well :)

ppalancica
  • 4,236
  • 4
  • 27
  • 42