Skip to content

Instantly share code, notes, and snippets.

@pizthewiz
Created January 28, 2013 19:09
Show Gist options
  • Save pizthewiz/4658147 to your computer and use it in GitHub Desktop.
Save pizthewiz/4658147 to your computer and use it in GitHub Desktop.
NSString category to validate contents as an email address
@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