文章提供一款簡單的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
%>