Swift 3:确定电子邮件域是否对Firebase有效

时间:2017-07-20 07:41:34

标签: swift firebase firebase-realtime-database

我正在构建一个连接到firebase的用户注册。我无法让firebase看出电子邮件域是否有效,所以我想提供一系列有效的知名电子邮件域,用户必须注册我的应用程序。我想错误处理无效电子邮件域的出现,所以我需要能够将用户输入的电子邮件的末尾与我允许的有效电子邮件数组进行比较。如何确认ex:'apples@gmail.com'是有效但ex:'apples@gnail.com'无效?

    let emails: Array = ["gmail.com", "yahoo.com", "comcast.net", "hotmail.com", "msn.com", "verizon.net"]



@IBAction func nextBtnPressed(_ sender: Any) {
        let ref: DatabaseReference!
        ref = Database.database().reference()
        if let email = emailTextField.text, let pwd = passwordTextField.text, let firstName = firstNameTextField.text, let lastName = lastNameTextField.text, let dob = birthdayTextField.text {
            if pwd != self.reEnterPassTextField.text {
                errorMessageLbl.text = "Passwords do not match"
                errorMessageLbl.isHidden = false
                return
            } else if firstName == "" || lastName == "" || dob == ""{
                errorMessageLbl.text = "Cannot leave fields blank"
                errorMessageLbl.isHidden = false
                return
            } else if email.characters.elementsEqual([emails]) {
                print("Failure")

1 个答案:

答案 0 :(得分:1)

其中一种方法可以做到这一点:

let validDomains = ["gmail.com", "yahoo.com", "comcast.net", "hotmail.com", "msn.com", "verizon.net"]

let emailTextBlockText = "example@gmail.com"

if let domain = emailTextBlockText.components(separatedBy: "@").last, validDomains.contains(domain) {
    // Entered email has valid domain.
}