Validation in Login form not working perfectly in Swift

Viewed 435

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)
        }
    }
    
1 Answers

I've created a function to check each field if it's empty or not

func checkFields (Message: String) -> Bool {
        if textFieldEmailOutlet.text != "" && textFieldPasswordOutlet.text != "" {
            if isValidEmail(email: textFieldEmailOutlet.text!) && isValidPassword(password: textFieldPasswordOutlet.text!) {
                ProgressHUD.showSuccess(Message)
                return true
            }
            else {
                ProgressHUD.showError("Enter correct Email or Correct password format")
                return false
            }
        }
        else {
            ProgressHUD.showError("All fields are mandatory")
            return false

        }
    }

I'm using also two functions to check on email and password with specific regex:

//MARK: - Validation
    
    func isValidEmail(email: String) -> Bool {
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
        
        let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
        return emailPred.evaluate(with: email)
    }
    
    func isValidPassword(password: String) -> Bool {
        let passRegEx = "^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{8,64}$"
        
        let passPred = NSPredicate(format:"SELF MATCHES %@", passRegEx)
        return passPred.evaluate(with: password)
    }

Don't forget to import pod ProgressHUD, it will save time and effort

Related