美文网首页
利用字符串区分邮箱是否合法

利用字符串区分邮箱是否合法

作者: dxin_101 | 来源:发表于2021-11-17 17:42 被阅读0次

    -(BOOL)validateEmail:(NSString*)email

    {

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

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

    {

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

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

    [tmpInvalidMutableCharSetremoveCharactersInString:@"_-"];

    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 {

    return NO;

    }

    }

    相关文章

      网友评论

          本文标题:利用字符串区分邮箱是否合法

          本文链接:https://www.haomeiwen.com/subject/kazltrtx.html