2016-10-02 81 views
-1

我正在嘗試創建一個限制註冊以嚴格限制.edu註冊的函數。我如何實現一項功能,只要他們有.edu註冊,就可以讓任何大學的學生註冊。我有Facebook註冊功能,但在此之後,我想將用戶重定向到電子郵件確認屏幕。在iPhone App中如何添加.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._%+-][email protected][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

您是否只想確保點擊註冊按鈕時,在電子郵件文本字段中有一個以「.edu」結尾的文本? –

+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._%+-][email protected][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 
     } 

    } 

}