Droidme
Droidme

Reputation: 1252

iOS text validation in story board for 10+ textfields

I have a form with more than 10 text fields. I want to check if all are entered. I know the following way to check the same

if ([_fName.text isEqualToString:@""])

Is it the best way even if we have more than 10 text fields? or is there any simpler way?like assign something in storyboard or something?

Upvotes: 0

Views: 508

Answers (3)

Wizkid
Wizkid

Reputation: 1035

I suggest you do something like below. Note, you will probably want to do more than validate if something exists in the fields. You might want to check validity of dates, numbers, etc as well.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    // assuming you have a save bar button or equivalent
    // disable it or change it from 'Edit' to 'Save' when you start changing textFields
    self.navigationItem.rightBarButtonItem.enabled = NO;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{   
    // validate form
    if (_textField1.text.length > 0 &&
        _textField2.text.length > 0 &&
        _textField3.text.length > 0 &&
        _textField4.text.length > 0 &&
        _textField5.text.length > 0)
    {
        // Re-enable your bar button or change it from 'Edit' to 'Save' 
        // if form validates
        self.navigationItem.rightBarButtonItem.enabled = YES;

    }
}

Upvotes: 0

Keenle
Keenle

Reputation: 12220

You can conform your view controller to <UITextFieldDelegate> protocol and implement:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text isEqualToString:@""]) {
        // your code
    }
}

In addition your can check which exactly textField was changed and apply custom validation logic:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self._fName) {
        // your code
    }
}

Upvotes: 1

iBhavin
iBhavin

Reputation: 1261

You can use textFieldDidEndEditing for tracking text:

- (void)textFieldDidEndEditing:(UITextField *)textField {

    switch (textField.tag) {
          case NameFieldTag:
             // do something with this text field

          break;

          case EmailFieldTag:
             // do something with this text field

          break;

    }
}

Upvotes: 0

Related Questions