我正在尝试创建一个限制注册严格.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
}
}
答案 0 :(得分:0)
您可以使用正则表达式来检查它是否是以.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
}
}
}