Chris
Chris

Reputation: 387

Swift 3: Determining if an email domain is valid with Firebase

I'm building a user registration that connects to firebase. I am unable to get firebase to discern if an email domain is valid or not so I want to provide an array of valid well known email domains which users can have to register for my app. I want to error handle for the occurence of an invalid email domain, so I need to be able to compare the end of the email the user entered with the array of valid emails I will allow. How can I check to confirm that ex: '[email protected]' is valid but ex: '[email protected]' is not valid?

    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")

Upvotes: 1

Views: 884

Answers (1)

Muhammad Hassan
Muhammad Hassan

Reputation: 1047

One of the way you can do this:

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

let emailTextBlockText = "[email protected]"

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

Upvotes: 4

Related Questions