Skip to content

Instantly share code, notes, and snippets.

@jaredsinclair
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save jaredsinclair/7ec068ba302a8187c9a1 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
Author

Sample emails for unit tests:

self.validEmails = @[
                         @"first.last@iana.org",
                         @"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",
                         @"first.last@3com.com",
                         @"first.last@123.iana.org",
                         @"\"first\\last\"@iana.org",
                         @"\"Abc\\@def\"@iana.org",
                         @"user+mailbox@iana.org",
                         @"customer/department=shipping@iana.org",
                         @"$A12345@iana.org",
                         @"!def!xyz%abc@iana.org",
                         @"_somename@iana.org",
                         @"dclo@us.ibm.com",
                         @"peter.piper@iana.org",
                         @"test@iana.org",
                         @"TEST@iana.org",
                         @"1234567890@iana.org",
                         @"test+test@iana.org",
                         @"test-test@iana.org",
                         @"t*est@iana.org",
                         @"+1~1+@iana.org",
                         @"{_test_}@iana.org",
                         @"customer/department@iana.org",
                         @"\"Austin@Powers\"@iana.org",
                         @"Ima.Fool@iana.org",
                         @"a@bar.com",
                         @"valid@about.museum",
                         @"shaitan@my-domain.thisisminekthx",
                         @"test@xn--example.com",
                         @"test@test.com"
                         ];

    self.invalidEmails = @[
                           @"first.last@sub.do,com",
                           @"first\\@last@iana.org",
                           @"first.last",
                           @".first.last@iana.org",
                           @"first.last.@iana.org",
                           @"first..last@iana.org",
                           @"\"first\"last\"@iana.org",
                           @"first\\@last@iana.org",
                           @"first.last@",
                           @"first.last@[.12.34.56.78]",
                           @"first.last@[12.34.56.789]",
                           @"first.last@-xample.com",
                           @"first.last@exampl-.com",
                           @"abc\\@def@iana.org",
                           @"abc\\@iana.org",
                           @"Doug\\ \\\"Ace\\\"\\ Lovell@iana.org",
                           @"@iana.org",
                           @"doug@",
                           @"test..test@iana.org",
                           @"()[]\\;:,><@iana.org",
                           @"test@.",
                           @"NotAnEmail"
                           ];

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