-
-
Save illescasDaniel/d38dbe4dbd4e2c527facf80021d9f3b5 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
import Foundation | |
extension String { | |
mutating func isValidEmail() -> Bool { | |
guard !self.lowercased().hasPrefix("mailto:") else { return false } | |
guard let emailDetector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) else { return false } | |
let textRange = NSRange(location: 0, length: self.characters.count) | |
let matches = emailDetector.matches(in: self, options: .anchored, range: textRange) | |
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