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

Am sử dụng chức năng này để xác nhận email dưới dạng đăng nhập của tôi không nhận làm thế nào để tăng sự kiện này từ textfield tôixác nhận email trong ứng dụng iOS nhanh chóng

sử dụng như thế này

if(username.isEqualToString("") || [!LoginController, testStr == self.username]) 
     { 
      self.dismissViewControllerAnimated(true, completion: nil) 
      var alertView:UIAlertView = UIAlertView() 
      alertView.title = "Sign in Failed!" 
      alertView.message = "Please enter valid username" 
      alertView.delegate = self 
      alertView.addButtonWithTitle("OK") 
      alertView.show() 
      return 
     } 

nhưng nhận được lỗi chưa được giải quyết tại teststr i am new to swift có thể bất kỳ nội dung nào giải thích cho tôi cách giải quyết này

Trả lời

6

Bạn có thể xác thực email bằng chức năng regEX đơn giản, mà retu rn true nếu hợp lệ khác false

Bạn có thể xác nhận nó trong khi người sử dụng nhấn Done/Enter trên sự kiện bàn phím, giống như 'editingDidEnd', ràng buộc nó từ kịch bản đến tập tin lớp như thế nào,

@IBAction func validateEmail(sender: UITextField){ 
    if txtEmaildAddress.text.isEmpty { 
     println("enter email address") //prompt ALert or toast 
    } 
    else if self.validate(txtEmaildAddress.text) { 
     println("Invalid email address") // prompt alert for invalid email 
    } 
} 

func validate(YourEMailAddress: String) -> Bool { 
    let REGEX: String 
    REGEX = "[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" 
    return NSPredicate(format: "SELF MATCHES %@", REGEX).evaluateWithObject(YourEMailAddress) 
} 

có thể giúp gì phương pháp này để xác thực một cách dễ dàng.

HTH, Tận hưởng mã hóa !!

+1

Tại sao 'REGEX'? Tại sao phá vỡ mọi quy ước đặt tên biến có thể xảy ra? 'YourEmailAddress'? 'txtEmaildAddress'? Tại sao không dính vào một quy ước duy nhất? – Fogmeister

Các vấn đề liên quan