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

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift