在iPhone App中如何添加.edu邮箱验证?
In iPhone App How to Add an .edu Email verfication?
我正在尝试创建一个将注册限制为严格 .edu 注册的功能。我如何实现允许任何大学生注册的功能,只要他们有 .edu 注册。我有 Facebook 注册功能,但在此之后,我想将用户重定向到电子邮件确认屏幕。
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
}
}
您可以使用正则表达式来检查它是否是以 .edu 结尾的有效电子邮件,您可以使用这样的辅助函数:
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
}
}
然后在上面的 viewcontroller 上下文中使用它 - 您可以执行以下操作:
//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
}
}
}
我正在尝试创建一个将注册限制为严格 .edu 注册的功能。我如何实现允许任何大学生注册的功能,只要他们有 .edu 注册。我有 Facebook 注册功能,但在此之后,我想将用户重定向到电子邮件确认屏幕。
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
}
}
您可以使用正则表达式来检查它是否是以 .edu 结尾的有效电子邮件,您可以使用这样的辅助函数:
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
}
}
然后在上面的 viewcontroller 上下文中使用它 - 您可以执行以下操作:
//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
}
}
}