以网上down了一个asp动态生成form验证javascript的代码,自己扩容了一下。作者的大名我给忘了,不过我记得清楚,从第一条至第九条的代码都是该作者的原创。至于九至第十五条却是本人自己加的哦。而且由于本人水平有限,从第九条至第十五条不见得会条条生效啊?呵呵,其实把它贴在这里没有什么必要的,因为这点小会伎俩谁都会用的,我把它的作用定位为:开发时找不到该函数时到这里拷贝一下就可以了。呵,因为我即没有刻录机也没有移动硬盘。实在是没有办法把这些代码随身携带,不过还好,在北京到处都可以上网。所以就把这个放在这里,用的时候ctrl+c,再ctrl+v好了。
<%
'****************************************************************
'函数名称:checkform_js(frmname,errstr)
'功能:用asp的方法动态写出javascript的表单验证的函数checksubmit()
'使用方法:1、<!--include file=url+本函数所在的页>;
' 2、<form onsubmit="javascript:return checksubmit()">;
'原作者已被忘却,二次开发作者:guo.q.m
'最后更新:2004
'****************************************************************
'帮助:
'---------------------------------------------------------------
'·参数说明:
'frmname:表单域的名称
'errstr:验证列表,如:"num3型号必须不小于8位8,email5请输入正确的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类型必须选择 格式:state15请选择所在省名称!selcountrycity请选择城市信息
'注:级联菜单第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)
'www.china-u.com
'==========================================================
dim guo_error
guo_error ="text0文本项必须填写!,"
guo_error = guo_error & "number0数字项必须填写且必须为数字!,"
guo_error = guo_error & "number2数字项必须为数字!,"
guo_error = guo_error & "digital3位数项必须为6位!6,"
guo_error = guo_error & "moredigital4多位项必须大于4位!4,"
guo_error = guo_error & "email5mail项必须填写email地址!,"
guo_error = guo_error & "caractor6字符项必须为0-9a-z的字符!,"
guo_error = guo_error & "password27确认密码必须与密码项保持一致!password1,"
guo_error = guo_error & "listmenu1必须选择!"
guo_error = guo_error & "uid8用户名不能以数字开头!,"
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>
***************************************************************
以上代码绝大多数已通过测试,不过是不是都好用,我只能说那个级联的下拉菜单用起来不太好用。看看哪位仁兄或web美眉加以改进喽。呵呵
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器