• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

用ASP的方法动态写出JavaScript的表单验_js

javascript 搞代码 7年前 (2018-06-13) 162次浏览 已收录 0个评论

<%
‘请转存为CheckForm_js.asp使用

‘*****************************************************************************
‘函数名称:CheckForm_JS(frmName,errStr)
‘功能:用ASP的方法动态写出JavaScript的表单验证的函数checkSubmit()
‘使用方法:1、<!–Include File=URL+本函数所在的页>;
‘         2、<form >;

‘*****************************************************************************
‘帮助:
‘—————————————————————————–
‘・参数说明:
‘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:必须为a-z或0-9的字符的Text类型
‘7:确认密码和密码必须相等的Text类型
‘8:必须为a-z或0-9或A-Z的字符Text类型
‘9:必须大于指定位数的Text类型
‘—————————————————————————–
%>
<%
Sub CheckForm_JS(frmName,errStr)
Dim tmpArr
Dim i
Dim strShow       ‘输出JS的字符串
  ‘获取错误列表,建立数组
  tmpArr=Split(errStr,”,”)
  ‘写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&_
          “//Powered by dovia.net”&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&_
          strShow&_
          “else”&vbCrlf&_
          “return true;”&vbCrlf&_
          “}”&vbCrlf&_
          “//–>”&vbCrlf&_
          “</script>”
   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
             ‘”else”&vbCrlf&_
             ‘”return true;”&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
             ‘”else”&vbCrlf&_
             ‘”return true;”&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
             ‘”else”&vbCrlf&_
             ‘”return true;”&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
             ‘”else”&vbCrlf&_
             ‘”return true;”&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
             ‘”else”&vbCrlf&_
             ‘”return true;”&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
             ‘”else”&vbCrlf&_
             ‘”return true;”&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
             ‘”else”&vbCrlf&_
             ‘”return true;”&vbCrlf
      Exit Function
    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&_
             “return false;”&vbCrlf&_
             “}”&vbCrlf
             ‘”else”&vbCrlf&_
             ‘”return true;”&vbCrlf
      Exit Function
       Case “8”  ‘必须为a-z或0-9或A-Z的字符的Text类型
      findJS=”if ((!namReg.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
             ‘”else”&vbCrlf&_
             ‘”return true;”&vbCrlf
      Exit Function
       Case “9”  ‘必须大于指定位数的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
             ‘”else”&vbCrlf&_
             ‘”return true;”&vbCrlf
      Exit Function
  End Select
End Function
%>

欢迎大家阅读《用ASP的方法动态写出JavaScript的表单验…_js,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:用ASP的方法动态写出JavaScript的表单验_js
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址