2016-06-21 74 views
2

如何限制用戶在文本框中輸入電子郵件地址。 問題是,未顯示我的警惕,只是註冊,如果有效,而不檢查電子郵件字段或不電子郵件驗證swift 2

if (username.isEmpty || email.isEmpty || password.isEmpty || phonenumper.isEmpty) { 

    let alert = UIAlertController(title: "Sign Up Failed!", message:"Please enter your data for Signup", preferredStyle: .Alert) 
    alert.addAction(UIAlertAction(title: "OK ", style: .Default) { _ in }) 
    self.presentViewController(alert, animated: true){} 
} 

else { 
    if (isValidEmail(UserEmailTextFiled.text!)) { 
     let alert = UIAlertController(title: "Inviled Email", message:"Please enter your Email", preferredStyle: .Alert) 
     alert.addAction(UIAlertAction(title: "OK ", style: .Default) { _ in }) 
     self.presentViewController(alert, animated: true){} 

    }else{ 
    //code 

    } 
func isValidEmail(testStr:String) -> Bool { 
     let emailRegEx = "[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" 
     let range = testStr.rangeOfString(emailRegEx, options:.RegularExpressionSearch) 
     let result = range != nil ? true : false 
     return result 
    } 
+0

電子郵件驗證,我建議您更新isValidEmailŧ o將一個可選的String作爲參數,而不是String,如果它的nil返回false。它可以避免崩潰(你總是打開可選值而不檢查) – CZ54

+0

附註,因爲這實際上不是你問題的一部分,但我建議刪除正則表達式。電子郵件地址正則表達式驗證過於複雜,您可能無法解釋所有可能性。如果用戶輸入無效的電子郵件,那麼他們的問題不是你的問題。有關詳情,請深入討論這個博客帖子:https://davidcel.is/posts/stop-validating-email-addresses-with-regex/ –

回答

2

根據你的發佈代碼,if (isValidEmail(UserEmailTextFiled.text!)) {...意味着如果該電子郵件是無效電子郵件警報將顯示有效。你只需要一個!if (!isValidEmail(UserEmailTextFiled.text!)) {

+0

它完美地工作 – kvra13

1
public func isValidEmailAddress() -> Bool { 
    let emailRegex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$" 
    return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: self) 
} 

扭轉結果這是斯威夫特3.0 Func鍵與正則表達式。 只需複製/粘貼您的助手或擴展文件,然後使用它。

0

在雨燕3.0

extension String { 

    func nsstring() -> NSString { 
     return (self as NSString) 
    } 

    func isValidEmailAddress() -> Bool 
    { 

     var returnValue = true 
     let emailRegEx = "[A-Z0-9a-z.-_][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,3}" 
     do { 
      let regex = try NSRegularExpression(pattern: emailRegEx) 

      let results = regex.matches(in: self.trimmingCharacters(in: .whitespacesAndNewlines), range: NSRange(location: 0, length: self.characters.count)) 
      if results.count == 0 
      { 
       returnValue = false 
      } 
     } 
     catch let error as NSError 
     { 
      print("invalid regex: \(error.localizedDescription)") 
      returnValue = false 
     } 
     return returnValue 
    }  
} 

在視圖控制器

let isValidEmail = yourtextField?.text?.isValidEmailAddress() 
if isValidEmail! 
{ 
//Code Here 
} 
else 
{ 
//Error Code Here 
} 
0

在SWIFT 3.0

@IBOutlet weak var emailid: UILabel! 
@IBOutlet weak var MsgAlrtEmailId: UILabel! 

@IBAction func Register(_ sender: Any) 
    { 
     //email 

     let providedEmailAddress = emailid.text 

     let isEmailAddressValid = isValidEmailAddress(emailAddressString: providedEmailAddress!) 

     if isEmailAddressValid 
     { 
      print("Email address is valid") 
      MsgAlrtEmailId.text = "Email Address is valid" 
      MsgAlrtEmailId.sizeToFit() 
      MsgAlrtEmailId.textColor = UIColor.blue 

     } else { 
      print("Email address is not valid") 
      MsgAlrtEmailId.text = "Email Address Not valid" 
      MsgAlrtEmailId.sizeToFit() 
      MsgAlrtEmailId.textColor = UIColor.red 
     } 

    //email rule validation 
     func isValidEmailAddress(emailAddressString: String) -> Bool { 

      var returnValue = true 
      let emailRegEx = "[A-Z0-9a-z.-_][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,3}" 

      do { 
       let regex = try NSRegularExpression(pattern: emailRegEx) 
       let nsString = emailAddressString as NSString 
       let results = regex.matches(in: emailAddressString, range: NSRange(location: 0, length: nsString.length)) 

       if results.count == 0 
       { 
        returnValue = false 
       } 

      } catch let error as NSError { 
       print("invalid regex: \(error.localizedDescription)") 
       returnValue = false 
      } 

      return returnValue 
     }