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

2018-09-06 12:11

阅读:429

  以网上down了一个ASP动态生成Form验证javascript的代码,自己扩容了一下。作者的大名我给忘了,不过我记得清楚,从第一条至第九条的代码都是该作者的原创。至于九至第十五条却是本人自己加的哦。而且由于本人水平有限,从第九条至第十五条不见得会条条生效啊?呵呵,其实把它贴在这里没有什么必要的,因为这点小会伎俩谁都会用的,我把它的作用定位为:开发时找不到该函数时到这里拷贝一下就可以了。呵,因为我即没有刻录机也没有移动硬盘。实在是没有办法把这些代码随身携带,不过还好,在北京到处都可以上网。所以就把这个放在这里,用的时候Ctrl+C,再Ctrl+V好了。
复制代码 代码如下:<%
****************************************************************
函数名称:CheckForm_JS(frmName,errStr)
功能:用ASP的方法动态写出javascript的表单验证的函数checkSubmit()
使用方法:1、<!--IncludeFile=URL+本函数所在的页>;
2、<formonsubmit=javascript:returncheckSubmit()>;
原作者已被忘却,二次开发作者: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-9a-zA-Z
11:必须只包含数字及-在内的Text类型
12:必须为正确网址的Text类型
13:必须小于指定位数的Text类型
14:不得包含HTML标记的Text类型
15:确认未被禁用的Select类型必须选择格式:检查的表单项15提示信息关联项
注意:如有级联菜单,请将级联菜单的验证过程放到最后检验!!!!
------------------------------------------------------------------------
%>
<%
SubCheckForm_JS(frmName,errStr)
DimtmpArr
Dimi
DimstrShow输出JS的字符串
获取错误列表,建立数组
tmpArr=Split(errStr,,)
写JS
fori=0toUBound(tmpArr)
ifi<>0then
strShow=strShow&else&findJS(frmName,tmpArr(i))
else
strShow=strShow&findJS(frmName,tmpArr(i))
endif
next
输出
strShow=<scriptlanguage=javascript>&vbCrlf&_
<!--&vbCrlf&_
//PowerbyGuoquanman2004&vbCrlf&_
functioncheckSubmit()&vbCrlf&_
{&vbCrlf&_
varemailReg=/^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;&vbCrlf&_
varpwdReg=/[a-z0-9]$/;&vbCrlf&_
varuidBeginReg=/^[0-9]+[_a-z0-9]/;&vbCrlf&_
varphoneReg=/\d{2}-\d{5}/;&vbCrlf&_
varphoneDetailReg=/[^0-9\-]/;&vbCrlf&_
varuidReg=/[^a-zA-Z0-9_\-]/;&vbCrlf&_
varhtmlReg=/<(.*)>.*<\/\1>/;&vbCrlf&_
varre1=/^http:\/\/[A-Za-z][A-Za-z0-9\-]*[A-Za-z]*\./;&vbCrlf&_
varre2=/^http:\/\/[0-9]{1,5}[A-Za-z]*[0-9]*\./;&vbCrlf&_
varre3=/\.{2,}/;&vbCrlf&_
varre4=/\:{2,}/;&vbCrlf&_
varre5=/\/{3,}/;&vbCrlf&_
varre6=/\,+/;&vbCrlf&_
varre7=/\!+/;&vbCrlf&_
varre8=/\@+/;&vbCrlf&_
varre9=/\#+/;&vbCrlf&_
varre10=/\$+/;&vbCrlf&_
varre11=/\^+/;&vbCrlf&_
varre12=/\*+/;&vbCrlf&_
varre13=/\+/;&vbCrlf&_
varre14=/\.[a-z0-9_&=?\/]*[A-Za-z0-9\/\~]{2,}$/;&vbCrlf&_
strShow&_
else&vbCrlf&_
returntrue;&vbCrlf&_
}&vbCrlf&_
//-->&vbCrlf&_
</script>
Response.WritestrShow
EndSub
FunctionfindJS(frmName,errStr)
DimtmpArr
Dimi
参数值
i=0
获取错误列表,建立数组
tmpArr=Split(errStr,)
输出查询条件
SelectCasetmpArr(i+1)
Case0必填的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case1必填的ListMenu类型
findJS=if((document.&frmName&.&tmpArr(i)&.value)==)&vbCrlf&_
{&vbCrlf&_
window.alert(&tmpArr(i+2)&);&vbCrlf&_
document.&frmName&.&tmpArr(i)&.focus();&vbCrlf&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case2必须为数字的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case3必须为指定位数的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case4必须大于指定位数的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case5必须为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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case6必须为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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case7确认密码和密码必须相等的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case8确认以数字开头的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case9确认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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case10确认只包含英文字母及-,_在内的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case11确认只包含数字及-在内的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case12确认是否为有效网址!
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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case13确认不大于固定位数的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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case14确认含有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&_
returnfalse;&vbCrlf&_
}&vbCrlf
ExitFunction
Case15
==========================================================
确认未被禁用的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&_
returnfalse;&vbCrlf&_
}&vbCrlf&_
elseif(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&_
returnfalse;&vbCrlf&_
}&vbCrlf&_
}&vbCrlf&_
}&vbCrlf
ExitFunction
Case16确认未被禁用的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&_
returnfalse;&vbCrlf&_
}&vbCrlf&_
}&vbCrlf
ExitFunction
EndSelect
EndFunction
%>
***********************************************************************
使用范例:
复制代码 代码如下:<%@LANGUAGE=VBSCRIPTCODEPAGE=936%>
<!--#Includefile=inc/check_formJS.asp-->
<!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//EN
<%
==========================================================
表单验证实例
==========================================================
1、<!--#Includefile=inc/check_formJS.asp-->;
2、绘制表单:name=onsubmit=javascript:returncheckSubmit()注意大小写;
3、定义Guo_Error//一句只能出现一个“,”如位数和确认密码项须多加“”指定参数;
4、CallCheckForm_js(formname,Guo_Error)

==========================================================
DimGuo_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用户名不能以数字开头!,
CallCheckForm_js(form1,Guo_Error)
==========================================================
表单验证流程
==========================================================
1、通过split(Guo_Error,.)拆分至数组tempArr();
2、通过split(tempArr,)拆分至数组tempArr();
3、SelectCasesplit(tempArr(i+1),)执行验证及输出错误提示信息split(tempArr(i+2));
4、javascript区分大小写地,所以各表单组件的名称都要注意书写一致哟~~
==========================================================
%>
<html>
<head>
<metahttp-equiv=Content-Typecontent=text/html;charset=gb2312>
<title>表单验证实例</title>
</head>
<body>
<formname=form1method=postaction=check_form.asponsubmit=javascript:returncheckSubmit()>
<palign=center>待验证的表单</p>
***************************************************************
以上代码绝大多数已通过测试,不过是不是都好用,我只能说那个级联的下拉菜单用起来不太好用。看看哪位仁兄或WEB美眉加以改进喽。呵呵


评论


亲,登录后才可以留言!