【发布时间】:2018-05-07 06:03:32
【问题描述】:
我已经实现了@Vasily-Bodnarchuk here 提供的这个不错的解决方案
我想对此进行修改,以便验证澳大利亚号码。问题是下面代码中的 RegEx 验证似乎只验证美国号码。我尝试了几种不同的值,但似乎都不起作用。
有什么想法吗?
extension String {
enum RegularExpressions: String {
case phone = "^\\s*(?:\\+?(\\d{1,3}))?([-. (]*(\\d{3})[-. )]*)?((\\d{3})[-. ]*(\\d{2,4})(?:[-.x ]*(\\d+))?)\\s*$"
}
func isValid(regex: RegularExpressions) -> Bool {
return isValid(regex: regex.rawValue)
}
func isValid(regex: String) -> Bool {
let matches = range(of: regex, options: .regularExpression)
return matches != nil
}
func onlyDigits() -> String {
let filtredUnicodeScalars = unicodeScalars.filter{CharacterSet.decimalDigits.contains($0)}
return String(String.UnicodeScalarView(filtredUnicodeScalars))
}
func makeAColl() {
if isValid(regex: .phone) {
if let url = URL(string: "tel://\(self.onlyDigits())"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
}
}
}
【问题讨论】:
-
有一些第三方库可以验证和格式化任何国家的电话号码。尝试搜索其中之一。
标签: ios swift regex phone-number