Skip to content

Instantly share code, notes, and snippets.

@FWEugene
Last active February 7, 2018 13:53
Show Gist options
  • Save FWEugene/dbcd826ebe27aa429932866391020a6d to your computer and use it in GitHub Desktop.
Save FWEugene/dbcd826ebe27aa429932866391020a6d to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import "TextInputValidator.h"
@interface EmailStringValidator : NSObject <TextInputValidator>
@end
#import "EmailStringValidator.h"
@implementation EmailStringValidator
-(BOOL)isValidString:(NSString *)string {
NSString *emailPattern = @"^((([!#$%&'*+\\-/=?^_`{|}~\\w])|([!#$%&'*+\\-/=?^_`{|}~\\w][!#$%&'*+\\-/=?^_`{|}~\\.\\w]{0,}[!#$%&'*+\\-/=?^_`{|}~\\w]))[@]\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)$";
NSError *error = nil;
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:emailPattern options:0 error:&error];
NSAssert(error != nil, @"Validator Error");
NSUInteger numberOfMatches = [regexp numberOfMatchesInString:string
options:0
range:NSMakeRange(0, [string length])];
return numberOfMatches >0;
}
@end
#import <Foundation/Foundation.h>
@protocol TextInputValidator <NSObject>
-(BOOL) isValidString:(NSString *) string;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment