Search This Blog

Oct 5, 2010

Email Validation in iPhone application.

This code is used for email validation. This function will return the True of False for the give email address. If the Email is valid it will return True other wise it will return False.

-(BOOL)validateEmail:(NSString*)email{
   
    if( (0 != [email rangeOfString:@"@"].length) &&  (0 != [email rangeOfString:@"."].length) )
    {
        NSMutableCharacterSet *invalidCharSet = [[[[NSCharacterSet alphanumericCharacterSet] invertedSet]mutableCopy]autorelease];
        [invalidCharSet removeCharactersInString:@"_-"];
       
        NSRange range1 = [email rangeOfString:@"@" options:NSCaseInsensitiveSearch];
       
        // If username part contains any character other than "."  "_" "-"
       
        NSString *usernamePart = [email substringToIndex:range1.location];
        NSArray *stringsArray1 = [usernamePart componentsSeparatedByString:@"."];
        for (NSString *string in stringsArray1) {
            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet: invalidCharSet];
            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
                return NO;
        }
       
        NSString *domainPart = [email substringFromIndex:range1.location+1];
        NSArray *stringsArray2 = [domainPart componentsSeparatedByString:@"."];
       
        for (NSString *string in stringsArray2) {
            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:invalidCharSet];
            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
                return NO;
        }
       
        return YES;
    }
    else // no '@' or '.' present
        return NO;
}

2 comments:

  1. iPhone is a device that started the trend for Application store. iPhone’s touch-screen and its built-in accelerometer, makes you know in which way you’re holding the phone. The first phone to allow users to install software applications on to handsets. It’s a whole new aspect to mobile technology which examines the extra things your mobile can do & It’s all been done by brilliant iPhone Professionals.

    ReplyDelete
  2. Mobile phone app developer team, creating apps for the Apple and Android markets. We can develop your mobile phone application and get it published quickly.

    IphoneApps Development

    ReplyDelete