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

```

I use this function to validate email in my login form. I can't understand how to rise this event from my textfield.

Using like this

```
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
}

```

But getting unresolved error at `teststr`. I am new to Swift. Can any body explain me how to solve this?