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

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

 1   
 2'****************************************************************   
 3'函数名称:CheckForm_JS(frmName,errStr)   
 4'功能:用ASP的方法动态写出JavaScript的表单验证的函数checkSubmit()   
 5'使用方法:1、
 6<!--Include File=URL+本函数所在的页>;   
 7' 2、<form onsubmit="javascript:return checkSubmit()">;   
 8'原作者已被忘却,二次开发作者:Guo.Q.M   
 9'最后更新:2004   
10'****************************************************************   
11'帮助:   
12'---------------------------------------------------------------   
13'·参数说明:   
14'frmName:表单域的名称   
15'errStr:验证列表,如:"num|3|型号必须不小于8位|8,email|5|请输入正确的email格式",这里   
16' num表示表单域名称,3表示验证参数,8表示不小于的位数(可选)   
17'   
18'·验证参数列表:   
19'0:必填的Text类型   
20'1:必填的ListMenu类型   
21'2:必须为数字的Text类型   
22'3:必须为指定位数的Text类型   
23'4:必须大于指定位数的Text类型   
24'5:必须为Email的Text类型   
25'6:必须为a-z或0-9的字符的Text类型   
26'7:确认密码和密码必须相等的Text类型   
27'8:确认不是以以数字开头的Text类型   
28'9:必须包含10-888888格式的Text类型   
29'10:不得包含中文、空格及其他非法字符的Text类型,即只能包含"_""-""0-9""a-z"A-Z"   
30'11:必须只包含数字及"-"在内的Text类型   
31'12:必须为正确网址的Text类型   
32'13:必须小于指定位数的Text类型   
33'14:不得包含HTML标记的Text类型   
34'15:确认未被禁用的Select类型必须选择 格式:检查的表单项|15|提示信息|关联项"   
35'注意:如有级联菜单,请将级联菜单的验证过程放到最后检验!!!!   
36'------------------------------------------------------------------------   
  1   
  2Sub CheckForm_JS(frmName,errStr)   
  3Dim tmpArr   
  4Dim i   
  5Dim strShow '输出JS的字符串   
  6'获取错误列表,建立数组   
  7tmpArr=Split(errStr,",")   
  8'写JS   
  9for i=0 to UBound(tmpArr)   
 10if i<>0 then   
 11strShow=strShow&"else "&findJS(frmName,tmpArr(i))   
 12else   
 13strShow=strShow&findJS(frmName,tmpArr(i))   
 14end if   
 15next   
 16'输出   
 17strShow="<script language=javascript>"&vbCrlf&_   
 18"<!--"&vbCrlf&_   
 19"//Power by Guoquanman 2004"&vbCrlf&_   
 20"function checkSubmit()"&vbCrlf&_   
 21"{"&vbCrlf&_   
 22"var emailReg = /^[_a-z0-9]+@([_a-z0-9]+\\.)+[a-z0-9]{2,3}$/;"&vbCrlf&_   
 23"var pwdReg = /[a-z0-9]$/;"&vbCrlf&_   
 24"var uidBeginReg = /^[0-9]+[_a-z0-9]/;"&vbCrlf&_   
 25"var phoneReg = /\d{2}-\d{5}/;"&vbCrlf&_   
 26"var phoneDetailReg = /[^0-9\\-]/;"&vbCrlf&_   
 27"var uidReg = /[^a-zA-Z0-9_\\-]/;"&vbCrlf&_   
 28"var htmlReg = /<(.*)>.*<\/\1>/;"&vbCrlf&_   
 29  
 30"var re1 = /^http:\/\/[A-Za-z][A-Za-z0-9\\-]*[A-Za-z]*\\./;"&vbCrlf&_   
 31"var re2 = /^http:\/\/[0-9]{1,5}[A-Za-z]*[0-9]*\\./;"&vbCrlf&_   
 32"var re3 = /\\.{2,}/;"&vbCrlf&_   
 33"var re4 = /\:{2,}/;"&vbCrlf&_   
 34"var re5 = /\/{3,}/;"&vbCrlf&_   
 35"var re6 = /\,+/;"&vbCrlf&_   
 36"var re7 = /\\!+/;"&vbCrlf&_   
 37"var re8 = /\@+/;"&vbCrlf&_   
 38"var re9 = /\\#+/;"&vbCrlf&_   
 39"var re10 = /\$+/;"&vbCrlf&_   
 40"var re11 = /\^+/;"&vbCrlf&_   
 41"var re12 = /\\*+/;"&vbCrlf&_   
 42"var re13 = /\|+/;"&vbCrlf&_   
 43"var re14 = /\\.[a-z0-9_&=?\/]*[A-Za-z0-9\/\~]{2,}$/;"&vbCrlf&_   
 44strShow&_   
 45"else"&vbCrlf&_   
 46"return true;"&vbCrlf&_   
 47"}"&vbCrlf&_   
 48"//-->
 49"&vbCrlf&_   
 50"
 51"   
 52Response.Write strShow   
 53End Sub 
 54
 55Function findJS(frmName,errStr)   
 56Dim tmpArr   
 57Dim i   
 58'参数值   
 59i=0   
 60'获取错误列表,建立数组   
 61tmpArr=Split(errStr,"|")   
 62'输出查询条件   
 63Select Case tmpArr(i+1)   
 64  
 65  
 66Case "0" '必填的Text类型   
 67findJS="if ((document."&frmName&"."&tmpArr(i)&".value)=="""")"&vbCrlf&_   
 68"{"&vbCrlf&_   
 69"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
 70"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
 71"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
 72"return false;"&vbCrlf&_   
 73"}"&vbCrlf 
 74
 75Exit Function 
 76
 77  
 78Case "1" '必填的ListMenu类型   
 79findJS="if ((document."&frmName&"."&tmpArr(i)&".value)=="""")"&vbCrlf&_   
 80"{"&vbCrlf&_   
 81"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
 82"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
 83"return false;"&vbCrlf&_   
 84"}"&vbCrlf   
 85Exit Function   
 86  
 87  
 88Case "2" '必须为数字的Text类型   
 89findJS="if (isNaN(document."&frmName&"."&tmpArr(i)&".value))"&vbCrlf&_   
 90"{"&vbCrlf&_   
 91"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
 92"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
 93"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
 94"return false;"&vbCrlf&_   
 95"}"&vbCrlf   
 96Exit Function 
 97
 98  
 99Case "3" '必须为指定位数的Text类型   
100findJS="if (document."&frmName&"."&tmpArr(i)&".value.length!="&tmpArr(i+3)&")"&vbCrlf&_   
101"{"&vbCrlf&_   
102"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
103"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
104"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
105"return false;"&vbCrlf&_   
106"}"&vbCrlf   
107Exit Function 
108
109  
110Case "4" '必须大于指定位数的Text类型   
111findJS="if (document."&frmName&"."&tmpArr(i)&".value.length<"&tmpArr(i+3)&")"&vbCrlf&_   
112"{"&vbCrlf&_   
113"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
114"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
115"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
116"return false;"&vbCrlf&_   
117"}"&vbCrlf   
118Exit Function 
119
120  
121Case "5" '必须为Email的Text类型   
122findJS="if ((!emailReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_   
123"{"&vbCrlf&_   
124"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
125"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
126"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
127"return false;"&vbCrlf&_   
128"}"&vbCrlf   
129Exit Function 
130
131  
132Case "6" '必须为a-z或0-9的字符的Text类型   
133findJS="if ((!pwdReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_   
134"{"&vbCrlf&_   
135"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
136"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
137"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
138"return false;"&vbCrlf&_   
139"}"&vbCrlf   
140Exit Function 
141
142  
143Case "7" '确认密码和密码必须相等的Text类型   
144findJS="if ((document."&frmName&"."&tmpArr(i)&".value)!=(document."&frmName&"."&tmpArr(i+3)&".value))"&vbCrlf&_   
145"{"&vbCrlf&_   
146"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
147"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
148"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
149"return false;"&vbCrlf&_   
150"}"&vbCrlf   
151Exit Function 
152
153  
154Case "8" '确认以数字开头的Text类型   
155findJS="if ((uidBeginReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_   
156"{"&vbCrlf&_   
157"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
158"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
159"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
160"return false;"&vbCrlf&_   
161"}"&vbCrlf   
162Exit Function 
163
164  
165Case "9" '确认10-101212格式的电话号码   
166findJS="if ((!phoneReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_   
167"{"&vbCrlf&_   
168"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
169"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
170"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
171"return false;"&vbCrlf&_   
172"}"&vbCrlf   
173Exit Function 
174
175  
176Case "10" '确认只包含英文字母及"-","_"在内的Text。(即不包括中文及其他特殊字符)   
177findJS="if ((uidReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_   
178"{"&vbCrlf&_   
179"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
180"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
181"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
182"return false;"&vbCrlf&_   
183"}"&vbCrlf   
184Exit Function 
185
186  
187Case "11" '确认只包含数字及"-"在内的Text类型(电话号码及传真常用)   
188findJS="if ((phoneDetailReg.test(document."&frmName&"."&tmpArr(i)&".value))&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_   
189"{"&vbCrlf&_   
190"window.alert ('"&tmpArr(i+2)&"');"&vbCrlf&_   
191"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
192"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
193"return false;"&vbCrlf&_   
194"}"&vbCrlf   
195Exit Function   
196
197
198Case "12" '确认是否为有效网址!   
199findJS="if (((!re1.test(document."&frmName&"."&tmpArr(i)&".value))&&(!re2.test(document."&frmName&"."&tmpArr(i)&".value))"&_   
200"&&(document."&frmName&"."&tmpArr(i)&".value!=''))||"&_   
201"(re3.test(document."&frmName&"."&tmpArr(i)&".value))||(re4.test(document."&frmName&"."&tmpArr(i)&".value))||"&_   
202"(re5.test(document."&frmName&"."&tmpArr(i)&".value))||(re6.test(document."&frmName&"."&tmpArr(i)&".value))||"&_   
203"(re7.test(document."&frmName&"."&tmpArr(i)&".value))||(re8.test(document."&frmName&"."&tmpArr(i)&".value))||"&_   
204"(re9.test(document."&frmName&"."&tmpArr(i)&".value))||(re10.test(document."&frmName&"."&tmpArr(i)&".value))||"&_   
205"(re11.test(document."&frmName&"."&tmpArr(i)&".value))||(re12.test(document."&frmName&"."&tmpArr(i)&".value))||"&_   
206"(re13.test(document."&frmName&"."&tmpArr(i)&".value))||(!re14.test(document."&frmName&"."&tmpArr(i)&".value))"&_   
207"&&(document."&frmName&"."&tmpArr(i)&".value!=''))"&vbCrlf&_   
208"{"&vbCrlf&_   
209"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_   
210"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
211"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
212"return false;"&vbCrlf&_   
213"}"&vbCrlf   
214  
215Exit Function 
216
217  
218Case "13" '确认不大于固定位数的Text类型   
219findJS="if (document."&frmName&"."&tmpArr(i)&".value.length>"&tmpArr(i+3)&")"&vbCrlf&_   
220"{"&vbCrlf&_   
221"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_   
222"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
223"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
224"return false;"&vbCrlf&_   
225"}"&vbCrlf   
226Exit Function 
227
228  
229Case "14" '确认含有HTML标记的Text类型   
230findJS="if(htmlReg.test(document."&frmName&"."&tmpArr(i)&".value))"&vbCrlf&_   
231"{"&vbCrlf&_   
232"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_   
233"document."&frmName&"."&tmpArr(i)&".select();"&vbCrlf&_   
234"document."&frmName&"."&tmpArr(i)&".focus();"&vbCrlf&_   
235"return false;"&vbCrlf&_   
236"}"&vbCrlf   
237Exit Function   
238  
239  
240Case "15"   
241'==========================================================   
242' 确认未被禁用的Select类型必须选择 格式:state|15|请选择所在省名称!|selCountry|city|请选择城市信息   
243'注:级联菜单第1项当selectedIndex!=0时,第2项第3项被禁用!无须检查其他两项   
244'当级联菜单第1项selectedIndex==0时,第2项的selectedIndex不能为0,第二项的selectedIndex!=0时,第3项的selectedIndex也不能为0   
245'此项用于检查国家/省/市三项级联菜单,当国家不为中国时,省市可不填,为中国时,必须填写省以及相对的市!   
246findJS="if (document."&frmName&"."&tmpArr(i+3)&".selectedIndex ==0)"&vbCrlf&_   
247"{"&vbCrlf&_   
248"if (document."&frmName&"."&tmpArr(i)&".selectedIndex ==0)"&vbCrlf&_   
249"{"&vbCrlf&_   
250"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_   
251"document."&frmName&"."&tmpArr(i)&".focus;"&vbCrlf&_   
252"return false;"&vbCrlf&_   
253"}"&vbCrlf&_   
254"else if (document."&frmName&"."&tmpArr(i)&".selectedIndex != 0)"&vbCrlf&_   
255"{"&vbCrlf&_   
256"if (document."&frmName&"."&tmpArr(i+4)&".selectedIndex == 0)"&vbCrlf&_   
257"{"&vbCrlf&_   
258"window.alert('"&tmpArr(i+5)&"');"&vbCrlf&_   
259"document."&frmName&"."&tmpArr(i+4)&".focus;"&vbCrlf&_   
260"return false;"&vbCrlf&_   
261"}"&vbCrlf&_ 
262
263  
264"}"&vbCrlf&_   
265  
266"}"&vbCrlf   
267Exit Function 
268
269Case "16" '确认未被禁用的Select类型必须选择 格式:检查的表单项|16|提示信息|关联项"注:当关联项为第一项时,确认开始!   
270findJS="if (document."&frmName&"."&tmpArr(i+3)&".selectedIndex != 0)"&vbCrlf&_   
271"{"&vbCrlf&_   
272"if (document."&frmName&"."&tmpArr(i)&".selectedIndex == 0)"&vbCrlf&_   
273"{"&vbCrlf&_   
274"window.alert('"&tmpArr(i+2)&"');"&vbCrlf&_   
275"document."&frmName&"."&tmpArr(i)&".focus;"&vbCrlf&_   
276"return false;"&vbCrlf&_   
277"}"&vbCrlf&_   
278  
279  
280"}"&vbCrlf   
281Exit Function 
282
283  
284End Select   
285End Function   

使用范例:
< %@LANGUAGE="VBSCRIPT " CODEPAGE="936"%>

 1   
 2'==========================================================   
 3' 表单验证实例   
 4'==========================================================   
 5'1、
 6<!--#Include file = "inc/check_formJS.asp"-->
 7;   
 8'2、绘制表单:name="" onsubmit = "Javascript: return checkSubmit()" 注意大小写;   
 9'3、定义Guo_Error //一句只能出现一个“,”如位数和确认密码项须多加“|”指定参数;   
10'4、Call CheckForm_js("formname,Guo_Error)   
11'www.knowsky.com   
12'========================================================== 
13
14Dim Guo_Error   
15Guo_Error ="text|0|文本项必须填写!,"   
16Guo_Error = Guo_Error & "number|0|数字项必须填写且必须为数字!,"   
17Guo_Error = Guo_Error & "number|2|数字项必须为数字!,"   
18Guo_Error = Guo_Error & "digital|3|位数项必须为6位!|6,"   
19Guo_Error = Guo_Error & "moredigital|4|多位项必须大于4位!|4,"   
20Guo_Error = Guo_Error & "email|5|Mail项必须填写Email地址!,"   
21Guo_Error = Guo_Error & "caractor|6|字符项必须为0-9A-Z的字符!,"   
22Guo_Error = Guo_Error & "password2|7|确认密码必须与密码项保持一致!|password1,"   
23Guo_Error = Guo_Error & "listmenu|1|必须选择!"   
24Guo_Error = Guo_Error & "uid|8|用户名不能以数字开头!,"   
25  
26Call CheckForm_js("form1",Guo_Error)   
27'==========================================================   
28' 表单验证流程   
29'==========================================================   
30'1、通过split(Guo_Error,".")拆分至数组tempArr();   
31'2、通过split(tempArr,"|")拆分至数组tempArr();   
32'3、Select Case split(tempArr(i+1),"|")执行验证及输出错误提示信息split(tempArr(i+2));   
33'4、Javascript区分大小写地,所以各表单组件的名称都要注意书写一致哟~~   
34'==========================================================   
 1<html>
 2<head>
 3<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 4<title>表单验证实例</title>
 5</head>
 6<body>
 7<form action="check_form.asp" method="post" name="form1" onsubmit="JavaScript:return checkSubmit()">
 8<p align="center">待验证的表单</p>   
 9***************************************************************   
10以上代码绝大多数已通过测试,不过是不是都好用,我只能说那个级联的下拉菜单用起来不太好用。看看哪位仁兄或WEB美眉加以改进喽。呵呵</form></body></html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus