资源描述:
《如何判断电子邮件的地址格式是否正确.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、如何判断电子邮件的地址格式是否正确?我在网站上设置了邮件列表功能,实现用户自动订阅和发送,但很多用户输入和提交的邮件地址格式都是无效的,无法处理。请问如何解决这一问题?我们可用下列办法来解决这一问题——但只是能够判断每个电子邮件地址的格式是否有效,并不能保证该地址确实存在。第一种办法:<%FunctionIsValidEmail(Email)ValidFlag=FalseIf(Email<>"")And(InStr(1,Email,"@")>0)And(InStr(1,Email,".")>0)ThenatCount=0SpecialFlag=FalseForatLoop=
2、1ToLen(Email)atChr=Mid(Email,atLoop,1)IfatChr="@"ThenatCount=atCount+1If(atChr>=Chr(32))And(atChr<=Chr(44))ThenSpecialFlag=TrueIf(atChr=Chr(47))Or(atChr=Chr(96))Or(atChr>=Chr(123))ThenSpecialFlag=TrueIf(atChr>=Chr(58))And(atChr<=Chr(63))ThenSpecialFlag=TrueIf(atChr>=Chr(91))And(atChr<=Chr(
3、94))ThenSpecialFlag=TrueNextIf(atCount=1)And(SpecialFlag=False)ThenBadFlag=FalsetAry1=Split(Email,"@")UserName=tAry1(0)DomainName=tAry1(1)If(UserName="")Or(DomainName="")ThenBadFlag=TrueIfMid(DomainName,1,1)="."thenBadFlag=TrueIfMid(DomainName,Len(DomainName),1)="."thenBadFlag=TrueValidFla
4、g=True'格式正确返回TureEndIfEndIfIfBadFlag=TrueThenValidFlag=False'格式不正确返回FalseIsValidEmail=ValidFlagEndFunction%>第二种办法:<%function3/3IsValidEmail(email)dimnames,name,i,cIsValidEmail=truenames=Split(email,"@")ifUBound(names)<>1then IsValidEmail=false exitfunctionendifforeachnameinnames ifLen(name
5、)<=0then IsValidEmail=false exitfunction endif fori=1toLen(name) c=Lcase(Mid(name,i,1)) ifInStr("abcdefghijklmnopqrstuvwxyz_-.",c)<=0andnotIsNumeric(c)then IsValidEmail=false exitfunction endif next ifLeft(name,1)="."orRight(name,1)="."then IsValidEmail=false exitfunction endi
6、fnextifInStr(names(1),".")<=0then IsValidEmail=false exitfunctionendifi=Len(names(1))-InStrRev(names(1),".")ifi<>2andi<>3then IsValidEmail=false exitfunctionendififInStr(email,"..")>0then IsValidEmail=falseendifendfunction%>第三种办法,用下面这个函数进行判断。它会检查邮件地址是否含有“@”,以及“.”是否在“@”后面:3/3functionisEmail
7、(pInString) lAt=False lDot=false forx=2tolen(pInstring)-1 ifmid(pInString,x,1)="@"thenlAt=True ifmid(pInString,x,1)="."andlAt=TruethenlDot=True next iflAt=TrueandlDot=Truethen isEmail=True else isEmail=False endifendfunction3/3