0
回答
代码判断邮箱是否合法2
滴滴云服务器,限时包月0.9元,为开发者而生>>>   

在前面的一段时间里介绍了如何代码判断邮箱是否合法

http://blog.csdn.net/favormm/archive/2011/06/19/6555350.aspx

 

今天又与大家分享一段代码,用来判断邮箱是否合法,原理就是字符串操作来检查。

-(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;
}
 

 

代码收集于网上,如侵害了版权,请通知我,于予删除。


原文链接:http://blog.csdn.net/favormm/article/details/6573318
<无标签>
举报
长平狐
发帖于5年前 0回/119阅
顶部