iOS Legend
iOS Legend

Reputation: 27

how to show alert message if both user name and password is empty

how to show alert message...if both user name and password is empty..

fist two are working well...If email textfield is empty... it shows alert message as please enter your email id...and password also it works... But, if both user name and password is empty.. it shows message like...please enter your email id

    if textFieldEmailOutlet.text == "" {
        GenericFunctions.showAlert(title: "Alert", message: "Please enter your email id")
    }else if textFieldPasswordOutlet.text == ""{
        GenericFunctions.showAlert(title: "Alert", message: "Please enter your password")
    }else if (self.textFieldEmailOutlet.text == "") || (self.textFieldPasswordOutlet.text == ""){
        GenericFunctions.showAlert(title: "Alert", message: "Please enter your email id & password")
    }

please help..

Upvotes: 0

Views: 894

Answers (1)

CTheCheese
CTheCheese

Reputation: 362

Your logic is reversed. Once it hits the first if statement it holds true because the e-mail IS missing. You need to reverse your statements to check for both first. You've also set it to check if either e-mail or password is blank.

if (self.textFieldEmailOutlet.text == "") && (self.textFieldPasswordOutlet.text == ""){
        GenericFunctions.showAlert(title: "Alert", message: "Please enter your email id & password")
    }else if textFieldPasswordOutlet.text == ""{
        GenericFunctions.showAlert(title: "Alert", message: "Please enter your password")
    }else if textFieldEmailOutlet.text == ""{
        GenericFunctions.showAlert(title: "Alert", message: "Please enter your email id")
    }

This checks if the e-mail AND password are missing, and gives the result you were looking for. If either one of them is filled out, however, it will fail the first if clause and check to see if there is a password. If there is a password it will move on to check the e-mail address where it would (logically) fail and send the alert about needing an e-mail address.

Upvotes: 1

Related Questions