Skip to content

Instantly share code, notes, and snippets.

Created May 5, 2014 15:33
Show Gist options
  • Save anonymous/3bea53c05336f931bd1d to your computer and use it in GitHub Desktop.
Save anonymous/3bea53c05336f931bd1d to your computer and use it in GitHub Desktop.
Test email address validation with esoteric email addresses
- (BOOL)isValidEmailAddress:(NSString *)text {
NSError *error = NULL;
NSRange textRange = {0, [text length]};
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
if (!detector) {
NSLog(@"Could not instantiate link detector for email validation: %@", error);
return NO;
}
NSTextCheckingResult *result = [detector firstMatchInString:text options:0 range:textRange];
if (![result.URL.scheme isEqual:@"mailto"]) {
// No link was detected, or it wasn't an email address
return NO;
}
if (result.range.location > textRange.location || result.range.length < textRange.length) {
// The detected email address does't span the entire input text
return NO;
}
return YES;
}
- (void)testEmail:(NSString *)text isValid:(BOOL)isValid {
BOOL result = [self isValidEmailAddress:text];
if (result != isValid) NSLog(@"%@ should be %@", text, isValid ? @"valid" : @"invalid");
}
- (void)testEmails {
[self testEmail:@"NotAnEmail" isValid:NO];
[self testEmail:@"@NotAnEmail" isValid:NO];
[self testEmail:@"\"test\\\\blah\"@example.com" isValid:YES];
[self testEmail:@"\"test\\blah\"@example.com" isValid:NO];
[self testEmail:@"\"test\\\rblah\"@example.com" isValid:YES];
[self testEmail:@"\"test\rblah\"@example.com" isValid:NO];
[self testEmail:@"\"test\\\"blah\"@example.com" isValid:YES];
[self testEmail:@"\"test\"blah\"@example.com" isValid:NO];
[self testEmail:@"customer/department@example.com" isValid:YES];
[self testEmail:@"$A12345@example.com" isValid:YES];
[self testEmail:@"!def!xyz%abc@example.com" isValid:YES];
[self testEmail:@"_Yosemite.Sam@example.com" isValid:YES];
[self testEmail:@"~@example.com" isValid:YES];
[self testEmail:@".wooly@example.com" isValid:NO];
[self testEmail:@"wo..oly@example.com" isValid:NO];
[self testEmail:@"pootietang.@example.com" isValid:NO];
[self testEmail:@".@example.com" isValid:NO];
[self testEmail:@"\"Austin@Powers\"@example.com" isValid:YES];
[self testEmail:@"Ima.Fool@example.com" isValid:YES];
[self testEmail:@"\"Ima.Fool\"@example.com" isValid:YES];
[self testEmail:@"\"Ima Fool\"@example.com" isValid:YES];
[self testEmail:@"Ima Fool@example.com" isValid:NO];
}
@cmlenz
Copy link

cmlenz commented May 5, 2014

Results:

✅ NotAnEmail detected as invalid
✅ @NotAnEmail detected as invalid
⛔️ "test\\blah"@example.com should be valid
✅ "test\blah"@example.com detected as invalid
⛔️ "test\\nblah"@example.com should be valid
✅ "test\nblah"@example.com detected as invalid
⛔️ "test\"blah"@example.com should be valid
✅ "test"blah"@example.com detected as invalid
✅ customer/department@example.com detected as valid
⛔️ $A12345@example.com should be valid
⛔️ !def!xyz%abc@example.com should be valid
⛔️ _Yosemite.Sam@example.com should be valid
⛔️ ~@example.com should be valid
✅ .wooly@example.com detected as invalid
⛔️ wo..oly@example.com should be invalid
✅ pootietang.@example.com detected as invalid
✅ .@example.com detected as invalid
⛔️ "Austin@Powers"@example.com should be valid
✅ Ima.Fool@example.com detected as valid
⛔️ "Ima.Fool"@example.com should be valid
⛔️ "Ima Fool"@example.com should be valid
✅ Ima Fool@example.com detected as invalid

11 of 22 failed.

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