Created
January 28, 2013 19:09
-
-
Save pizthewiz/4658147 to your computer and use it in GitHub Desktop.
NSString category to validate contents as an email address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSString (CLUEmailAdditions) | |
- (BOOL)isValidEmailAddress; | |
@end | |
@implementation NSString (CLUEmailAdditions) | |
static NSString* CLUEmailAddressPattern = @"^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$"; | |
// NB - mostly yoinked from https://github.com/jPaolantonio/JPValidation/blob/master/JPValidators/JPValidatorEmail.m | |
- (BOOL)isValidEmailAddress { | |
static NSRegularExpression* emailRegularExpression; | |
if (!emailRegularExpression) { | |
emailRegularExpression = [NSRegularExpression regularExpressionWithPattern:CLUEmailAddressPattern options:NSRegularExpressionCaseInsensitive error:nil]; | |
} | |
// TODO - consider supporting comma separated addresses | |
NSUInteger numberOfMatches = [emailRegularExpression numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)]; | |
return numberOfMatches == 1; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment