您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> asp编程 >> 正文

ASP动态生成的javascript表单验证代码

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 以网上down了一个ASP动态生成Form验证Javascript的代码,自己扩容了一下。作者的大名我给忘了,不过我记得清楚,从第一条至第九条的代码都是该作者的原创。至于九至第十五条却是本人自己加的哦。而且由于本人水平有限,从第九条至第十五条不见得会条条生效啊?呵呵,其实把它贴在这里没有什么必要的,因为这点小会伎俩谁都会用的,我把它的作用定位为:开发时找不到该函数时到这里拷贝一下就可以了。呵,因为我即没有刻录机也没有移动硬盘。实在是没有办法把这些代码随身携带,不过还好,在北京到处都可以上网。所以就把这个放在这里,用的时候Ctrl+C,再Ctrl+V好了。

<%
'****************************************************************
'函数名称:CheckForm_JS(frmName,errStr)
'功能:用ASP的方法动态写出JavaScript的表单验证的函数checkSubmit()
'使用方法:1、"&vbCrlf&_
  ""
   Response.Write strShow
End Sub

 

Function findJS(frmName,errStr) 
Dim tmpArr
Dim i
  '参数值
  i=0
  '获取错误列表,建立数组
  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"  '必须为数字的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"  '必须为指定位数的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"  '必须大于指定位数的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