博学深思慎言笃行
画一年的画,只用一天就能卖出去,画一天的画,要卖一年。
posts - 166, comments - 292, trackbacks - 0, articles - 1
BlogJava
首页
新随笔
联系
管理
聚合
<
2008年5月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
MAIL: junglesong@gmail.com
MSN: junglesong_5@hotmail.com
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(2)
给我留言
查看公开留言
查看私人留言
随笔分类
(184)
Ajax(3)
CSS(9)
Hibernate
Java API(4)
JavaScript(5)
Java基础(39)
JMS(3)
Maven2之旅(13)
My way(2)
Object Orient Programming(29)
Spring(1)
Swing(9)
Web开发(12)
个人作品(13)
算法数据结构(24)
线程Thread(3)
随想录(15)
随笔档案
(120)
2008年7月 (8)
2008年6月 (8)
2008年5月 (7)
2008年4月 (11)
2008年3月 (6)
2008年2月 (2)
2008年1月 (5)
2007年12月 (8)
2007年11月 (3)
2007年10月 (4)
2007年9月 (8)
2007年8月 (8)
2007年7月 (7)
2007年6月 (34)
2007年4月 (1)
个人软件下载
Sql Anywhere在华军的下载页面
SqlToolBox
文件批量命名器在华军的下载页面
目录文件比较器在华军的下载页面
我的其它博客
我的邻居们
BeanSoft's Java Blog
BlueDavy之技术Blog
CowNew开源团队
DANCE WITH JAVA
David.Turing's blog
fastpace
itkui的博客
Java快速开发平台
Java杂家
Jiangshachina
liaojiyong
Long的博客
Max On Java
mycsdnc#blog
Steady's Java Zone
sterning
zkjbeyond
信拈妙偶
千里冰封
和风细雨
山风小子的博客
庄周梦蝶
朱远翔 的博客
每日一得
点燃闪电
生活源于奋斗不息
翠湖月色
蛟龍居
谈笑有鸿儒,往来无白丁
铁手剑谱
鱼上游
最新随笔
1. 蔓延法判断两个城市的连接状态
2. 用正则表达式找出每个属性对应的值
3. 动态生成日历
4. 日期时间处理实用类
5. java.util.Comparator使用示例
6. 八皇后回溯版
7. 轮圈数数退出问题
8. 泛型单链表类
9. 泛型动态数组类
10. 整形数组实用类,用于求两数组的并交差集
搜索
积分与排名
积分 - 112881
排名 - 66
最新评论
1. re: JTable常见用法细则
谢谢
--膘致男子
2. re: 日期时间处理实用类[未登录]
取得oracle中俩个时间的差的sql语句是什么 具体到时间
--solo
3. re: SqlToolBox 1.60发布,介绍及使用
@如坐春风
好啊,到时别忘了通知我一声。我要好好学JAVA,呵呵
--creasure
4. re: SqlToolBox 1.60发布,介绍及使用
@creasure
是好主意,只是现在时机不太成熟,修改后的版本我会考虑open一下的。
--如坐春风
5. re: SqlToolBox 1.60发布,介绍及使用
@如坐春风
建议你做个opensourc,因为这样就会有更多的朋友来一起做这个,让他更加完善!呵呵
--creasure
阅读排行榜
1. Dom4j下载及使用Dom4j读写XML简介(2867)
2. 程序员四大职业发展方向之比较(2527)
3. 三种权限设计方案的归纳和比较(2165)
4. JTable常见用法细则(2074)
5. 使用commons-fileupload实现单个和多个文件上传(2053)
评论排行榜
1. 程序员四大职业发展方向之比较(13)
2. 程序员之路探究(13)
3. 自己编写的数据库帮助工具(Sql Anythere v1.00) 介绍(12)
4. SqlToolBox 1.60发布,介绍及使用(11)
5. 设计构建一个软件程序的基本步骤探讨(10)
60天内阅读排行
1. 程序员之路探究(1855)
2. 动态代理机制初探(1464)
3. 求两字符串的公共子串(192)
4. 判断数组元素是否存在重复,要求时间复杂度为O(1)(178)
5. 蔓延法判断两个城市的连接状态(157)
表单验证方式的通用化
Posted on 2008-04-07 21:41
博学精思慎言笃行
阅读(1030)
评论(7)
编辑
收藏
所属分类:
Web开发
、
JavaScript
此文是“
Web页面表单域验证方式的改进
”的续篇。
在上一篇“Web页面表单域验证方式的改进“中,我们通过把验证法则(正则表达式和是否必填字段)写在表单域中,将验证过程和验证法则成功的分离,从而减少了重复代码,使验证变得简单易行,在实际使用中,我们可以把验证过程放在一个JS文件中,对于全输入验证界面,在页面的表单验证部分只需要调用其中的checkForm函数就能进行有效验证,页面上不再需要书写重复性高的JS验证代码;对于复杂的表单,比如其中含有复选框或是需要两个文本框比较的地方,这种方法也可让你不写通用验证代码而把主要精力放在特殊的验证上。这些能减轻不少工作量,让繁琐的工作变得轻松愉快起来。
下面我们看一看这种用法的实际使用。
首先,我们可以把验证过程放在一个JS文件中,具体代码如下:
formchecker.js
/**/
/*
*
* Check form
*/
function
checkForm(vform)
{
for
(
var
i
=
0
;i
<
vform.elements.length;i
++
)
{
if
(vform.elements[i].type
==
"
text
"
)
{
var
checkResult
=
checkTextBox(vform.elements[i]);
var
name
=
vform.elements[i].getAttribute(
"
name
"
);
if
(checkResult)
{
document.getElementById(name
+
"
Msg
"
).className
=
"
feedbackHide
"
;
}
else
{
document.getElementById(name
+
"
Msg
"
).className
=
"
feedbackShow
"
;
vform.elements[i].focus();
return
false
;
}
}
}
return
true
;
}
/**/
/*
*
* Check text field in the form
*/
function
checkTextBox(vTextBox)
{
var
validChar
=
vTextBox.getAttribute(
"
validChar
"
);
var
isRequired
=
vTextBox.getAttribute(
"
isRequired
"
);
var
inputValue
=
vTextBox.value;
if
(isRequired
!=
"
true
"
&&
inputValue.length
<
1
)
{
return
true
;
}
else
{
var
regexStr
=
"
^
"
+
validChar
+
"
$
"
;
var
regex
=
new
RegExp(regexStr);
return
regex.test(inputValue);
}
}
/**/
/*
*
* Remove Extra Char in textbox
*/
function
removeExtraChar(vTextBox,event)
{
var
maxlength
=
parseInt(vTextBox.getAttribute(
"
maxlength
"
));
var
inputValue
=
vTextBox.value;
if
(inputValue.length
>=
maxlength)
{
event.keyCode
=
9
;
}
}
下面想验证一个用户登录页面,它的页面部分代码如下:
<%
@ page contentType
=
"
text/html; charset=UTF-8
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
html
>
<
head
>
<
title
>
"我的事务备忘录"用户登录页面
</
title
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=UTF-8"
>
<
script
src
="web/js/ajax.js"
type
="text/javascript"
></
script
>
<
script
src
="web/js/formchecker.js"
type
="text/javascript"
></
script
>
<
link
rel
="stylesheet"
rev
="stylesheet"
href
="web/css/style.css"
type
="text/css"
/>
</
head
>
<
body
>
<
div
id
="branding"
>
<%
String
msg
=
(
String
) request.getAttribute(
"
Msg
"
);
if
(msg !
=
null
) {
out.print(msg);
}
%>
<!--
外层表格,比内表格宽,以在左右留出一定空当
-->
<
table
style
="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; border-bottom: 1px solid; boder_top: 0px"
bordercolor
="#236d78"
cellspacing
="0"
cellpadding
="0"
width
="530"
align
="center"
border
="0"
>
<
tr
bgcolor
="#eaecf5"
height
="25"
>
<
td
colspan
=3
>
<
font
face
=webdings
color
=#ff8c00
>
8
</
font
><
b
>
用户登录:
</
b
></
td
>
</
tr
>
<
tr
bgcolor
=#236d78
height
=1
><
td
></
td
></
tr
>
<
tr
bgcolor
=#eaecf5
>
<
td
bgcolor
=#ffffff
>
<!--
内置表格,居中,比外表格窄,
-->
<
form
action
="UserLogin"
onsubmit
="return checkForm(this);"
>
<
table
width
=460
align
=center
border
=0
>
<
tbody
>
<
tr
>
<
td
width
=70
>
用户名:
</
td
>
<
td
>
<
input
type
="text"
name
="userName"
validChar
="[\u4E00-\u9FA5]{2,3}"
isRequired
="true"
maxlength
="8"
onkeydown
="removeExtraChar(this,event)"
onfocus
="this.style.backgroundColor='#e6e6e6'"
onblur
="this.style.backgroundColor='#ffffff'"
/>
<
font
color
=red
>
(必填)
</
font
>
<
span
id
="userNameMsg"
class
="feedbackHide"
>
用户名必须输入两到三位汉字
</
span
>
</
td
>
</
tr
>
<
tr
>
<
td
width
=70
>
密码:
</
td
>
<
td
>
<
input
type
="text"
name
="userPswd"
validChar
="\w+"
isRequired
="true"
maxlength
="8"
onkeydown
="removeExtraChar(this,event)"
onfocus
="this.style.backgroundColor='#e6e6e6'"
onblur
="this.style.backgroundColor='#ffffff'"
/>
<
font
color
=red
>
(必填)
</
font
>
<
span
id
="userPswdMsg"
class
="feedbackHide"
>
密码必须输入英语或数字
</
span
>
</
td
>
</
tr
>
<
tr
>
<
td
></
td
>
<
td
><
input
type
="submit"
value
="登录"
/></
td
>
</
tr
>
<
tr
>
<
td
colspan
="2"
align
="center"
>
如果没有用户请点击这里
<
a
href
='ShowPage?page=register'
>
注册
</
a
></
td
>
</
tr
>
</
tbody
>
</
table
>
</
form
>
<!--
内置表格结束
-->
</
td
>
</
tr
>
</
table
>
<!--
外层表格结束
-->
<
div
>
</
body
>
</
html
>
请注意其中没有任何页面验证的JS代码,只有在表单验证的地方调用formchecker.js中的函数。
<
form
action
="UserLogin"
onsubmit
="return checkForm(this);"
>
不需要写JS代码,只需要引入formchecker.js,就实现了表单的验证,下面是验证效果之一:
对于复杂一些的页面,在formchecker.js的帮助下也能有效减少验证代码量,你只要书写一些特殊的通过validchar不能验证的代码即可,比如说如下注册页面:
你只要书写两次密码比较的JS代码,其它的还是可以让checkForm函数帮你完成。具体代码如下:
<%
@ page contentType
=
"
text/html; charset=UTF-8
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
html
>
<
head
>
<
title
>
"我的事务备忘录"用户注册页面
</
title
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=UTF-8"
>
<
script
src
="web/js/ajax.js"
type
="text/javascript"
></
script
>
<
script
src
="web/js/formchecker.js"
type
="text/javascript"
></
script
>
<
link
rel
="stylesheet"
rev
="stylesheet"
href
="web/css/style.css"
type
="text/css"
/>
</
head
>
<
body
>
<
div
>
<%
String
msg
=
(
String
) request.getAttribute(
"
Msg
"
);
if
(msg !
=
null
) {
out.print(msg);
}
%>
<!--
外层表格,比内表格宽,以在左右留出一定空当
-->
<
table
style
="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; border-bottom: 1px solid; boder_top: 0px"
bordercolor
="#236d78"
cellspacing
="0"
cellpadding
="0"
width
="530"
align
="center"
border
="0"
>
<
tr
bgcolor
="#eaecf5"
height
="25"
>
<
td
colspan
=3
>
<
font
face
=webdings
color
=#ff8c00
>
8
</
font
><
b
>
注册个人用户
</
b
></
td
>
</
tr
>
<
tr
bgcolor
=#236d78
height
=1
><
td
></
td
></
tr
>
<
tr
bgcolor
=#eaecf5
>
<
td
bgcolor
=#ffffff
>
<!--
内置表格,居中,比外表格窄,
-->
<
form
action
="UserRegister"
onsubmit
="return check(this);"
>
<
table
width
=460
align
=center
border
=0
>
<
tbody
>
<
tr
>
<
td
width
=70
>
用户名:
</
td
>
<
td
>
<
input
type
="text"
name
="userName"
validChar
="[\u4E00-\u9FA5]{2,3}"
isRequired
="true"
onfocus
="this.style.backgroundColor='#e6e6e6'"
onblur
="this.style.backgroundColor='#ffffff'"
/>
<
font
color
=red
>
(必填)
</
font
>
<
span
id
="userNameMsg"
class
="feedbackHide"
>
用户名必须输入两到三位汉字
</
span
>
</
td
>
</
tr
>
<
tr
>
<
td
width
=70
>
密码:
</
td
>
<
td
>
<
input
type
="text"
name
="userPswd"
validChar
="\w+"
isRequired
="true"
onfocus
="this.style.backgroundColor='#e6e6e6'"
onblur
="this.style.backgroundColor='#ffffff'"
/>
<
font
color
=red
>
(必填)
</
font
>
<
span
id
="userPswdMsg"
class
="feedbackHide"
>
密码必须输入英语或数字
</
span
>
</
td
>
</
tr
>
<
tr
>
<
td
width
=70
>
再次输入密码:
</
td
>
<
td
>
<
input
type
="text"
name
="userPswd2"
validChar
="\w+"
isRequired
="true"
onfocus
="this.style.backgroundColor='#e6e6e6'"
onblur
="this.style.backgroundColor='#ffffff'"
/>
<
font
color
=red
>
(必填)
</
font
>
<
span
id
="userPswd2Msg"
class
="feedbackHide"
>
密码必须输入英语或数字
</
span
>
</
td
>
</
tr
>
<
tr
>
<
td
></
td
>
<
td
><
input
type
="submit"
value
="注册"
/></
td
>
</
tr
>
</
tbody
>
</
table
>
</
form
>
<!--
内置表格结束
-->
</
td
>
</
tr
>
</
table
>
<!--
外层表格结束
-->
<
div
>
</
body
>
</
html
>
<
script
LANGUAGE
="JavaScript"
>
<!--
function
check(vform){
//
先进行文本框基本验证
if
(
checkForm(vform)
==
false
){
return
false
;
}
//