jump to navigation

iPhone Email Validation for Text Fieldone July 31, 2009

Posted by Ameya in iPhone, iPhone App Devlopment.
Tags: , ,
trackback

Searched a lot for email validation for iPhone did not find any , hence was forced to create an validation method of my own.

Found an sample as below.

NSString *mystring = @"Hello World!";
NSString *regex = @".*l{2,}.*";
NSPredicate *regextest = [NSPredicate
                         predicateWithFormat:@"SELF MATCHES %@", regex];
if ([regextest evaluateWithObject:mystring] == YES) {
    NSLog(@"Match!");
} else {
    NSLog(@"No match!");
}

The above code will not work as NSPredicate is not available for iPhone framework.
Also found an sample that had the following code as below.

RegexKitLite Objective-C ÕýÔò±í´ïʾ_ССµÄÌ«Ñô 

NSString *email=@”asd@fgh.com”;
NSString *strEmailMatchstring=@”\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b”;
if(![email isMatchedByRegex:strEmailMatchstring]){

NSLog(@”Invalid email address found”);
objAlert = [[UIAlertView alloc] initWithTitle:@”Error!” message:

@”Enter a valid e-mail address” delegate:nil cancelButtonTitle:nil otherButtonTitles:@”Try Again”,nil];
[objAlert show];[objAlert release];

}

 
Found Regex download , which is available at http://downloads.sourceforge.net/regexkit/RegexKitLite-3.0.tar.bz2

* To include the RegexKitLite.h and RegexKitLite.m file in you project.

* In project setting search "other" at Other Linker Flags put the word "-licucore".

* Import header in the view controller where it will be used.

Modify the above method to as follows.
 -(BOOL)emailValidate:(NSString *)email{
 NSString *strEmailMatchstring=@"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";
 if([email isMatchedByRegex:strEmailMatchstring])
 return YES;
 else
 return NO;
 }

Created an method of my own hope this would help people searching for such validation methods.

-(BOOL)emailValidate :( NSString *)email{

//Based on the string below
//NSString *strEmailMatchstring=@”\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b”;

//Quick return if @ Or . not in the string
if([email rangeOfString:@"@"].location==NSNotFound || [email rangeOfString:@"."].location==NSNotFound)
return NO;

//Break email address into its components
NSString *accountName=[email substringToIndex: [email rangeOfString:@"@"].location];
email=[email substringFromIndex:[email rangeOfString:@"@"].location+1];

//’.’ not present in substring
if([email rangeOfString:@"."].location==NSNotFound)
return NO;
NSString *domainName=[email substringToIndex:[email rangeOfString:@"."].location];
NSString *subDomain=[email substringFromIndex:[email rangeOfString:@"."].location+1];

//username, domainname and subdomain name should not contain the following charters below
//filter for user name
NSString *unWantedInUName = @” ~!@#$^&*()={}[]|;’:\”<>,?/`”;
//filter for domain
NSString *unWantedInDomain = @” ~!@#$%^&*()={}[]|;’:\”<>,+?/`”;
//filter for subdomain
NSString *unWantedInSub = @” `~!@#$%^&*()={}[]:\”;’<>,?/1234567890″;

//subdomain should not be less that 2 and not greater 6
if(!(subDomain.length>=2 && subDomain.length<=6)) return NO;

if([accountName isEqualToString:@""] || [accountName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInUName]].location!=NSNotFound || [domainName isEqualToString:@""] || [domainName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInDomain]].location!=NSNotFound || [subDomain isEqualToString:@""] || [subDomain rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInSub]].location!=NSNotFound)
return NO;

return YES;
}

Advertisement

Comments»

1. Marcel - August 24, 2009

Hi . Thank you very much . But it needs more work . For example : example@example..com the validation returns YES

2. Ameya - August 27, 2009

Have u tried it with RegexKitLite frame work , should work fine. Else you can modify my method to meet your requirements.

3. 2010 in review « iPhone App Dev Help - January 3, 2011

[...] iPhone Email Validation for Text Fieldone July 2009 2 comments 4 [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.