C#检测输入的邮件地址是否合法

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// 检测输入的邮件地址strEmail是否合法,非法则返回true。
ExpandedBlockEnd.gif        
/// </summary>

None.gif          public   bool  CheckEmail( string  strEmail)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
int i,j;
InBlock.gif            
string strTmp,strResult;
InBlock.gif            
string strWords = "abcdefghijklmnopqrstuvwxyz_-.0123456789"//定义合法字符范围
InBlock.gif
            bool blResult =false;
InBlock.gif            strTmp 
= strEmail.Trim();
InBlock.gif            
//检测输入字符串是否为空,不为空时才执行代码。
InBlock.gif
            if(!(strTmp == null || strTmp.Length == 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//判断邮件地址中是否存在“@”号
InBlock.gif
                if((strTmp.IndexOf("@")<0))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    blResult 
= true;
InBlock.gif                    
return blResult;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//以“@”号为分割符,把地址切分成两部分,分别进行验证。
ExpandedSubBlockStart.gifContractedSubBlock.gif
                string[] strChars = strTmp.Split(new char[] dot.gif{'@'});
InBlock.gif                
foreach(string strChar in strChars)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    i 
= strChar.Length;
InBlock.gif                    
//“@”号前部分或后部分为空时。
InBlock.gif
                    if(i == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        blResult 
= true;
InBlock.gif                        
return blResult;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
//逐个字进行验证,如果超出所定义的字符范围strWords,则表示地址非法。
InBlock.gif
                    for(j=0;j<i;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        strResult 
= strChar.Substring(j,1).ToLower();
InBlock.gif                        
if(strWords.IndexOf(strResult)<0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            blResult 
= true;
InBlock.gif                            
return blResult;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return blResult;
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/lgzdd/archive/2005/07/31/204015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值