Created
March 6, 2017 21:47
-
-
Save ranmyfriend/96f6ae1b64b177af62de402c6898a314 to your computer and use it in GitHub Desktop.
Phone Number validation in Swift 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
public func isPhone()->Bool { | |
if self.isAllDigits() == true { | |
let phoneRegex = "[235689][0-9]{6}([0-9]{3})?" | |
let predicate = NSPredicate(format: "SELF MATCHES %@", phoneRegex) | |
return predicate.evaluate(with: self) | |
}else { | |
return false | |
} | |
} | |
private func isAllDigits()->Bool { | |
let charcterSet = NSCharacterSet(charactersIn: "+0123456789").inverted | |
let inputString = self.components(separatedBy: charcterSet) | |
let filtered = inputString.joined(separator: "") | |
return self == filtered | |
} | |
} | |
"9704963170".isPhone() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment