Validation of email text iOS swift

 Problem:

I'm having the textfield that had an input as email address.I want to validate the text user entered in the textfield is a valid email address.

Solution:

extension String {

    func isValidEmailAddress()-> Bool {

        let regex = try! NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", options: [.caseInsensitive])

        return regex.firstMatch(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.count)) != nil

    }

}



Call the above function as like below.


mytextField.text.isValidEmailAddress()

Comments

Popular posts from this blog

store cgpoint in userdefaults iOS swift

libc++abi: terminating due to uncaught exception of type NSException iOS swift