Email validation on textField in iPhone

Use NSPredicate and Regex:
- (BOOL)validateEmailWithString:(NSString*)email{
   
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
   
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
   
return [emailTest evaluateWithObject:email];
}
 

For a bunch of emails separated by a comma:

- (NSMutableArray*)validateEmailWithString:(NSString*)emails{
   
NSMutableArray *validEmails = [[NSMutableArray alloc] init];
   
NSArray *emailArray = [emails componentsSeparatedByString:@","];
   
for (NSString *email in emailArray)
   
{
       
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
       
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
       
if ([emailTest evaluateWithObject:email])
           
[validEmails addObject:email];
   
}
   
return [validEmails autorelease];
}
 
All the best. 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>