ASP動態(tài)生成的javascript表單驗證代碼

字號:


    以網(wǎng)上down了一個ASP動態(tài)生成Form驗證Javascript的代碼,自己擴容了一下。作者的大名我給忘了,不過我記得清楚,從第一條至第九條的代碼都是該作者的原創(chuàng)。至于九至第十五條卻是本人自己加的哦。而且由于本人水平有限,從第九條至第十五條不見得會條條生效???呵呵,其實把它貼在這里沒有什么必要的,因為這點小會伎倆誰都會用的,我把它的作用定位為:開發(fā)時找不到該函數(shù)時到這里拷貝一下就可以了。呵,因為我即沒有刻錄機也沒有移動硬盤。實在是沒有辦法把這些代碼隨身攜帶,不過還好,在北京到處都可以上網(wǎng)。所以就把這個放在這里,用的時候Ctrl+C,再Ctrl+V好了。
    <%
    '****************************************************************
    '函數(shù)名稱:CheckForm_JS(frmName,errStr)
    '功能:用ASP的方法動態(tài)寫出JavaScript的表單驗證的函數(shù)checkSubmit()
    '使用方法:1、"&vbCrlf&_
    ""
    Response.Write strShow
    End Sub
    Function findJS(frmName,errStr)
    Dim tmpArr
    Dim i
    '參數(shù)值
    i=0
    '獲取錯誤列表,建立數(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ù)字開頭的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格式的電話號碼
    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類型(電話號碼及傳真常用)
    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|請選擇所在省名稱!|selCountry|city|請選擇城市信息
    '注:級聯(lián)菜單第1項當(dāng)selectedIndex!=0時,第2項第3項被禁用!無須檢查其他兩項
    '當(dāng)級聯(lián)菜單第1項selectedIndex==0時,第2項的selectedIndex不能為0,第二項的selectedIndex!=0時,第3項的selectedIndex也不能為0
    '此項用于檢查國家/省/市三項級聯(lián)菜單,當(dāng)國家不為中國時,省市可不填,為中國時,必須填寫省以及相對的市!
    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類型必須選擇 格式:檢查的表單項|16|提示信息|關(guān)聯(lián)項"注:當(dāng)關(guān)聯(lián)項為第一項時,確認(rèn)開始!
    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
    %>
    ***********************************************************************
    使用范例:
    <" CODEPAGE="936"%>
    ">
    <%
    '==========================================================
    ' 表單驗證實例
    '==========================================================
    '1、;
    '2、繪制表單:name="" onsubmit = "Javascript: return checkSubmit()" 注意大小寫;
    '3、定義Guo_Error //一句只能出現(xiàn)一個“,”如位數(shù)和確認(rèn)密碼項須多加“|”指定參數(shù);
    '4、Call CheckForm_js("formname,Guo_Error)
    'www.knowsky.com
    '==========================================================
    Dim Guo_Error
    Guo_Error ="text|0|文本項必須填寫!,"
    Guo_Error = Guo_Error & "number|0|數(shù)字項必須填寫且必須為數(shù)字!,"
    Guo_Error = Guo_Error & "number|2|數(shù)字項必須為數(shù)字!,"
    Guo_Error = Guo_Error & "digital|3|位數(shù)項必須為6位!|6,"
    Guo_Error = Guo_Error & "moredigital|4|多位項必須大于4位!|4,"
    Guo_Error = Guo_Error & "email|5|Mail項必須填寫Email地址!,"
    Guo_Error = Guo_Error & "caractor|6|字符項必須為0-9A-Z的字符!,"
    Guo_Error = Guo_Error & "password2|7|確認(rèn)密碼必須與密碼項保持一致!|password1,"
    Guo_Error = Guo_Error & "listmenu|1|必須選擇!"
    Guo_Error = Guo_Error & "uid|8|用戶名不能以數(shù)字開頭!,"
    Call CheckForm_js("form1",Guo_Error)
    '==========================================================
    ' 表單驗證流程
    '==========================================================
    '1、通過split(Guo_Error,".")拆分至數(shù)組tempArr();
    '2、通過split(tempArr,"|")拆分至數(shù)組tempArr();
    '3、Select Case split(tempArr(i+1),"|")執(zhí)行驗證及輸出錯誤提示信息split(tempArr(i+2));
    '4、Javascript區(qū)分大小寫地,所以各表單組件的名稱都要注意書寫一致喲~~
    '==========================================================
    %>
    待驗證的表單
    ***************************************************************
    以上代碼絕大多數(shù)已通過測試,不過是不是都好用,我只能說那個級聯(lián)的下拉菜單用起來不太好用??纯茨奈蝗市只騑EB美眉加以改進嘍。呵呵