Email & Phone Validation in Swift

后端 未结 21 1817
长发绾君心
长发绾君心 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 13:00

    Swift 4.1.

    func isValidPhone(phone: String) -> Bool {
    
          let phoneRegex = "^[0-9]{6,14}$";
          let valid = NSPredicate(format: "SELF MATCHES %@", phoneRegex).evaluate(with: phone)
          return valid
       }
    
    func isValidEmail(candidate: String) -> Bool {
    
           let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
           var valid = NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: candidate)
            if valid {
                valid = !candidate.contains("..")
            }
            return valid
      }
    

提交回复
热议问题