I just spent a lot of time doing something that would have been ridiculously easy with Cocoa’s NSPredicate : checking an email address to know if it is valid, before sending something to it. NSPredicate is not available on the iPhone.
I used instead RegexKitLite, which in turn uses libicucore, this time available on your iPhone, to check things. I just added the core class, and the RKLMatchEnumerator too, which adds a matchEnumeratorWithRegex to NSEnumerator. Et voilĂ !
@@
NSEnumerator *matchEnumerator = NULL; NSString *emailRegEx = @"A-Z0-9a-z._%+-+@A-Za-z0-9.-+\\.A-Za-z{2,4}";
matchEnumerator = emailAddress matchEnumeratorWithRegex:emailRegEx;
//Validate email address if (matchEnumerator nextObject == NULL) { UIAlertView *alert = [UIAlertView alloc initWithTitle:@"" message:NSLocalizedString(@"EmailNotValid", @"No search") delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; alert show; alert release; return; }
@@