Email & Phone Validation in Swift

后端 未结 21 1771
长发绾君心
长发绾君心 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条回答
  •  -上瘾入骨i
    2020-12-07 12:52

    Swift 4 & Swift 5:

    func isValidPhone(phone: String) -> Bool {
            let phoneRegex = "^[0-9+]{0,1}+[0-9]{5,16}$"
            let phoneTest = NSPredicate(format: "SELF MATCHES %@", phoneRegex)
            return phoneTest.evaluate(with: phone)
        }
    
    func isValidEmail(email: String) -> Bool {
            let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
            let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
            return emailTest.evaluate(with: email)
        }
    

    • +1994423565 - Valid
    • ++1994423565 - Invalid
    • 01994423565 - Valid
    • 001994423565 - Valid
    • something@example.com - Valid
    • something@.com - Invalid

提交回复
热议问题