Email & Phone Validation in Swift

后端 未结 21 1776
长发绾君心
长发绾君心 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:01

    Updated for Swift 3

    extension String {
        var isPhoneNumber: Bool {
            do {
                let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
                let matches = detector.matches(in: self, options: [], range: NSMakeRange(0, self.characters.count))
                if let res = matches.first {
                    return res.resultType == .phoneNumber && res.range.location == 0 && res.range.length == self.characters.count
                } else {
                    return false
                }
            } catch {
                return false
            }
        }
    }
    

提交回复
热议问题