iOS/iOS Swift 앱만들기 A부터 Z까지

iOS Swift 이메일 & 비밀번호 유효성 검사

Developer-Michelle 2023. 2. 15. 10:53

이메일 & 비밀번호 유효성 검사

func login(user: String, pass: String) {
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
        let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
        let emailValid: Bool = emailTest.evaluate(with: user)
        let passValid: Bool = (pass != "" && pass.count >= 6)
        
        if (emailValid && passValid) {
            let foodListVC = self.storyboard?.instantiateViewController(identifier: "MainViewController") as! MainViewController
            self.navigationController?.pushViewController(foodListVC, animated: true)
        } else {
            let alert = UIAlertController(title: "Wrong Credentials", message: "이메일과 비밀번호를 다시 확인해주세요.", preferredStyle: UIAlertController.Style.alert)
            let okAction = UIAlertAction(title: "OK", style: .default, handler : nil )
            alert.addAction(okAction)
            present(alert, animated: true, completion: nil)
        }
    }