Email & Phone Validation in Swift

后端 未结 21 1766
长发绾君心
长发绾君心 2020-12-07 12:02

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.

         


        
21条回答
  •  离开以前
    2020-12-07 12:36

    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")
                            }
    

提交回复
热议问题