How to check whether the UITextfield is empty string

| | August 8, 2015

I’m trying to build a login page for an app. Something went wrong when testing the login condition.
There are two textfields, username and password. When the Login button is clicked, alert shows when

1) either fields are empty
2) both filled.

The code reflects this below. But the result came out no matter that the textfield is empty or not, it only showed as textfields were filled ( only the else part run). So I assume there was something wrong with the if condition. But when I checked the if condition in other view, it worked.

Can anyone help?

if ([_pLoginIDField.text isEqualToString:@""] || [_pPasswordField.text isEqualToString:@""]) {

    UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Yeah !" message:@"must complete all fields" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [error show];
}
else {
    UIAlertView *error2 = [[UIAlertView alloc] initWithTitle:@"Oooops !" message:@"hhhhhhhhhh" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [error2 show];
}

One Response to “How to check whether the UITextfield is empty string”

  1. Try this:

    if (_pLoginIDField.text.length > 0 || _pPasswordField.text.length > 0)
    

Leave a Reply