In iOS App, how to add Email validation on UITextField?
NSRegularExpression
is the Best Way to Validate Email Addresses with iOS 4.x and Later.
-(BOOL) validateEmail:(NSString*) emailString
{
NSString *regExPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$";
NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString options:0 range:NSMakeRange(0, [emailString length])];
NSLog(@"%i", regExMatches);
if (regExMatches == 0) {
return NO;
}
else
return YES;
}
Usage :
if([self validateEmail:@"something@domain.com"]) {
//Email Address is valid.
}
else {
//Email Address is invalid.
}