Skip to content

Instantly share code, notes, and snippets.

@sumate-sdk
Forked from siberianisaev/NSString+Email.swift
Last active August 29, 2015 14:25
Show Gist options
  • Save sumate-sdk/bc9486e79eed456264f6 to your computer and use it in GitHub Desktop.
Save sumate-sdk/bc9486e79eed456264f6 to your computer and use it in GitHub Desktop.
import Foundation
extension NSString {
/**
http://stackoverflow.com/questions/800123/what-are-best-practices-for-validating-email-addresses-in-objective-c-for-ios-2
RFC 2822 http://tools.ietf.org/html/rfc2822
*/
func isEmail() -> Bool {
let sRegex = "(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"
if let b = NSPredicate(format: "SELF MATCHES[c] %@", sRegex)?.evaluateWithObject(self) {
return b
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment