-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample.html
More file actions
178 lines (174 loc) · 5.21 KB
/
sample.html
File metadata and controls
178 lines (174 loc) · 5.21 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validator Test</title>
<script src="validator.js"></script>
<script src="form_validator_non_jquery.js"></script>
<script>
var v = FormValidator;
v.submit_callback = function(error_items) {
var str = "";
for (var i in error_items) {
str += error_items[i].meta.display + "\n";
for (var j in error_items[i].error_messages) {
str += "- " + error_items[i].error_messages[j] + "\n";
}
}
alert(str);
};
// when validate item has no callback, use this.
v.default_callback = function(item, value) {
var tar = document.getElementById(item.identity + '_status');
if (!item.is_correct) {
str = "";
for (var i in item.error_messages) {
str += item.error_messages[i] + ' ';
}
tar.innerHTML = str;
} else {
tar.innerHTML = 'valid';
}
};
// you can add your own rule.
v.add_rule('date', {
shoulda : function(value, pattern) {
var regex = /^\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;
return regex.test(value);
},
message : "{display}不是有效的的日期格式。"
});
// source can a dom id or a function with a return value.
v.ensure({
source : function() {
return document.getElementById('item2').value + document.getElementById('item3').value;
},
identity : 'test',
display : '字段2和3'
}, {
validates : ['presence', 'size'],
size : {
minimium : 8,
maximium : 10
}
});
v.ensure({
source : 'item1',
display : '字段1',
condition : function() {
return document.getElementById('item2').value !== "xxxxx";
}
}, {
validates : ['presence']
});
v.ensure({
source : 'item2',
display : '字段2'
}, {
validates : ['size'],
size : {
minimium : 4
}
});
v.ensure({
source : 'item3',
display : '字段3'
}, {
validates : ['size'],
size : {
maximium : 6
}
});
v.ensure({
source : 'item4',
display : '字段4'
}, {
validates : ['size'],
size : {
minimium : 4,
maximium : 6
}
});
v.ensure({
source : 'item5',
display : '字段5'
}, {
validates : ['format'],
format : /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/,
message : '{display}不是有效的email地址。'
});
v.ensure({
source : 'item6',
display : '字段6'
}, {
validates : ['shoulda'],
shoulda : function(value, pattern) {
return value == 'hello'
},
message : '{display}不是hello。'
});
v.ensure({
source : 'item7',
display : '字段7'
}, {
validates : ['inclusion'],
inclusion : ['male', 'female']
});
v.ensure({
source : 'item8',
display : '字段8'
}, {
validates : ['presence', 'exclusion'],
exclusion : ['nil', 'null']
});
v.ensure({
source : 'item9',
display : '字段9'
}, {
validates : ['presence', 'date']
});
</script>
</head>
<body>
<form action="#" method="get" id="test_form" onsubmit="return v.check_all()">
<label> not empty:
<input type="text" name="item1" id="item1" onblur="v.check('item1')"/>
<i id="item1_status"> </i> </label>
<br />
<label> more than 4:
<input type="text" name="item2" id="item2" onblur="v.check('item2')"/>
<i id="item2_status"> </i> </label>
<br />
<label> less than 6:
<input type="text" name="item3" id="item3" onblur="v.check('item3')"/>
<i id="item3_status"> </i> </label>
<br />
<label> between 4 and 6:
<input type="text" name="item4" id="item4" onblur="v.check('item4')"/>
<i id="item4_status"> </i> </label>
<br />
<label> custom regex:
<input type="text" name="item5" id="item5" onblur="v.check('item5')"/>
<i id="item5_status"> </i> </label>
<br />
<label> by function:
<input type="text" name="item6" id="item6" onblur="v.check('item6')"/>
<i id="item6_status"> </i> </label>
<br />
<label> inclusion:
<input type="text" name="item7" id="item7" onblur="v.check('item7')"/>
<i id="item7_status"> </i> </label>
<br />
<label> exclusion:
<input type="text" name="item8" id="item8" onblur="v.check('item8')"/>
<i id="item8_status"> </i> </label>
<br />
<label> custom(date):
<input type="text" name="item9" id="item9" onblur="v.check('item9')"/>
<i id="item9_status"> </i> </label>
<br />
<input type="submit" />
</form>
</body>
</html>