Search code examples
iosobjective-cuitextfielduialertviewuialertviewdelegate

Validating UITextField and Display an UIAlertView if error


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

Solution

  • 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.