ASP判断E-Mail的合法性,以及过滤邮箱字符

作者:网络 来源:佚名 更新时间:2008-02-05 18:19:28 点击:
'作者:loster(oicq:181306) [如果转载请勿删除此信息,谢谢!]
'函数名:chk_email()

'返回值:布尔值(true为通过,false为未通过)

'参数:email(需要判断的email,类型:字符串) type_1(是否需要判断@之后的domain,类型:布尔值)

'type_2(是否只能是特定域的e-mail注册,类型:布尔值) type_3(一个e-mail是否只能注册一次,类型:布尔值)



const c_maildomain=".com,.com.cn,.net,.net.cn,.org,.org.cn,.gov,.gov.cn,.edu,.edu.cn,.cn,.cc,.biz,.info" '电子邮件可接受的domain


const c_lockdomain="bit.edu.cn,loster.org" '特定的域

function chk_email(email,type_1,type_2,type_3)
dim i,k
dim at
dim email_1,temp

if email="" then
chk_email=false
exit function
end if

email_1=cstr(trim(email))

if len(email_1)<5 then
chk_email=false
exit function
end if

at=false

for i=1 to len(email_1)
temp=mid(email_1,i,1)
if temp="@" then
at=true
exit for
end if
next

if at=false then
chk_email=false
exit function
end if

k=0

for i=1 to len(email_1)
temp=mid(email_1,i,1)
if temp="." then
k=k+1
end if
next

if k=0 or k>2 then
chk_email=false
exit function
end if


if type_1=true then

for i=1 to len(email_1)
temp=mid(email_1,i,1)
if temp="@" then
k=i
exit for
end if
next

for i=k to len(email_1)
temp=mid(email_1,i,1)
if temp="." then
k=i
exit for
end if
next

temp=""

for i=k to len(email_1)
temp=temp+mid(email_1,i,1)
next

temp=cstr(trim(temp))
at=false

for i=0 to r_reader(c_maildomain,",")
if temp=reader(i) then
at=true
exit for
end if
next

erase reader

if at=false then
chk_email=false
exit function
end if

end if

if type_2=true then
for i=1 to len(email_1)
temp=mid(email_1,i,1)
if temp="@" then
k=i
exit for
end if
next

temp=""

for i=k+1 to len(email_1)
temp=temp+mid(email_1,i,1)
next

temp=cstr(trim(temp))

at=false
for i=0 to r_reader(c_lockdomain,",")
if temp=reader(i) then
at=true
exit for
end if
next

erase reader

if at=false then
chk_email=false
exit function
end if
end if

if type_3=true then
dim j
call greate_userdb()
call create_rs("select * from user_basic_info where user_email='"&email_1&"'",1,1,"user")
if rs.bof=false then
chk_email=false
call close_rs()
call close_userdb()
exit function
end if
call close_rs()
call close_userdb()
end if
chk_email=true
end function



dim user_db,user_driver
dim user_conn

user_db="data/userdata.mdb"

'建立user_conn的过程
sub greate_userdb()
user_driver="driver={microsoft access driver (*.mdb)}"
user_driver=user_driver&";dbq="&server.mappath(user_db)
'response.write (user_driver)
set user_conn = server.createobject("adodb.connection")
user_conn.open(user_driver)
end sub

'关闭user_conn的过程
sub close_userdb()
user_conn.close
set user_conn=nothing
end sub

dim reader
dim rs

'读取常数,并将每个常数保存在reader数组中,参数:r_str(常数串),f_str(分隔符),返回值:reader数组的下标
function r_reader(r_str,f_str)
dim i

if r_str="" or f_str="" then
exit function
end if

reader=split(r_str,f_str)

for i=0 to ubound(reader,1)
reader(i)=cstr(trim(reader(i)))
next
r_reader=ubound(reader,1)
end function