<转>代码判断邮箱是否合法

原文:http://blog.csdn.net/favormm/article/details/6573318

 

 1 -(BOOL)validateEmail:(NSString*)email{  
2
3 if( (0 != [email rangeOfString:@"@"].length) && (0 != [email rangeOfString:@"."].length) )
4 {
5 NSMutableCharacterSet *invalidCharSet = [[[[NSCharacterSet alphanumericCharacterSet] invertedSet]mutableCopy]autorelease];
6 [invalidCharSet removeCharactersInString:@"_-"];
7
8 NSRange range1 = [email rangeOfString:@"@" options:NSCaseInsensitiveSearch];
9
10 // If username part contains any character other than "." "_" "-"
11
12 NSString *usernamePart = [email substringToIndex:range1.location];
13 NSArray *stringsArray1 = [usernamePart componentsSeparatedByString:@"."];
14 for (NSString *string in stringsArray1) {
15 NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet: invalidCharSet];
16 if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
17 return NO;
18 }
19
20 NSString *domainPart = [email substringFromIndex:range1.location+1];
21 NSArray *stringsArray2 = [domainPart componentsSeparatedByString:@"."];
22
23 for (NSString *string in stringsArray2) {
24 NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:invalidCharSet];
25 if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
26 return NO;
27 }
28
29 return YES;
30 }
31 else // no '@' or '.' present
32 return NO;
33 }



你可能感兴趣的:(代码)