判断邮箱格式是否正确

//    是否是 合法邮箱
BOOL IsValidEmail(const CString strEmail)
{
    if( strEmail.GetLength() < 5 ) //26个小写字母
    {
        return FALSE;
    }
    char ch;

    int atCount =0;
    int atPos = 0;
    int dotCount = 0;
    for(int i=0; i<strEmail.GetLength(); i++) //  从0 开始判断
    {
        ch = strEmail[i];
        if( IsValidChar(ch) )
        {
            if(ch==64) //"@"
            {
                atCount ++;
                atPos = i;
            }
            else if( (atCount>0) && (ch==46) )//@ 符号后的"."号
            {
                dotCount ++;
            }
        }
        else
        {
            return FALSE;
        }
    }
    //6. 结尾不可以是字符 "@" 或者 "".
    if( ch == 46 )
    {
        return FALSE;
    }
    //2. 必须包含一个 并且 只有一个符号“@”
    //3. @  后必须包含至少一个至多三个符号"."
    if( (atCount!=1) || (dotCount<1) || (dotCount>3) )
        return FALSE;
    //5. 不允许储蓄“@.” 或者 ".@"
    int x=-1, y=-1;
    x=strEmail.Find(_T("@."));
    y=strEmail.Find(_T(".@"));
    if( x>0 || y>0 )
    {
        return FALSE;
    }
    return TRUE;
}

 

参考文:

  http://blog.sina.com.cn/s/blog_7f2cb66b0100t3l4.html

posted @ 2015-02-05 21:19  王家^0^小轩  阅读(389)  评论(0编辑  收藏  举报