你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iphone開發:郵箱有效驗證

iphone開發:郵箱有效驗證

編輯:IOS開發綜合

判斷是不是一個有效的郵箱地址。


-(BOOL)is_email_can_be_open:(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; 
    } 
    return NO;

 


摘自 雲懷空-abel

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved