您的位置:首页 > Web前端 > JavaScript

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

2008-01-05 18:56 996 查看
表单的验证是开发WEB应用程序中常遇到的一关。有时候我们必须保证表单的某些项必须填写、必须为数字、必须是指定的位数等等,这时候就要用到表单验证了,一般我们常用的表单验证有2种方式:

1、编写JavaScript或VBScript的表单验证函数,在客户端进行验证;
2、在表单提交后,使用ASP的方法Request.Form获取表单的输入值进行判断,然后返回结果,这是在服务端进行验证;

  这2种方式都有其优缺点,比如第1种方式速度比较快,而且通常使用警告框的方式,用户能够很快的根据提示完成表单的填写,但是缺点就是用户的浏览器必须是支持JavaScript脚本的,再不然如果他关闭了JavaScript,那就!@#$%&^*(小田已经倒在地上了^_^);而第2种方式的兼容性比较好,但是缺点是速度比较慢(提交到服务端,在返回)而且使用也不方便。这次主要是用JavaScript的方法来验证,当然,如果同时用2种方式来验证是最保险了,不过(汗…………)要累死我们这些程序员了:)

用ASP动态生成JavaScript的表单验证代码
<%
'其中参数frmName是指表单域的名称,而errStr是表单项+判断类型+出错提示的一个数组,其语法为:"表单项名称1|判断类型1|出错提示1|[可选参数1],表单项名称2|判断类型2|出错提示2|[可选参数2],..."
'表单项名称:例如name等,是自定义的
'判断类型:就是Case语句里的0,1,2,3等等
'出错提示:例如 姓名必须填写 等,是自定义的
'可选参数:比如在判断确认密码和密码必须相等的Text类型时,可选参数就是想要判断相当的密码表单项名称;在必须为指定位数的Text类型时,可选参数就是指定的位数。当然可以选参数可以是好几个,具体就看你Case语句里的if是怎么编的了。

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

End Select
End Function

'函数CheckForm_JS(frmName,errStr)。这个函数的作用是最后将一个个JavaScript的if判断整合起来
Sub CheckForm_JS(frmName,errStr)
Dim tmpArrDim i
Dim strShow
'输出JS的字符串'获取错误列表,建立数组
tmpArr = Split(errStr,",")
if i <> 0 then
strShow = strShow&"else "&findJS(frmName,tmpArr(i))
else
strShow=strShow&findJS(frmName,tmpArr(i))
end if
End Sub

%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: