ASP動(dòng)態(tài)生成的javascript表單驗(yàn)證代碼
'****************************************************************
'函數(shù)名稱:CheckForm_JS(frmName,errStr)
'功能:用ASP的方法動(dòng)態(tài)寫(xiě)出javascript的表單驗(yàn)證的函數(shù)checkSubmit()
'使用方法:1、<!--Include File=URL+本函數(shù)所在的頁(yè)>;
' 2、<form onsubmit="javascript:return checkSubmit()">;
'原作者已被忘卻,二次開(kāi)發(fā)作者:Guo.Q.M
'最后更新:2004
'****************************************************************
'幫助:
'---------------------------------------------------------------
'·參數(shù)說(shuō)明:
'frmName:表單域的名稱
'errStr:驗(yàn)證列表,如:"num|3|型號(hào)必須不小于8位|8,email|5|請(qǐng)輸入正確的email格式",這里
' num表示表單域名稱,3表示驗(yàn)證參數(shù),8表示不小于的位數(shù)(可選)
'
'·驗(yàn)證參數(shù)列表:
'0:必填的Text類型
'1:必填的ListMenu類型
'2:必須為數(shù)字的Text類型
'3:必須為指定位數(shù)的Text類型
'4:必須大于指定位數(shù)的Text類型
'5:必須為Email的Text類型
'6:必須為a-z或0-9的字符的Text類型
'7:確認(rèn)密碼和密碼必須相等的Text類型
'8:確認(rèn)不是以以數(shù)字開(kāi)頭的Text類型
'9:必須包含10-888888格式的Text類型
'10:不得包含中文、空格及其他非法字符的Text類型,即只能包含"_""-""0-9""a-z"A-Z"
'11:必須只包含數(shù)字及"-"在內(nèi)的Text類型
'12:必須為正確網(wǎng)址的Text類型
'13:必須小于指定位數(shù)的Text類型
'14:不得包含HTML標(biāo)記的Text類型
'15:確認(rèn)未被禁用的Select類型必須選擇 格式:檢查的表單項(xiàng)|15|提示信息|關(guān)聯(lián)項(xiàng)"
'注意:如有級(jí)聯(lián)菜單,請(qǐng)將級(jí)聯(lián)菜單的驗(yàn)證過(guò)程放到最后檢驗(yàn)!?。?!
'------------------------------------------------------------------------
%>
<%
Sub CheckForm_JS(frmName,errStr)
Dim tmpArr
Dim i
Dim strShow '輸出JS的字符串
'獲取錯(cuò)誤列表,建立數(shù)組
tmpArr=Split(errStr,",")
'寫(xiě)JS
for i=0 to UBound(tmpArr)
if i<>0 then
strShow=strShow&"else "&findJS(frmName,tmpArr(i))
else
strShow=strShow&findJS(frmName,tmpArr(i))
end if
next
'輸出
strShow="<script language=javascript>"&vbCrlf&_
"<!--"&vbCrlf&_
"http://Power by Guoquanman 2004"&vbCrlf&_
"function checkSubmit()"&vbCrlf&_
"{"&vbCrlf&_
"var emailReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;"&vbCrlf&_
"var pwdReg = /[a-z0-9]$/;"&vbCrlf&_
"var uidBeginReg = /^[0-9]+[_a-z0-9]/;"&vbCrlf&_
"var phoneReg = /\d{2}-\d{5}/;"&vbCrlf&_
"var phoneDetailReg = /[^0-9\-]/;"&vbCrlf&_
"var uidReg = /[^a-zA-Z0-9_\-]/;"&vbCrlf&_
"var htmlReg = /<(.*)>.*<\/\1>/;"&vbCrlf&_
"var re1 = /^http:\/\/[A-Za-z][A-Za-z0-9\-]*[A-Za-z]*\./;"&vbCrlf&_
"var re2 = /^http:\/\/[0-9]{1,5}[A-Za-z]*[0-9]*\./;"&vbCrlf&_
"var re3 = /\.{2,}/;"&vbCrlf&_
"var re4 = /\:{2,}/;"&vbCrlf&_
"var re5 = /\/{3,}/;"&vbCrlf&_
"var re6 = /\,+/;"&vbCrlf&_
"var re7 = /\!+/;"&vbCrlf&_
"var re8 = /\@+/;"&vbCrlf&_
"var re9 = /\#+/;"&vbCrlf&_
"var re10 = /\$+/;"&vbCrlf&_
"var re11 = /\^+/;"&vbCrlf&_
"var re12 = /\*+/;"&vbCrlf&_
"var re13 = /\|+/;"&vbCrlf&_
"var re14 = /\.[a-z0-9_&=?\/]*[A-Za-z0-9\/\~]{2,}$/;"&vbCrlf&_
strShow&_
"else"&vbCrlf&_
"return true;"&vbCrlf&_
"}"&vbCrlf&_
"http://-->"&vbCrlf&_
"</script>"
Response.Write strShow
End Sub
Function findJS(frmName,errStr)
Dim tmpArr
Dim i
'參數(shù)值
i=0
'獲取錯(cuò)誤列表,建立數(shù)組
tmpArr=Split(errStr,"|")
'輸出查詢條件
Select Case tmpArr(i+1)
Case "0" '必填的Text類型
findJS="if ((document."&frmName&"."&tmpArr(i)&".value)=="""")"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "1" '必填的ListMenu類型
findJS="if ((document."&frmName&"."&tmpArr(i)&".value)=="""")"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "2" '必須為數(shù)字的Text類型
findJS="if (isNaN(document."&frmName&"."&tmpArr(i)&".value))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "3" '必須為指定位數(shù)的Text類型
findJS="if (document."&frmName&"."&tmpArr(i)&".value.length!="&tmpArr(i+3)&")"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "4" '必須大于指定位數(shù)的Text類型
findJS="if (document."&frmName&"."&tmpArr(i)&".value.length<"&tmpArr(i+3)&")"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "5" '必須為Email的Text類型
findJS="if ((!emailReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "6" '必須為a-z或0-9的字符的Text類型
findJS="if ((!pwdReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "7" '確認(rèn)密碼和密碼必須相等的Text類型
findJS="if ((document."&frmName&"."&tmpArr(i)&".value)!=(document."&frmName&"."&tmpArr(i+3)&".value))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "8" '確認(rèn)以數(shù)字開(kāi)頭的Text類型
findJS="if ((uidBeginReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "9" '確認(rèn)10-101212格式的電話號(hào)碼
findJS="if ((!phoneReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "10" '確認(rèn)只包含英文字母及"-","_"在內(nèi)的Text。(即不包括中文及其他特殊字符)
findJS="if ((uidReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "11" '確認(rèn)只包含數(shù)字及"-"在內(nèi)的Text類型(電話號(hào)碼及傳真常用)
findJS="if ((phoneDetailReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "12" '確認(rèn)是否為有效網(wǎng)址!
findJS="if (((!re1.test(document."&frmName&"."&tmpArr(i)&".value))&&(!re2.test(document."&frmName&"."&tmpArr(i)&".value))"&_
"&&(document."&frmName&"."&tmpArr(i)&".value!=''))||"&_
"(re3.test(document."&frmName&"."&tmpArr(i)&".value))||(re4.test(document."&frmName&"."&tmpArr(i)&".value))||"&_
"(re5.test(document."&frmName&"."&tmpArr(i)&".value))||(re6.test(document."&frmName&"."&tmpArr(i)&".value))||"&_
"(re7.test(document."&frmName&"."&tmpArr(i)&".value))||(re8.test(document."&frmName&"."&tmpArr(i)&".value))||"&_
"(re9.test(document."&frmName&"."&tmpArr(i)&".value))||(re10.test(document."&frmName&"."&tmpArr(i)&".value))||"&_
"(re11.test(document."&frmName&"."&tmpArr(i)&".value))||(re12.test(document."&frmName&"."&tmpArr(i)&".value))||"&_
"(re13.test(document."&frmName&"."&tmpArr(i)&".value))||(!re14.test(document."&frmName&"."&tmpArr(i)&".value))"&_
"&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "13" '確認(rèn)不大于固定位數(shù)的Text類型
findJS="if (document."&frmName&"."&tmpArr(i)&".value.length>"&tmpArr(i+3)&")"&vbCrlf&_
"{"&vbCrlf&_
"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "14" '確認(rèn)含有HTML標(biāo)記的Text類型
findJS="if(htmlReg.test(document."&frmName&"."&tmpArr(i)&".value))"&vbCrlf&_
"{"&vbCrlf&_
"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "15"
'==========================================================
' 確認(rèn)未被禁用的Select類型必須選擇 格式:state|15|請(qǐng)選擇所在省名稱!|selCountry|city|請(qǐng)選擇城市信息
'注:級(jí)聯(lián)菜單第1項(xiàng)當(dāng)selectedIndex!=0時(shí),第2項(xiàng)第3項(xiàng)被禁用!無(wú)須檢查其他兩項(xiàng)
'當(dāng)級(jí)聯(lián)菜單第1項(xiàng)selectedIndex==0時(shí),第2項(xiàng)的selectedIndex不能為0,第二項(xiàng)的selectedIndex!=0時(shí),第3項(xiàng)的selectedIndex也不能為0
'此項(xiàng)用于檢查國(guó)家/省/市三項(xiàng)級(jí)聯(lián)菜單,當(dāng)國(guó)家不為中國(guó)時(shí),省市可不填,為中國(guó)時(shí),必須填寫(xiě)省以及相對(duì)的市!
findJS="if (document."&frmName&"."&tmpArr(i+3)&".selectedIndex ==0)"&vbCrlf&_
"{"&vbCrlf&_
"if (document."&frmName&"."&tmpArr(i)&".selectedIndex ==0)"&vbCrlf&_
"{"&vbCrlf&_
"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus;"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf&_
"else if (document."&frmName&"."&tmpArr(i)&".selectedIndex != 0)"&vbCrlf&_
"{"&vbCrlf&_
"if (document."&frmName&"."&tmpArr(i+4)&".selectedIndex == 0)"&vbCrlf&_
"{"&vbCrlf&_
"window.alert('"&tmpArr(i+5)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i+4)&".focus;"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf&_
"}"&vbCrlf&_
"}"&vbCrlf
Exit Function
Case "16" '確認(rèn)未被禁用的Select類型必須選擇 格式:檢查的表單項(xiàng)|16|提示信息|關(guān)聯(lián)項(xiàng)"注:當(dāng)關(guān)聯(lián)項(xiàng)為第一項(xiàng)時(shí),確認(rèn)開(kāi)始!
findJS="if (document."&frmName&"."&tmpArr(i+3)&".selectedIndex != 0)"&vbCrlf&_
"{"&vbCrlf&_
"if (document."&frmName&"."&tmpArr(i)&".selectedIndex == 0)"&vbCrlf&_
"{"&vbCrlf&_
"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus;"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&vbCrlf&_
"}"&vbCrlf
Exit Function
End Select
End Function
%>
***********************************************************************
使用范例:
<!--#Include file = "inc/check_formJS.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
'==========================================================
' 表單驗(yàn)證實(shí)例
'==========================================================
'1、<!--#Include file = "inc/check_formJS.asp"-->;
'2、繪制表單:name="" onsubmit = "javascript: return checkSubmit()" 注意大小寫(xiě);
'3、定義Guo_Error //一句只能出現(xiàn)一個(gè)“,”如位數(shù)和確認(rèn)密碼項(xiàng)須多加“|”指定參數(shù);
'4、Call CheckForm_js("formname,Guo_Error)
'www.china-u.com
'==========================================================
Dim Guo_Error
Guo_Error ="text|0|文本項(xiàng)必須填寫(xiě)!,"
Guo_Error = Guo_Error & "number|0|數(shù)字項(xiàng)必須填寫(xiě)且必須為數(shù)字!,"
Guo_Error = Guo_Error & "number|2|數(shù)字項(xiàng)必須為數(shù)字!,"
Guo_Error = Guo_Error & "digital|3|位數(shù)項(xiàng)必須為6位!|6,"
Guo_Error = Guo_Error & "moredigital|4|多位項(xiàng)必須大于4位!|4,"
Guo_Error = Guo_Error & "email|5|Mail項(xiàng)必須填寫(xiě)Email地址!,"
Guo_Error = Guo_Error & "caractor|6|字符項(xiàng)必須為0-9A-Z的字符!,"
Guo_Error = Guo_Error & "password2|7|確認(rèn)密碼必須與密碼項(xiàng)保持一致!|password1,"
Guo_Error = Guo_Error & "listmenu|1|必須選擇!"
Guo_Error = Guo_Error & "uid|8|用戶名不能以數(shù)字開(kāi)頭!,"
Call CheckForm_js("form1",Guo_Error)
'==========================================================
' 表單驗(yàn)證流程
'==========================================================
'1、通過(guò)split(Guo_Error,".")拆分至數(shù)組tempArr();
'2、通過(guò)split(tempArr,"|")拆分至數(shù)組tempArr();
'3、Select Case split(tempArr(i+1),"|")執(zhí)行驗(yàn)證及輸出錯(cuò)誤提示信息split(tempArr(i+2));
'4、javascript區(qū)分大小寫(xiě)地,所以各表單組件的名稱都要注意書(shū)寫(xiě)一致喲~~
'==========================================================
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>表單驗(yàn)證實(shí)例</title>
</head>
<body>
<form name="form1" method="post" action="check_form.asp" onsubmit="javascript:return checkSubmit()">
<p align="center">待驗(yàn)證的表單</p>
***************************************************************
以上代碼絕大多數(shù)已通過(guò)測(cè)試,不過(guò)是不是都好用,我只能說(shuō)那個(gè)級(jí)聯(lián)的下拉菜單用起來(lái)不太好用??纯茨奈蝗市只騑EB美眉加以改進(jìn)嘍。呵呵
- javascript Zifa FormValid 0.1表單驗(yàn)證 代碼打包下載
- 收藏的js表單驗(yàn)證控制代碼大全
- JavaScript 表單驗(yàn)證正則表達(dá)式大全[推薦]
- JavaScript 使用正則表達(dá)式進(jìn)行表單驗(yàn)證的示例代碼
- js 常用正則表達(dá)式表單驗(yàn)證代碼
- jquery validate.js表單驗(yàn)證的基本用法入門(mén)
- js 回車(chē)提交表單兩種實(shí)現(xiàn)方法
- jquery ajax提交表單從action傳值到j(luò)sp實(shí)現(xiàn)小結(jié)
- 巧用js提交表單輕松解決一個(gè)頁(yè)面有多個(gè)提交按鈕
- js模擬點(diǎn)擊以提交表單為例兼容主流瀏覽器
- JS中的form.submit()不能提交表單的錯(cuò)誤原因
- angularJS提交表單(form)
- js實(shí)現(xiàn)鍵盤(pán)Enter鍵提交表單的方法
- Javascript的表單驗(yàn)證-提交表單
相關(guān)文章
判斷dll文件是否已經(jīng)注冊(cè)的ASP函數(shù)代碼
有時(shí)候需要判斷服務(wù)器是否按照了dll文件,那么就可以參考下面的代碼進(jìn)行判斷。2011-09-09AJAX簡(jiǎn)單應(yīng)用實(shí)例-彈出層
AJAX的簡(jiǎn)單應(yīng)用,包括js彈出DIV層、層拖動(dòng)、ajax后臺(tái)讀取數(shù)據(jù)!注意getinfo.asp文件是以u(píng)tf-8格式保存的,在getinfo.asp里面可以寫(xiě)讀數(shù)據(jù)庫(kù)代碼!2008-08-08asp漢字中文圖片驗(yàn)證碼的實(shí)現(xiàn)代碼
asp漢字中文圖片驗(yàn)證碼的實(shí)現(xiàn)代碼...2007-11-11asp MYSQL出現(xiàn)問(wèn)號(hào)亂碼的解決方法
用asp讀取MYSQL數(shù)據(jù)庫(kù)出現(xiàn)亂碼,讀取到的漢字都是????2008-08-08asp短信接口源碼 比較簡(jiǎn)單但也挺實(shí)用用的(兩個(gè)寫(xiě)法)
這是關(guān)于短信接口的一些調(diào)用方法,asp的,兩個(gè)方式,調(diào)用短信接口都比較簡(jiǎn)單,但也比較實(shí)用2012-01-01asp實(shí)現(xiàn)新評(píng)論自動(dòng)發(fā)短信提示的代碼
前幾天看到有幾個(gè)wordpress都有有新評(píng)論自動(dòng)發(fā)短信提示的功能,我覺(jué)得不錯(cuò),對(duì)于不經(jīng)常在線的管理員特別有幫助,如果有人發(fā)非法信息一下子就知道了,而且發(fā)信息是免費(fèi)的。2009-11-11ASP 代碼出現(xiàn)80040e14錯(cuò)誤的解決方法
本文減淡概述了解決80040e14錯(cuò)誤的兩種方法。2009-04-04CreateKeyWord asp實(shí)現(xiàn)的由給定的字符串生成關(guān)鍵字的代碼
CreateKeyWord asp實(shí)現(xiàn)的由給定的字符串生成關(guān)鍵字的代碼...2007-09-09