Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created March 15, 2020 16:00
Show Gist options
  • Save foxicode/2d8e8f00be79aac861c64a619aaa7372 to your computer and use it in GitHub Desktop.
Save foxicode/2d8e8f00be79aac861c64a619aaa7372 to your computer and use it in GitHub Desktop.
Alternative way to check if String is email (Swift)
extension String {
func matches(_ expression: String) -> Bool {
if let range = range(of: expression, options: .regularExpression, range: nil, locale: nil) {
return range.lowerBound == startIndex && range.upperBound == endIndex
} else {
return false
}
}
var isValidEmail: Bool {
matches("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment