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

字號:


    以網(wǎng)上down了一個(gè)asp動態(tài)生成form驗(yàn)證javascript的代碼,自己擴(kuò)容了一下。作者的大名我給忘了,不過我記得清楚,從第一條至第九條的代碼都是該作者的原創(chuàng)。至于九至第十五條卻是本人自己加的哦。而且由于本人水平有限,從第九條至第十五條不見得會條條生效???呵呵,其實(shí)把它貼在這里沒有什么必要的,因?yàn)檫@點(diǎn)小會伎倆誰都會用的,我把它的作用定位為:開發(fā)時(shí)找不到該函數(shù)時(shí)到這里拷貝一下就可以了。呵,因?yàn)槲壹礇]有刻錄機(jī)也沒有移動硬盤。實(shí)在是沒有辦法把這些代碼隨身攜帶,不過還好,在北京到處都可以上網(wǎng)。所以就把這個(gè)放在這里,用的時(shí)候ctrl+c,再ctrl+v好了。
    <%
    '****************************************************************
    '函數(shù)名稱:checkform_js(frmname,errstr)
    '功能:用asp的方法動態(tài)寫出javascript的表單驗(yàn)證的函數(shù)checksubmit()
    '使用方法:1、&vbcrlf&_
    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ù)字開頭的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項(xiàng)當(dāng)selectedindex!=0時(shí),第2項(xiàng)第3項(xiàng)被禁用!無須檢查其他兩項(xiàng)
    '當(dāng)級聯(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)用于檢查國家/省/市三項(xiàng)級聯(lián)菜單,當(dāng)國家不為中國時(shí),省市可不填,為中國時(shí),必須填寫省以及相對的市!
    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)開始!
    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%>
    >
    <%
    '==========================================================
    ' 表單驗(yàn)證實(shí)例
    '==========================================================
    '1、;
    '2、繪制表單:name= onsubmit = javascript: return checksubmit() 注意大小寫;
    '3、定義guo_error //一句只能出現(xiàn)一個(gè)“,”如位數(shù)和確認(rèn)密碼項(xiàng)須多加“|”指定參數(shù);
    '4、call checkform_js(formname,guo_error)
    'www.knowsky.com
    '==========================================================
    dim guo_error
    guo_error =text|0|文本項(xiàng)必須填寫!,
    guo_error = guo_error & number|0|數(shù)字項(xiàng)必須填寫且必須為數(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)必須填寫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ù)字開頭!,
    call checkform_js(form1,guo_error)
    '==========================================================
    ' 表單驗(yàn)證流程
    '==========================================================
    '1、通過split(guo_error,.)拆分至數(shù)組temparr();
    '2、通過split(temparr,|)拆分至數(shù)組temparr();
    '3、select case split(temparr(i+1),|)執(zhí)行驗(yàn)證及輸出錯(cuò)誤提示信息split(temparr(i+2));
    '4、javascript區(qū)分大小寫地,所以各表單組件的名稱都要注意書寫一致喲~~
    '==========================================================
    %>
    待驗(yàn)證的表單
    ***************************************************************
    以上代碼絕大多數(shù)已通過測試,不過是不是都好用,我只能說那個(gè)級聯(lián)的下拉菜單用起來不太好用??纯茨奈蝗市只騱eb美眉加以改進(jìn)嘍。呵呵