i am using the following code for phone number validation. But i am getting the following error. I cant able to proceed further. Help us to take it forward.
File-New-File.Make a Swift class named AppExtension.Add the following.
extension UIViewController{
func validateEmailAndGetBoolValue(candidate: String) -> Bool {
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluateWithObject(candidate)
}
}
Use:
var emailValidator:Bool?
self.emailValidator = self.validateEmailAndGetBoolValue(resetEmail!)
print("emailValidator : "+String(self.emailValidator?.boolValue))
Use a loop to alternate desired results.
OR
extension String
{
//Validate Email
var isEmail: Bool {
do {
let regex = try NSRegularExpression(pattern: "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", options: .CaseInsensitive)
return regex.firstMatchInString(self, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count)) != nil
} catch {
return false
}
}
}
Use:
if(resetEmail!.isEmail)
{
AppController().requestResetPassword(resetEmail!)
self.view.makeToast(message: "Sending OTP")
}
else
{
self.view.makeToast(message: "Please enter a valid email")
}