ASP动态生成的javascript表单验证代码
来源:CSDN 发布时间:2008-10-13 20:11:00
标签:验证,表单,javascript,asp
函数名称:CheckForm_JS(frmName,errStr)
功能:用ASP的方法动态写出JavaScript的表单验证的函数checkSubmit()
使用方法:
1、<!--Include File=URL+本函数所在的页>;
2、<form onsubmit="javascript:return checkSubmit()">;
原作者已被忘却,二次开发作者:Guo.Q.M
帮助:
·参数说明:
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:确认不是以以数字开头的Text类型
9:必须包含10-888888格式的Text类型
10:不得包含中文、空格及其他非法字符的Text类型,即只能包含"_""-""0-9""a-z"A-Z"
11:必须只包含数字及"-"在内的Text类型
12:必须为正确网址的Text类型
13:必须小于指定位数的Text类型
14:不得包含HTML标记的Text类型
15:确认未被禁用的Select类型必须选择 格式:检查的表单项|15|提示信息|关联项"
注意:如有级联菜单,请将级联菜单的验证过程放到最后检验!!!!
<%
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&_
"//Power by Guoquanman 2004"&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&_
"var uidBeginReg = /^[0-9]+[_a-z0-9]/;"&vbCrlf&_
"var phoneReg = /\d{2}-\d{5}/;"&vbCrlf&_
"var phoneDetailReg = /[^0-9\-]/;"&vbCrlf&_
"var uidReg = /[^a-zA-Z0-9_\-]/;"&vbCrlf&_
"var htmlReg = /<(.*)>.*<\/\1>/;"&vbCrlf&_
"var re1 = /^http:\/\/[A-Za-z][A-Za-z0-9\-]*[A-Za-z]*\./;"&vbCrlf&_
"var re2 = /^http:\/\/[0-9]{1,5}[A-Za-z]*[0-9]*\./;"&vbCrlf&_
"var re3 = /\.{2,}/;"&vbCrlf&_
"var re4 = /\:{2,}/;"&vbCrlf&_
"var re5 = /\/{3,}/;"&vbCrlf&_
"var re6 = /\,+/;"&vbCrlf&_
"var re7 = /\!+/;"&vbCrlf&_
"var re8 = /\@+/;"&vbCrlf&_
"var re9 = /\#+/;"&vbCrlf&_
"var re10 = /\$+/;"&vbCrlf&_
"var re11 = /\^+/;"&vbCrlf&_
"var re12 = /\*+/;"&vbCrlf&_
"var re13 = /\|+/;"&vbCrlf&_
"var re14 = /\.[a-z0-9_&=?\/]*[A-Za-z0-9\/\~]{2,}$/;"&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
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 ('"&tmpArr(i+2)&"');"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_
"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_
"return false;"&vbCrlf&_
"}"&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
Exit Function
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&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
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&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
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&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
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&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
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&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
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&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
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&_
"return false;"&vbCrlf&_
"}"&vbCrlf
Exit Function
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&_
"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" '确认未被禁用的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&_
"return false;"&vbCrlf&_
"}"&vbCrlf&_
"}"&vbCrlf
Exit Function
End Select
End Function
%>
使用范例:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#Include file = "inc/check_formJS.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%
'表单验证实例
'1、<!--#Include file = "inc/check_formJS.asp"-->;
'2、绘制表单:name="" onsubmit = "Javascript: return checkSubmit()" 注意大小写;
'3、定义Guo_Error //一句只能出现一个“,”如位数和确认密码项须多加“|”指定参数;
'4、Call CheckForm_js("formname,Guo_Error)
Dim Guo_Error
Guo_Error ="text|0|文本项必须填写!,"
Guo_Error = Guo_Error & "number|0|数字项必须填写且必须为数字!,"
Guo_Error = Guo_Error & "number|2|数字项必须为数字!,"
Guo_Error = Guo_Error & "digital|3|位数项必须为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|确认密码必须与密码项保持一致!|password1,"
Guo_Error = Guo_Error & "listmenu|1|必须选择!"
Guo_Error = Guo_Error & "uid|8|用户名不能以数字开头!,"
Call CheckForm_js("form1",Guo_Error)
' 表单验证流程
'1、通过split(Guo_Error,".")拆分至数组tempArr();
'2、通过split(tempArr,"|")拆分至数组tempArr();
'3、Select Case split(tempArr(i+1),"|")执行验证及输出错误提示信息split(tempArr(i+2));
'4、Javascript区分大小写地,所以各表单组件的名称都要注意书写一致哟~~
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>表单验证实例</title>
</head>
<body>
<form name="form1" method="post" action="check_form.asp" onsubmit="JavaScript:return checkSubmit()">
<p align="center">待验证的表单</p>


猜你喜欢
- 1.c#可以调用msyql的导入导出命令,但是需要先判断客户机是否安装了mysql,及其安装mysql的路径问题。2.查询mysql安装路径
- 具体就不做详细讲解了,直接上代码:<!DOCTYPE html><html><head><meta
- 注:Unicode相关知识的详细介绍请参考UTF-8, UTF-16, UTF-32 & BOM。 对于UTF-8/16/32而言,
- websocket在vue2中的封装使用先说需求: 页面中有websocket连接,进入的时候发送参数到后端,后端发送消息, 离开页面时发送
- PHP+MySQL的组合是构建网站的一个常见搭配,不过如何使用PHP通过Web访问MySQL数据库呢?下面从Web数据库架构的工作原理讲起。
- 代码如下:--PK select * from sys.key_constraints where object_id = OBJECT_
- 本文实例讲述了PHP获取当前相对于域名目录的方法。分享给大家供大家参考。具体如下:http://127.0.0.1/dev/classd/i
- 本文介绍了三种跨域访问的方法,php,asp及jsp种访问远程文件的方法。这几天脑细胞剩下的不多了,不过问题都一个个解决了。我希望搜索引擎能
- Fucklt.py 使用了最先进的技术能够使你的代码不管里面有什么样的错误,你只管 FuckIt,程序就能"正常"执行,
- 仿射密码Affine Cipher是Multiplicative Cipher和Caesar Cipher算法的组合.仿射密码的基本实现如下
- Java 正则表达式判断字符串是否以字符开始:public static boolean startWithChar(String s) {
- 注意,一般官方接口都带有可导功能,如果你实现的层不具有可导功能,就需要自己实现梯度的反向传递。官方Linear层:class Linear(
- 在Oracle 8i版本之前,使用internal用户来执行数据库的启动和关闭以及create database等操作;从8i版本以后,Or
- 1、问题:安装VSCode后打开,发现显示的语言为英文,想显示为中文?2、解决方法:2.1、快捷键CTRL+SHIFT+P,找到如下设置Co
- 个人认为python的paramiko模块是运维人员必学模块之一,其ssh登录功能是旅行居家必备工具。安装paramiko很简单,pip i
- 系统默认是torch.FloatTensor类型data = torch.Tensor(2,3)是一个2*3的张量,类型为FloatTens
- Spring @Enable 模块概览框架实现@Enable注解模块激活模块Spring Framework@EnableWebMvcWeb
- 如下所示:from statsmodels.tsa.stattools import adfullerprint(adfuller(data
- 旧版本的代码请见上一篇博文: Python实现带图形界面的炸金花游戏本文尝试在旧版本的基础上,“升级&
- 本文实例讲述了php mysql procedure实现获取多个结果集的方法。分享给大家供大家参考,具体如下:protected funct