user5664514
user5664514

Reputation: 1

Checking to see if fields are empty isn't working

I seem to be having trouble with this code. Trying to run a check to make sure fields have text. If there is no text within the fields, it's supposed to show a HUD with the text "Email/Password Cannot be empty". It seems to be ignoring the check and goes straight to attempting to sign in. When I remove the forced unwrapping, it then stops and does the check but if there's text in the fields, then does not move forward to the HUD message "Signing in"

@IBAction func logInDidTapped(_ sender: Any) {
    guard let email = emailTxtField.text, !email.isEmpty,
    let password = passwordTxtField.text, !password.isEmpty else {
        SVProgressHUD.showError(withStatus: "Email/Password Can't Be Empty")
        return
    }
    SVProgressHUD.show(withStatus: "Signing In...")
}

Upvotes: 0

Views: 51

Answers (2)

Nithin Yell
Nithin Yell

Reputation: 103

You do not required to use guard because emailTxtField.text/passwordTxtField.text returns as "" if they are empty.

For precise results check the count of for a text.

@IBAction func logInDidTapped(_ sender: Any) {

    if let email = emailTxtField.text,
        let password = passwordTxtField.text {

        if email.count > 0 && password.count > 0 {

            SVProgressHUD.show(withStatus: "Signing In...")
        } else {

            SVProgressHUD.showError(withStatus: "Email/Password Can't Be Empty")
        }
    }
}

Upvotes: 0

AaoIi
AaoIi

Reputation: 8396

Make your check as simple as the following to check if the UITextfield has text or not:

if emailTxtField.text == "" || passwordTxtField.text == "" {
    SVProgressHUD.showError(withStatus: "Email/Password Can't Be Empty")
    return
}

SVProgressHUD.show(withStatus: "Signing In...")

Hope this helps you out.

Upvotes: 1

Related Questions