5

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

4
  • 1
    So you believe that (self.firstNameField.text?.isEmpty) != nil will somehow be false???? Commented Oct 20, 2015 at 4:21
  • Where do you call this validation code? It should be consistently called whenever you change any of the text fields or tap signup Commented Oct 20, 2015 at 4:21
  • In Swift 2 I would do: if firstNameField.text.character.count == 0 { .. Commented Oct 20, 2015 at 4:22
  • Also: have you heard about loops? Commented Oct 20, 2015 at 4:23

4 Answers 4

10

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)
Sign up to request clarification or add additional context in comments.

1 Comment

No problem! If you want to mark this answer as the correct one, you can click the check mark under the vote count
1

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
} 

Comments

1

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
}

Comments

1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.