表单的验证一直是网页设计者头痛的问题,表单验证类 Validator就是为解决这个问题而写的,旨在使设计者从纷繁复杂的表单验证中解放出来,把精力集中于网页的设计和功能上的改进上。
Validator是基于JavaScript技术的伪静态类和对象的自定义属性,可以对网页中的表单项输入进行相应的验证,允许同一页面中同时验证多个表单,熟悉接口之后也可以对特定的表单项甚至仅仅是某个字符串进行验证。因为是伪静态类,所以在调用时不需要实例化,直接以"类名+.语法+属性或方法名"来调用。此外,Validator还提供3种不同的错误提示模式,以满足不同的需要。
Validator目前可实现的验证类型有:
[JavaScript] 版
Validator目前可实现的验证类型有:
1.是否为空;
2.中文字符;
3.双字节字符
4.英文;
5.数字;
6.整数;
7.实数;
8.Email地址;
9.使用HTTP协议的网址;
10.电话号码;
11.货币;
12.手机号码;
13.邮政编码;
14.身份证号码(1.05增强);
15.QQ号码;
16.日期;
17.符合安全规则的密码;
18.某项的重复值;
19.两数的关系比较;
20.判断输入值是否在(n, m)区间;
21.输入字符长度限制(可按字节比较);
22.对于具有相同名称的单选按钮的选中判断;
23.限制具有相同名称的多选按钮的选中数目;
24.自定义的正则表达式验证;
25.文件上传格式过滤(1.04)
运行环境(客户端):
在Windows Server 2003下用IE6.0+SP1和Mozilla Firefox 1.0测试通过;
在Lunix RedHat 9下的Netscape测试通过;
对于客户端的表单验证,这个基于JavaScript编写的Validator基本上都可以满足,具体可以下载CHM文件:Validator.CHM下载
示例:
代码如下:
[Ctrl+A 全选 提示:你可先修改部分代码,再点运行代码]
更新历史:
1.01
修正对12月份的日期验证(感谢flylg999)
1.03
修正Range验证类型时将数字当字符串比较的bug(感谢cncom和xtlhnhbb)
修正日期验证(感谢Papsam)
增加Username验证类型
增加对Phone验证类型时支持分机号
1.04
增加文件格式的过滤,用于上传时限制上传的文件格式
1.05
增强对身份证号码的验证
[ASP]版代码拷贝框
1
<
%
2
ClassValidator
3
'
*************************************************
4
'
ValidatorforASPbeta2服务器端脚本
5
'
codeby我佛山人
6
'
[email protected]
7
'
http://www.cunite.com
8
'
*************************************************
9
Private
Re,Dic
10
Private
Separator
11
Private
ErrorItem,ErrorMessage,ErrorMode,ErrorNo
12
Private
FormName,FormIndex,FilePath,GetMethod
13
14
Private
Sub
Class_Initialize()
15
Set
Re
=
New
RegExp
16
Re.IgnoreCase
=
True
17
Re.Global
=
True
18
Set
Dic
=
CreateObject
(
"
Scripting.Dictionary
"
)
19
Separator
=
"
,"
20
ErrorItem
=
"
"
21
ErrorMessage
=
"
"
22
ErrorMode
=
5
23
ErrorNo
=
1
24
FilePath
=
Server.MapPath(Request.ServerVariables(
"
Script_Name
"
))
25
GetMethod
=
"
FSO"
26
EndSub
27
28
Private
Sub
Class_Terminate()
29
Set
Re
=
Nothing
30
Dic.RemoveAll()
31
Set
Dic
=
Nothing
32
EndSub
33
34
Public
Sub
Validate()
35
IF
Request(
"
Submit
"
)
=
""
Then
Exit
Sub
36
IF
Not
IsValidPost()
Then
Exit
Sub
37
38
With
Dic
39
.Add
"
Compare
"
,
"
Compare(PostValue,operator,toObj)"
40
.Add
"
Custom
"
,
"
Custom(PostValue,regexp)"
41
.Add
"
Date
"
,
"
IsDateFormat(PostValue,format)"
42
.Add
"
Limit
"
,
"
Limit(PostValue,min,max)"
43
.Add
"
LimitB
"
,
"
LimitB(PostValue,min,max)"
44
.Add
"
Range
"
,
"
Range(PostValue,min,max)"
45
.Add
"
Repeat
"
,
"
IsEqual(PostValue,Request(toObj))"
46
.Add
"
Group
"
,
"
Group(PostValue,min,max)"
47
48
.Add
"
NotEqual
"
,
"
Op1<>Op2"
49
.Add
"
GreaterThan
"
,
"
Op1>Op2"
50
.Add
"
GreaterThanEqual
"
,
"
Op1>=Op2"
51
.Add
"
LessThan
"
,
"
Op1<Op2"
52
.Add
"
LessThanEqual
"
,
"
Op1<=Op2"
53
.Add
"
Equal
"
,
"
Op1=Op2"
54
End
With
55
56
Call
MatchCode()
57
58
IF
ErrorMessage
<>
""
Then
DisplayError
59
EndSub
60
61
Private
Sub
MatchCode()
62
Dim
bI,bG,bM
63
Dim
Str
64
65
Select
Case
GetMethod
66
Case
"
FSO
"
:
67
Dim
FSO:
Set
FSO
=
Server.
CreateObject
(
"
Scripting.FileSystemObject
"
)
68
Set
TS
=
FSO.OpenTextFile(FilePath,
1
,
false
)
69
Str
=
TS.ReadAll()
70
TS.Close
71
Set
TS
=
Nothing
72
Set
FSO
=
Nothing
73
Case
"
XMLHTTP
"
:
74
Dim
XHttp:
Set
XHttp
=
Server.
CreateObject
(
"
MSXML2.XMLHTTP
"
)
75
With
XHttp
76
Call
.Open(
"
Get
"
,
"
http://
"
&
Request.ServerVariables(
"
Server_Name
"
)
&
Request.ServerVariables(
"
Script_Name
"
),
False
)
77
Call
.Send()
78
Str
=
B2S(.responseBody)
79
End
With
80
Set
XHttp
=
Nothing
81
End
Select
82
Dim
itemString
83
With
Re
84
bI
=
.IgnoreCase
85
bG
=
.Global
86
bM
=
.MultiLine
87
.IgnoreCase
=
True
88
.Global
=
True
89
.Pattern
=
"
[\s\S]*<form[^>]+>([\s\S]+)<\/form>[\s\S]*"
90
Str
=
.
Replace
(Str,
"
$1
"
)
91
92
.Global
=
True
93
.MultiLine
=
True
94
.Pattern
=
"
<\/?(?!input|textarea|select)[^>]*>"
95
Str
=
.
Replace
(Str,
""
)
96
97
.Pattern
=
"
^.*(<(?=input|textarea|select)[^>]*>).*$"
98
Str
=
.
Replace
(Str,
"
$1
"
)
99
100
.Pattern
=
"
([\r\n]+|^\s*)(?=<)"
101
Str
=
.
Replace
(Str,
""
)
102
While
Test(
"
dataType=([""\'])([^""\'>]+)\1
"
,Str)
103
.MultiLine
=
False
104
.Pattern
=
"
^([^\n]+)\n([\s\S]*)$"
105
itemString
=
.
Replace
(Str,
"
$1
"
)
106
Str
=
.
Replace
(Str,
"
$2
"
)
107
.Pattern
=
"
(name|dataType|to1|min|max|msg|require|regexp|format)=([""\'])([^""\'>]+)\2"
108
109
Dim
Matches:
Set
Matches
=
.
Execute
(itemString)
110
Dim
Match,RetStr:RetStr
=
"
"
111
For
Each
MatchinMatches
112
RetStr
=
RetStr
&
Match.Value
&
"
:"
113
Next
114
Call
IsValid(
Replace
(
Replace
(
Replace
(RetStr,
"
:$
"
,
""
),
"
to=
"
,
"
toObj=
"
),
"
""Require""
"
,
"
""NotEmpty""
"
))
115
Wend
116
.IgnoreCase
=
bI
117
.Global
=
bG
118
.MultiLine
=
bM
119
120
End
With
121
EndSub
122
123
Private
Sub
IsValid(ByValStr)
124
Dim
name,msg,dataType,toObj,min,max,require,regexp,format
125
min
=
1
:max
=
100
:require
=
"
true
"
:format
=
"
YMD"
126
Execute
Str
127
Dim
PostValue:PostValue
=
Request(name)
128
Dim
Fun
129
130
IF
require
=
"
false
"
AND
PostValue
=
""
Then
Exit
Sub
131
132
IF
Dic.Exists(dataType)
Then
133
分享到:
评论