Joshua Hollis Joshua Hollis - 2 months ago 8
iOS Question

In iPhone App How to Add an .edu Email verfication?

I am trying to create a function that restricts sign up to strictly .edu sign up. How could I implement a function that would allow any University student to sign up as long as they have a .edu sign up. I have a Facebook registration function but after this, I want to redirect the users to an email confirmation screen.

func validate(email: String) -> Bool { do {
//instantiate a regex checking for valid email ending in .edu
let regex = try NSRegularExpression(pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.+-]+\.edu$", options: NSRegularExpression.Options.caseInsensitive)
//get the number of matches
let matches = regex.matches(in: email, options: [], range: NSRange(location: 0, length: email.characters.count))
//if no matches, then email provided does not meet your requirements - otherwise the email validates
return matches.count > 0
} catch { //regular expression init failed...
return false
}
}

//conform EmailConfirmationVC UITextFieldDelegate class EmailConfirmationVC: BaseTableVC, UITextFieldDelegate {

//set up your IBOutlets and other properties as you have already, avatar, nameValue, gener, etc -
@IBOutlet weak var emailTF: UITextField!


override func viewDidLoad() {
super.viewDidLoad()

//set your email textfield delegate to self
emailTF.delegate = self

//the rest of your viewDidLoad...

}

//add the validate func above as a method of this class
func validate(email: String) -> Bool { //etc
}

//then implement the textFieldDidEndEditing
func textFieldDidEndEditing(_ textField: UITextField) {
//for now, we are only interested in your email field - so if its not, return early
if textField != emailTF { return }
//else, safely unwrap and use the text from the field
guard let userEmail = textField.text else { return }
//then you the validate method to verify the email checks out
let validEduEmail = validate(email: userEmail)

if validEduEmail {
//email is a valid .edu email -> move on to next registration steps, etc
} else {
//otherwise - this is not an .edu email - let the user know somehow
}

}

Answer

You could use a regular expression to check that it is a valid email ending in .edu, you could use a helper function like this:

    func validate(email: String) -> Bool {

        do {
            //instantiate a regex checking for valid email ending in .edu
            let regex = try NSRegularExpression(pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.+-]+\\.edu$", options: NSRegularExpression.Options.caseInsensitive)
            //get the number of matches
            let matches = regex.matches(in: email, options: [], range: NSRange(location: 0, length: email.characters.count))
            //if no matches, then email provided does not meet your requirements - otherwise the email validates
            return matches.count > 0
        } catch {
            //regular expression init failed... 
            return false
        }

    }

To then use this in the context of your viewcontroller above - you could do something like the following:

//conform EmailConfirmationVC UITextFieldDelegate
class EmailConfirmationVC: BaseTableVC, UITextFieldDelegate {

    //set up your IBOutlets and other properties as you have already, avatar, nameValue, gener, etc -
    @IBOutlet weak var emailTF: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()

        //set your email textfield delegate to self
        emailTF.delegate = self

        //the rest of your viewDidLoad...

    }

    //add the validate func above as a method of this class
    func validate(email: String) -> Bool { //etc 
    }

    //then implement the textFieldDidEndEditing
    func textFieldDidEndEditing(_ textField: UITextField) {
        //for now, we are only interested in your email field - so if its not, return early
        if textField != emailTF { return }
        //else, safely unwrap and use the text from the field
        guard let userEmail = textField.text else { return }
        //then you the validate method to verify the email checks out
        let validEduEmail = validate(email: userEmail)

        if validEduEmail {
            //email is a valid .edu email -> move on to next registration steps, etc
        } else {
            //otherwise - this is not an .edu email - let the user know somehow
        }

    }

}