-
-
Save rlaguilar/a238f4b373cb7e38511503c0853e077f to your computer and use it in GitHub Desktop.
Swift method to check for a valid email address (uses Apples data detector instead of a regex)
This file contains 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 { | |
func isValidEmail() -> Bool { | |
guard !self.lowercaseString.hasPrefix("mailto:") else { return false } | |
guard let emailDetector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue) else { return false } | |
let matches = emailDetector.matchesInString(self, options: NSMatchingOptions.Anchored, range: NSRange(location: 0, length: self.characters.count)) | |
guard matches.count == 1 else { return false } | |
return matches[0].URL?.scheme == "mailto" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment