以網上down了一個ASP動態生成Form驗證javascript的代碼,自己擴容了一下。作者的大名我給忘了,不過我記得清楚,從第一條至第九條的代碼都是該作者的原創。至於九至第十五條卻是本人自己加的哦。而且由於本人水平有限,從第九條至第十五條不見得會條條生效啊?呵呵,其實把它貼在這裡沒有什麼必要的,因為這點小會伎倆誰都會用的,我把它的作用定位為:開發時找不到該函數時到這裡拷貝一下就可以了。呵,因為我即沒有刻錄機也沒有移動硬盤。實在是沒有辦法把這些代碼隨身攜帶,不過還好,在北京到處都可以上網。所以就把這個放在這裡,用的時候Ctrl+C,再Ctrl+V好了。
<% '****************************************************************
'函數名稱:CheckForm_JS(frmName,errStr)
'功能:用ASP的方法動態寫出javascript的表單驗證的函數checkSubmit()
'使用方法:1、<!--IncludeFile=URL+本函數所在的頁>;
'2、<formonsubmit="javascript:returncheckSubmit()">;
'原作者已被忘卻,二次開發作者:Guo.QM
'最後更新:2004
'****************************************************************
'幫助:
'---------------------------------------------------------------
'·參數說明:
'frmName:表單域的名稱
'errStr:驗證列表,如:"num|3|型號必須不小於8位|8,email|5|請輸入正確的email格式",這裡
'num表示表單域名稱,3表示驗證參數,8表示不小於的位數(可選)
'
'·驗證參數列表:
'0:必填的Text類型
'1:必填的ListMenu類型
'2:必須為數字的Text類型
'3:必須為指定位數的Text類型
'4:必須大於指定位數的Text類型
'5:必須為Email的Text類型
'6:必須為az或0-9的字符的Text類型
'7:確認密碼和密碼必須相等的Text類型
'8:確認不是以以數字開頭的Text類型
'9:必須包含10-888888格式的Text類型
'10:不得包含中文、空格及其他非法字符的Text類型,即只能包含"_""-""0-9""az"AZ"
'11:必須只包含數字及"-"在內的Text類型
'12:必須為正確網址的Text類型
'13:必須小於指定位數的Text類型
'14:不得包含HTML標記的Text類型
'15:確認未被禁用的Select類型必須選擇格式:檢查的表單項|15|提示信息|關聯項"
'注意:如有級聯菜單,請將級聯菜單的驗證過程放到最後檢驗! ! ! !
'------------------------------------------------------------------------
%>
<%
SubCheckForm_JS(frmName,errStr)
DimtmpArr
Dimi
DimstrShow'輸出JS的字符串
'獲取錯誤列表,建立數組
tmpArr=Split(errStr,",")
'寫JS
fori=0toUBound(tmpArr)
ifi<>0then
strShow=strShow&"else"&findJS(frmName,tmpArr(i))
else
strShow=strShow&findJS(frmName,tmpArr(i))
endif
next
'輸出
strShow="<scriptlanguage=javascript>"&vbCrlf&_
"<!--"&vbCrlf&_
"//PowerbyGuoquanman2004"&vbCrlf&_
"functioncheckSubmit()"&vbCrlf&_
"{"&vbCrlf&_
"varemailReg=/^[_a-z0-9]+@([_a-z0-9]+/.)+[a-z0-9]{2,3}$/;"&vbCrlf&_
"varpwdReg=/[a-z0-9]$/;"&vbCrlf&_
"varuidBeginReg=/^[0-9]+[_a-z0-9]/;"&vbCrlf&_
"varphoneReg=//d{2}-/d{5}/;"&vbCrlf&_
"varphoneDetailReg=/[^0-9/-]/;"&vbCrlf&_
"varuidReg=/[^a-zA-Z0-9_/-]/;"&vbCrlf&_
"varhtmlReg=/<(.*)>.*<///1>/;"&vbCrlf&_
"varre1=/^http:////[A-Za-z][A-Za-z0-9/-]*[A-Za-z]*/./;"&vbCrlf&_
"varre2=/^http:////[0-9]{1,5}[A-Za-z]*[0-9]*/./;"&vbCrlf&_
"varre3=//.{2,}/;"&vbCrlf&_
"varre4=//:{2,}/;"&vbCrlf&_
"varre5=///{3,}/;"&vbCrlf&_
"varre6=//,+/;"&vbCrlf&_
"varre7=//!+/;"&vbCrlf&_
"varre8=//@+/;"&vbCrlf&_
"varre9=//#+/;"&vbCrlf&_
"varre10=//$+/;"&vbCrlf&_
"varre11=//^+/;"&vbCrlf&_
"varre12=//*+/;"&vbCrlf&_
"varre13=//|+/;"&vbCrlf&_
"varre14=//.[a-z0-9_&=?//]*[A-Za-z0-9///~]{2,}$/;"&vbCrlf&_
strShow&_
"else"&vbCrlf&_
"returntrue;"&vbCrlf&_
"}"&vbCrlf&_
"//-->"&vbCrlf&_
"</script>"
Response.WritestrShow
EndSub
FunctionfindJS(frmName,errStr)
DimtmpArr
Dimi
'參數值
i=0
'獲取錯誤列表,建立數組
tmpArr=Split(errStr,"|")
'輸出查詢條件
SelectCasetmpArr(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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"1"'必填的ListMenu類型
findJS="if((document."&frmName&"."&tmpArr(i)&".value)=="""")"&vbCrlf&_
"{"&vbCrlf&_
"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"2"'必須為數字的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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"3"'必須為指定位數的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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"4"'必須大於指定位數的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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"6"'必須為az或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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"7"'確認密碼和密碼必須相等的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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"8"'確認以數字開頭的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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"9"'確認10-101212格式的電話號碼
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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"10"'確認只包含英文字母及"-","_"在內的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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"11"'確認只包含數字及"-"在內的Text類型(電話號碼及傳真常用)
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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"12"'確認是否為有效網址!
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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"13"'確認不大於固定位數的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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"14"'確認含有HTML標記的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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"15"
'==========================================================
'確認未被禁用的Select類型必須選擇格式:state|15|請選擇所在省名稱! |selCountry|city|請選擇城市信息
'注:級聯菜單第1項當selectedIndex!=0時,第2項第3項被禁用!無須檢查其他兩項
'當級聯菜單第1項selectedIndex==0時,第2項的selectedIndex不能為0,第二項的selectedIndex!=0時,第3項的selectedIndex也不能為0
'此項用於檢查國家/省/市三項級聯菜單,當國家不為中國時,省市可不填,為中國時,必須填寫省以及相對的市!
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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf&_
"elseif(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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf&_
"}"&vbCrlf&_
"}"&vbCrlf
ExitFunction
Case"16"'確認未被禁用的Select類型必須選擇格式:檢查的表單項|16|提示信息|關聯項"注:當關聯項為第一項時,確認開始!
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&_
"returnfalse;"&vbCrlf&_
"}"&vbCrlf&_
"}"&vbCrlf
ExitFunction
EndSelect
EndFunction
%>