ios - 如何在 UIAlertController 中验证电子邮件地址?

标签 ios swift xcode uialertcontroller

我正在尝试设置此 UIAlertController,以便在输入正确的电子邮件地址之前“注册”按钮不会变为事件状态。 (包含@符号)。

关于如何做到这一点有什么建议吗?尝试了其他文章,但解决方案不起作用,这里是 Swift newb。

我已包含以下代码:

@IBAction func loginTapped(_ sender: UIButton) {
    //The user is not logged in, so prompt for their email address        
    let loginAlert = UIAlertController(title: "Sign Up For LivNao", message: 
                     "Please enter your email address to join the LivNao study", 
                     preferredStyle: UIAlertController.Style.alert)
    loginAlert.addAction(UIAlertAction(title: "Cancel", style: 
                                       UIAlertAction.Style.cancel, handler: nil))
    loginAlert.addAction(UIAlertAction(title: "Sign Up", style: 
                         UIAlertAction.Style.default, handler: { 
                                             (action: UIAlertAction) in
                                              self.handleLogin(loginAlert)
                         }))
    loginAlert.addTextField { (textField : UITextField) in
        textField.placeholder = "Enter email"
    }
    loginAlert.view.setNeedsLayout()
    self.present(loginAlert, animated: true, completion: nil)
}

最佳答案

看看这个。

var signUp: UIAlertAction!

override func viewDidLoad() {
   super.viewDidLoad()
   // Do any additional setup after loading the view, typically from a nib.

        let loginAlert = UIAlertController(title: "Sign Up For LivNao", message: "Please enter your email address to join the LivNao study", preferredStyle: UIAlertController.Style.alert)
        let cancel = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil)
        loginAlert.addAction(cancel)
        signUp = UIAlertAction(title: "Sign Up", style: UIAlertAction.Style.default, handler: { (action: UIAlertAction) in
            //self.handleLogin(loginAlert)
        })
        loginAlert.addAction(signUp)
        loginAlert.addTextField { (textField : UITextField) in
            textField.placeholder = "Enter email"
            textField.delegate = self
        }

        signUp.isEnabled = false
        loginAlert.view.setNeedsLayout()
        self.present(loginAlert, animated: true, completion: nil)

    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        signUp.isEnabled = isValidEmail(testStr: textField.text!) ? true : false
        return true;
    }

    func isValidEmail(testStr:String) -> Bool {
        let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"

        let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
        return emailTest.evaluate(with: testStr)
    } 

关于ios - 如何在 UIAlertController 中验证电子邮件地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56811078/

相关文章:

ios - presentViewController :animated:completion: causes my program to crash

ios - xib 和代码之间具有不同高度的 UIView

ios - 使用ARKIT检测人脸不同部分

ios - 我应该避免在Ensembles合并时保存到永久性存储中吗?

swift - 获取 CGFloat 类型的第一个数字

ios - 在不使用内置容器的情况下实现 View Controller 容器?

swift - 如何在swift中将字典添加到数组

ios - 列表中的重复条目

ios - 如何从 appdelegate 打开 View ?

ios - 以编程方式调整 Storyboard 中 UITableView 的 y 值