메일박스의 합법성 여부를 판단하다

6213 단어 판단
-(BOOL)validateEmail:(NSString*)email

{

    if((0 != [email rangeOfString:@"@"].length) &&

       (0 != [email rangeOfString:@"."].length))

    {

 

        NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

        NSMutableCharacterSet* tmpInvalidMutableCharSet = [[tmpInvalidCharSet mutableCopy] autorelease];

        [tmpInvalidMutableCharSet removeCharactersInString:@"_-"];

 

        // compare option  , 

        //NSCaseInsensitiveSearch 

        //NSLiteralSearch  , 

        //NSNumericSearch  , 

        NSRange range1 = [email rangeOfString:@"@"

                                      options:NSCaseInsensitiveSearch];

 

        // 

        NSString* userNameString = [email substringToIndex:range1.location];

        NSArray* userNameArray   = [userNameString componentsSeparatedByString:@"."];

 

        for(NSString* string in userNameArray)

        {

            NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet];

            if(rangeOfInavlidChars.length != 0 || [string isEqualToString:@""])

                return NO;

        }

 

        NSString *domainString = [email substringFromIndex:range1.location+1];

        NSArray *domainArray   = [domainString componentsSeparatedByString:@"."];

 

        for(NSString *string in domainArray)

        {

            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet];

            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])

                return NO;

        }

 

        return YES;

    }

    else // no ''@'' or ''.'' present

        return NO;

}

http://www.cocoachina.com/bbs/columns.php?action=columns

좋은 웹페이지 즐겨찾기