文章提供一款简单的asp 用户登录代码,提供了从数据库教程连接到文件的登录以及用户在输入信息时js判断验证哈。
<%
'dim databasename,conn,constr
databasename="userman.mdb"
constr="provider=microsoft.jet.oledb.4.0;data source=" & server.mappath(databasename)
set conn=server.createobject("adodb.connection")
conn.open constr
%>
登录页面文件
<html>
<head>
<title>请输入用户名和密码</title>
</head>
<body>
<script language = "vbscript">
function chkfields()
name = document.myform.username.value
if document.myform.username.value ="" then
window.alert("请输入用户名!")
return false
end if
if document.myform.userpwd.value ="" then
window.alert("请输入密码!")
return false
end if
if document.myform.email.value = "" then
window.alert("请输入电子邮件!")
return false
end if
return true
end function
</script>
<p align = "center"><font color="#0000ff" size="5">用户注册</font></p>
<p align = "center"><font color = "#800000">
</font></p>
<form method = "post" action ="verify.asp" name = "myform">
<center>
<table border="0" width="78%">
<tr>
<td width="27%" bgcolor="#c0c0c0" align="center"><font size="2">用 户 名:</font></td>
<td width="73%"><input type = "text" name = "username" size = 20> <font size="2" color="#ff0000">*用户名只能输入大写或小写字母</font></td>
</tr>
<tr>
<td width="27%" bgcolor="#c0c0c0" align="center"><font size="2">密 码:</font></td>
<td width="73%"><input type = "password" name = "userpwd" size = 20> <font size="2" color="#ff0000">*密码只能输入数字</font></td>
</tr>
<tr>
<td width="27%" bgcolor="#c0c0c0" align="center"><font size="2">电子邮件:</font></td>
<td width="73%"><input type = "text" name = "email" size = 20> <font size="2" color="#ff0000">*</font><font size="2" color="#ff0000">请填写正确的邮件格式</font></td>
</tr>
</table>
<p align = "center"><input type = "submit" value = "提交" name = "b1" onclick = chkfields>
<input type = "reset" value = "重置" name = "b2"></p>
</form>
<p align = "center"></p>
</body>
</html>
登录处理文件
<%
'读取从表单传递过来的身份数据
username=trim(request.form("username"))
userpwd=request.form("userpwd")
email=request.form("email")
if username="" or userpwd="" or email="" then
response.write "<script>alert('填写不完全!');history.back();</script>"
response.end
elseif not checkletter(username) then
response.write "<script>alert('输入的用户名没有全部是大写或小写字母!');history.back();</script>"
response.end
elseif not isnumeric(userpwd) then
response.write "<script>alert('输入的密码为非数字!');history.back();</script>"
response.end
elseif not isvalidemail(email) then
response.write "<script>alert('输入的邮件地址格式不正确!');history.back();</script>"
response.end
elseif len(username)<6 then
response.write "<script>alert('用户名长度至少6位!');history.back();</script>"
response.end
else
response.write "<center>输入正确!"
end if
function checkletter(str)
checkletter=true
letters="abcdefghijklmnopqrstuvwxyz"
for i=1 to len(str)
checkchar=ucase(mid(str,i,1)) 返回字符串str中第i个字符,并将其转换为大写
if (instr(letters,checkchar)<=0) then 'checkchar对应字符在letters中不存在
checkletter=false
exit function
end if
next
end function
function isvalidemail(email)
dim names,name,i,c
isvalidemail=true
names=split(email,"@") '使用@字符将email字符串分成几个子字符串并保存在names数组中
if ubound(names)<>1 then
'ubound函数返回数组names的最大下标,ubound(names)<>1表明email字符串中存在@字符且不只一个,所以email不是有效的邮件地址格式
isvalidemail=false
exit function
end if
for each name in names
if len(name)<=0 then
isvalidemail=false
exit function
end if
for i=1 to len(name)
c=lcase(mid(name,i,1))
'返回字符串name内第i个字符并将其转换成小写
if instr("abcdefghijklmnopqrstuvwxyz_-.",c)<=0 and not isnumeric(c) then
'instr函数返回指定字符串在另一字符串中第一次出现的位置。instr("abcdefghijklmnopqrstuvwxyz_-.",c)<=0表明字符c不在字符串"abcdefghijklmnopqrstuvwxyz_-."中
isvalidemail=false
exit function
end if
next
if left(name,1)="." or right(name,1)="." then
'返回字符串name最左边一个字符,返回字符串name最右边一个字符
isvalidemail=false
exit function
end if
next
if instr(names(1),".")<=0 then
'email字符串中@右边部分不包含字符”.”
isvalidemail=false
exit function
end if
i=len(names(1))-instrrev(names(1),".")
'instrrev函数返回指定字符串在另一个字符串中出现的从结尾计起的的位置,instrrev(names(1),".")得到字符"."在字符串names(1) 中从结尾计起的位置
if i<>2 and i<>3 then
'电子邮件最后一般为cn或com,长度为2或3
isvalidemail=false
exit function
end if
'email中存在字符串".."
if instr(email,"..")>0 then
isvalidemail=false
end if
end function
%>