Anthony Rossello
Anthony Rossello

Reputation: 219

Check if multiple textfields are empty - iOS with Swift

So, I'm developing an app signUp screen. I'm trying to check each field on the signUp screen to see if it's empty, and if it is, display an error message in a label to the user. I've been using a chain of else-ifs

    if ((self.firstNameField.text?.isEmpty) != nil) {
        errorLabel.text = "first name missing"
        errorLabel.hidden = false
    }

    else if ((self.lastNameField.text?.isEmpty) != nil) {
        errorLabel.text = "last name missing"
        errorLabel.hidden = false
    }

    else if ((self.emailField.text?.isEmpty) != nil) {
        errorLabel.text = "email missing"
        errorLabel.hidden = false
    }

    else if ((self.passwordField.text?.isEmpty) != nil) {
        errorLabel.text = "password missing"
        errorLabel.hidden = false
    }

    else if ((self.confirmPasswordField.text?.isEmpty) != nil) {
        errorLabel.text = "password confirmation missing"
        errorLabel.hidden = false
    }

    else if (self.passwordField.text != self.confirmPasswordField.text) {
        errorLabel.text = "Passwords don't match, try again!"
        errorLabel.hidden = false
    } 
    //omitted what happens if there are no fields missing

Now, when I run the application with all the textfields empty, the errorLabel displays the message "first name missing". Putting in a first name and pressing the signup button does nothing. I want it to change to "last name missing", but it stays at "first name missing".

Upvotes: 5

Views: 5596

Answers (4)

HTU
HTU

Reputation: 1034

You can avoid if-else cases and achieve it with one line code with ternary conditional operators in swift.

firstNameField.text?.isEmpty == true ? showError(message: ""first name missing"") : isValid()
...
}

func showError(message : String){
 errorLabel.text = message
 errorLabel.hidden = false
}
func isValid(){
  // hide label or anything else you want
}

Upvotes: 1

Avinash Vaghasiya
Avinash Vaghasiya

Reputation: 364

Try this out:

if self.firstNameField.text! == "" {
   errorLabel.text = "first name missing"
   errorLabel.hidden = false
}
else if self.lastNameField.text! == "" {
  errorLabel.text = "last name missing"
  errorLabel.hidden = false
}
 else if self.emailField.text! == "" {
  errorLabel.text = "email missing"
  errorLabel.hidden = false
}
 else if self.passwordField.text! == "" {
   errorLabel.text = "password missing"
   errorLabel.hidden = false
}
 else if self.confirmPasswordField.text! == "" {
  errorLabel.text = "password confirmation missing"
  errorLabel.hidden = false
}
 else if !(self.passwordField.text! == self.confirmPasswordField.text!) {
  errorLabel.text = "Passwords don't match, try again!"
  errorLabel.hidden = false
}

Upvotes: 1

Jojodmo
Jojodmo

Reputation: 23596

The reason this is happening is because you are checking if self.field.text?.isEmpty != nil. You should be checking for (self.field.text?.isEmpty ?? true)

Essentially, you're trying to get the text in the field, and if there is no text, then nil is returned. By using field.text?, you are making the next variable you access nil based on whether field.text is nil. So, when there is no text, field.text == nil, doing field.text?.isEmpty will always return nil.

When there is text, field.text?.isEmpty will not be nil, and will always be false, but nil != false, so the statement will always return false.

To fix this, you should check

if(self.field.text?.isEmpty ?? true)

which essentially means

if((self.field.text?.isEmpty == nil ? true : self.field.text?.isEmpty))

Basically, this will return true if field.text == nil (which would make field.text?.isEmpty nil, making the result true due to the ?? operator), and will also return true if field.text != nil || field.text.isEmpty. It will only return false if self.field.text != nil && !self.field.text.isEmpty.

Another way to write this statement would be

if(self.field.text == nil || self.field.text!.isEmpty)

Upvotes: 10

Abhinav
Abhinav

Reputation: 38152

Try this out:

if self.firstNameField.text?.isEmpty {
     errorLabel.text = "first name missing"
     errorLabel.hidden = false
}

else if self.lastNameField.text?.isEmpty {
    errorLabel.text = "last name missing"
    errorLabel.hidden = false
}

else if self.emailField.text?.isEmpty {
    errorLabel.text = "email missing"
    errorLabel.hidden = false
}

else if self.passwordField.text?.isEmpty {
    errorLabel.text = "password missing"
    errorLabel.hidden = false
}

else if self.confirmPasswordField.text?.isEmpty {
    errorLabel.text = "password confirmation missing"
    errorLabel.hidden = false
}

else if (self.passwordField.text != self.confirmPasswordField.text) {
    errorLabel.text = "Passwords don't match, try again!"
    errorLabel.hidden = false
} 

Upvotes: 1

Related Questions