我已经实施了@ 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)
}
}
}
}
}
答案 0 :(得分:0)
要验证澳大利亚手机,请使用此功能:
var phoneExpression = "/^\({0,1}((0|\+61)(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{3}$/"
// Valid
var phoneNumber1 = "0411 234 567"
var phoneNumber2 = "(02) 3892 1111"
var phoneNumber3 = "38921111"
var phoneNumber4 = "0411234567"
// Invalid
var phoneNumber5 = "3892 11"
var phoneNumber6 = "diane 0411 234 567"
var phoneNumber7 = "bob"