forked from RubyLouvre/avalon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.html
333 lines (284 loc) · 11.2 KB
/
validation.html
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>Qunar.com</title>
<meta name="Robots" content="index,follow,NOODP" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
<script src="http://gist.corp.qunar.com/hao.lin/jvalidator/demo/jvalidator.min.js"></script>
<style>
input { outline:none; border:1px solid #000; }
</style>
<script>
$.jvalidator.addPattern('must',{
message : '必须写成12345' ,
validate : function( value , done ) {
done( value == "12345" );
}
});
function initForm( formSelector , options ) {
var jv = $(formSelector).jvalidator();
options = options || {};
$(formSelector).find('input:button').bind('click',function(evt){
jv.validateAll(function( result , elements ){
if( result ) {
console.log(elements)
alert('验证通过.');
} else {
var msg = ['验证未通过.']
console.log(elements)
for(var i=0; i<elements.length; i++){
msg.push( elements[i].getMessage() )
}
alert( msg.join('\n') );
}
});
});
if( options.events ) {
jv.when( options.events );
jv.success(function($event, errors){
console.log($event)
console.log(errors)
$(this.element).css('border','1px solid #000');
});
jv.fail(function( $event , errors ){
console.log($event)
console.log(errors)
$(this.element).css('border','1px solid #f00');
});
}
return jv;
}
</script>
</head>
<body style="padding:10px;">
<h2>简单表单验证</h2>
<form id="form1">
<pre>required</pre>
<input name="name" data-jvalidator-pattern="numeric&min_length[3]" />
<input type="button" value="验证" />
</form>
<form id="form2">
<pre>numeric</pre>
<input name="name" data-jvalidator-pattern="numeric" />
<input type="button" value="验证" />
</form>
<form id="form3">
<pre>numeric&greater_than[5] 必须大于5才可以</pre>
<input name="name" data-jvalidator-pattern="numeric&greater_than[5]" />
<input type="button" value="验证" />
</form>
<form id="form4">
<pre>non-required|numeric|alpha</pre>
<input name="name" data-jvalidator-pattern="non-required|numeric|alpha" />
<input type="button" value="验证" />
</form>
<script>
initForm('#form1');
initForm('#form2');
initForm('#form3');
initForm('#form4');
</script>
<h2>单框验证事件</h2>
<form id="form5">
<pre>numeric|alpha,<i>blur</i> 时触发</pre>
<input name="name" data-jvalidator-pattern="numeric|alpha" />
</form>
<script>
var f = initForm('#form5',{
events : ['blur']
});
</script>
<form id="form6">
<pre>numeric|alpha,<i>keypress</i> 时触发</pre>
<input name="name" data-jvalidator-pattern="numeric|alpha" />
</form>
<script>
var f = initForm('#form6',{
events : ['keyup']
});
</script>
<form id="form6_1">
<pre>自定义规则:值必须为<i>12345</i></pre>
<input name="name" data-jvalidator-pattern="must" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form6_1');
</script>
<h2>高级验证规则</h2>
<form id="form7">
<pre>重复密码</pre>
<input name="pass" data-jvalidator-cname="密码" data-jvalidator-pattern="alpha_numeric" />
<input name="repass" data-jvalidator-pattern="match[@pass]" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form7');
</script>
<form id="form8">
<pre>重复密码,单框验证</pre>
<input name="pass" data-jvalidator-pattern="alpha_numeric" />
<input name="repass" data-jvalidator-pattern="match[@pass]" />
</form>
<script>
var f = initForm('#form8',{
events : ['keyup','blur']
});
</script>
<h2>不仅是 input</h2>
<form id="form9">
<pre>select, checkbox, radiobox, textarea</pre>
<select data-jvalidator-pattern="match[aaa]">
<option value="aaa">aaa</option>
<option value="bbb">bbb</option>
</select> 必须选择 aaa
<br />
<label><input name="chk" type="checkbox" value="1" data-jvalidator-pattern="contain[1]|contain[2]" />1</label>
<label><input name="chk" type="checkbox" value="2" />2</label>
<label><input name="chk" type="checkbox" value="3" />3</label>
<label><input name="chk" type="checkbox" value="4" />4</label>
<label><input name="chk" type="checkbox" value="5" />5</label>
必须包含 1 或 2
<br />
<label><input name="rdo" type="radio" value="1" data-jvalidator-pattern="match[1]|match[2]" />1</label>
<label><input name="rdo" type="radio" value="2" />2</label>
<label><input name="rdo" type="radio" value="3" />3</label>
<label><input name="rdo" type="radio" value="4" />4</label>
<label><input name="rdo" type="radio" value="5" />5</label>
必须选择 1 或 2
<br />
<textarea data-jvalidator-pattern="required"></textarea>
必须有内容
<br />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form9');
</script>
<form id="form10">
<pre>组合验证 1: a.第一个框必须是字母或数字 b.判断第二个框是否与第一个相同</pre>
<input name="pwd" type="password" data-jvalidator-pattern="alpha_numeric" />
<input name="repwd" type="password" data-jvalidator-pattern="match[@pwd]" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form10');
</script>
<form id="form11">
<pre>组合验证 2: input中必须包含select选择的内容</pre>
<select name="name">
<option value="aaa">aaa</option>
<option value="bbb">bbb</option>
</select>
<input data-jvalidator-pattern="contain[@name]" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form11');
</script>
<form id="form12">
<pre>组合验证 3: 如果select选择的是aaa,那么input必须是111. 如果select选择的是bbb,那么input必须是222. </pre>
<em>城市:</em>
<select data-jvalidator-cname="城市" name="name">
<option value="aaa">aaa</option>
<option value="bbb">bbb</option>
</select>
<input data-jvalidator-pattern="(@name[aaa]&match[111])|(@name[bbb]&match[222])" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form12');
</script>
<form id="form13">
<pre>组合规则 4: 如果选择身份证,则按身份证规则校验。 如果选择护照,则按护照规则校验(0到20位字母或数字)</pre>
<label><input name="type" type="radio" value="idcard" checked="checked" />身份证</label>
<label><input name="type" type="radio" value="passport" />护照</label>
<br />
<input data-jvalidator-pattern="(@type[idcard]&idcard)|(@type[passport]&passport)" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form13');
</script>
<h2>提示信息</h2>
<form id="form14">
<pre>默认提示信息</pre>
<input data-jvalidator-pattern="required&numeric" /><em></em>
<br />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form14');
f.setMessage( 'required' , '亲,你必须写点儿内容。' )
f.setMessage( 'numeric' , '亲,你不能填写%val的,这里必须是数字哦。' )
</script>
<form id="form16">
<pre>使用自定义提示信息变量</pre>
<select name="city" data-jvalidator-cname="城市">
<option value="北京">北京</option>
<option value="上海">上海</option>
</select>
姓名:<input name="name" data-jvalidator-cname="姓名" data-jvalidator-pattern="required" />
护照:<input name="passport" data-jvalidator-pattern="required" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form16');
f.setMessage( '@name' , 'required' , '(required提示) %cname(%name)字段必须写哦,而且我也知道你选的@city是=@city哦。' )
f.setMessage( '@passport' , 'required' , '(required提示) 护照必须填的亲!' )
</script>
<h2>其它</h2>
<form id="form17">
<pre>关于placeholder. 正确:随便输入点儿什么</pre>
<input data-jvalidator-placeholder="请输入文字" value="请输入文字" data-jvalidator-pattern="required" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form17');
</script>
<form id="form17_1">
<pre>异步验证:必须是数字,且等3秒才会验证成功</pre>
<input data-jvalidator-pattern="numeric&checked" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form17_1');
$.jvalidator.addPattern('checked',{
message : "异步验证" ,
validate : function( value , done ){
setTimeout(function(){
done(true)
},3000)
}
});
</script>
<form id="form18">
<pre>异步+组合验证. 正确:输入身份证或`北京`</pre>
<label><input name="type" type="radio" value="idcard" checked="checked" />身份证</label>
<label><input name="type" type="radio" value="beijing" />ip北京</label>
<br />
<input data-jvalidator-pattern="(@type[idcard]&idcard)|(@type[beijing]&beijing)" />
<input type="button" value="验证" />
</form>
<script>
var f = initForm('#form18');
$.jvalidator.addPattern('beijing',{
message : "当前位置必须是在北京" ,
validate : function( value , done ){
$.ajax({
url : "http://ws.qunar.com/ips.jcp" ,
dataType : "jsonp" ,
jsonpCallback : "callback" ,
success : function( data, textStatus, jqXHR ){
done( data.city == value );
}
});
}
});
</script>
</body>
</html>