Skip to content

Instantly share code, notes, and snippets.

@jaredsinclair
Last active August 29, 2015 14:17
Show Gist options
  • Save jaredsinclair/7ec068ba302a8187c9a1 to your computer and use it in GitHub Desktop.
Save jaredsinclair/7ec068ba302a8187c9a1 to your computer and use it in GitHub Desktop.
Email Validation in Objective-C (based on emailregex.com)
static NSString * BLVFormFieldValidator_EmailRegex = @"(?i)(?:[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])+)\\])";
@jaredsinclair
Copy link
Author

Sample emails for unit tests:

self.validEmails = @[
                         @"[email protected]",
                         @"1234567890123456789012345678901234567890123456789012345678901234@iana.org",
                         @"\"first\\\"last\"@iana.org",
                         @"\"first@last\"@iana.org",
                         @"\"first\\last\"@iana.org",
                         @"first.last@[12.34.56.78]",
                         @"first.last@x23456789012345678901234567890123456789012345678901234567890123.iana.org",
                         @"[email protected]",
                         @"[email protected]",
                         @"\"first\\last\"@iana.org",
                         @"\"Abc\\@def\"@iana.org",
                         @"[email protected]",
                         @"customer/[email protected]",
                         @"[email protected]",
                         @"!def!xyz%a[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"t*[email protected]",
                         @"[email protected]",
                         @"{_test_}@iana.org",
                         @"customer/[email protected]",
                         @"\"Austin@Powers\"@iana.org",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]",
                         @"[email protected]"
                         ];

    self.invalidEmails = @[
                           @"[email protected],com",
                           @"first\\@[email protected]",
                           @"first.last",
                           @"[email protected]",
                           @"[email protected]",
                           @"[email protected]",
                           @"\"first\"last\"@iana.org",
                           @"first\\@[email protected]",
                           @"first.last@",
                           @"first.last@[.12.34.56.78]",
                           @"first.last@[12.34.56.789]",
                           @"[email protected]",
                           @"[email protected]",
                           @"abc\\@[email protected]",
                           @"abc\\@iana.org",
                           @"Doug\\ \\\"Ace\\\"\\ [email protected]",
                           @"@iana.org",
                           @"doug@",
                           @"[email protected]",
                           @"()[]\\;:,><@iana.org",
                           @"test@.",
                           @"NotAnEmail"
                           ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment