I was stuck while validating the login form. When the user enters the empty email or just put the password in, it shows me the email field is required. If both are empty it shows the email and password are required, but it's not working fine in my case. I've attached the function of validation, kindly check the code. Thanks.
func loginController(){
if let email = email.text, let password = password.text{
if email.isEmpty{
let alert = UIAlertController(title: "Alert", message: "Email is empty", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Continue", style: UIAlertAction.Style.default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else{
if !email.validateEmailId(){
let alert = UIAlertController(title: "Alert", message: "Email is not valid", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Continue", style: UIAlertAction.Style.default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else if !password.validatePassword(){
let alert = UIAlertController(title: "Alert", message: "Password is not valid", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Continue", style: UIAlertAction.Style.default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else {
//navigation home screns
}
}
}
else{
let alert = UIAlertController(title: "Alert", message: "details is not valid", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Continue", style: UIAlertAction.Style.default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}