3 <title>CGI/Ex/validate.js tests
</title>
4 <script src=
"../lib/CGI/Ex/validate.js"></script>
8 <h1>CGI/Ex/validate.js tests
</h1>
9 <div style=
"background:#eee">
12 <input type=text size=
3 name=text1
>
13 <input type=text size=
3 name=text2
>
14 <input type=text size=
3 name=text3
>
15 <input type=text size=
3 name=text3
>
16 <input type=password size=
3 name=password1
>
17 <input type=hidden name=hidden1
>
18 <select name=select1
><option>foo
</option></select>
19 <select name=select2
><option value=
"foo">foo
</option></select>
20 <select name=select3
><option value=
"foo">foo
</option><option value=
"bar">bar
</option></select>
21 <select name=select4 multiple=multiple
><option value=
"foo">foo
</option><option value=
"bar">bar
</option></select>
22 <textarea name=textarea1 rows=
1 cols=
3></textarea>
23 <input type=checkbox name=checkbox1 value=
1>
24 <input type=checkbox name=checkbox2 value=
1><input type=checkbox name=checkbox2 value=
2>
25 <input type=radio name=radio1 value=
1>
26 <input type=radio name=radio2 value=
1><input type=radio name=radio2 value=
2>
29 <div id=
"output"></div>
35 function ok (is_ok, str) {
37 str = ""+ (is_ok ? "ok" : "not ok") +" "+ (++ok_i) +" - "+ str;
38 str = "
<span style=color:
"+(is_ok ? "green
" : "red
")+">"+ str +"</span><br>\n";
39 var el = document.getElementById("output")
40 el.innerHTML = str + el.innerHTML;
43 function validate (vars, vals) {
44 var f = document.the_form;
45 f.text1.value = typeof(vars.text1 ) != 'undefined' ? vars.text1 : '';
46 f.text2.value = typeof(vars.text2 ) != 'undefined' ? vars.text2 : '';
47 f.textarea1.value = typeof(vars.textarea1) != 'undefined' ? vars.textarea1 : '';
48 f.password1.value = typeof(vars.password1) != 'undefined' ? vars.password1 : '';
50 for (var j =
0; j < f.checkbox2.length; j++)
51 f.checkbox2[j].checked = false;
53 for (var i =
0; i < vars.checkbox2.length; i++)
54 for (var j =
0; j < f.checkbox2.length; j++)
55 if (vars.checkbox2[i] == f.checkbox2[j].value) f.checkbox2[j].checked = true;
57 var err_obj = v_validate(f, vals);
58 if (err_obj) return err_obj.as_hash();
62 function run_tests () {
63 var begin = new Date();
64 begin = begin.getTime();
66 ok(document.validate, "Found document.validate function");
67 ok(v_get_form_value, "Found v_get_form_value function ");
69 var form = document.the_form;
70 ok(form, "Got the form");
73 var e = validate({}, {text1:{required:
1}});
74 ok(e, "Got required error");
75 ok(e.text1_error == "The field text1 is required.", "Got the right required error");
77 e = validate({text1:
1}, {text1:{required:
1}});
78 ok(! e, "Got no required error");
82 e = validate({}, {text1:{required:
1, validate_if:'text2'}});
83 ok(! e, "Got no error on validate_if");
84 e = validate({text2:
1}, {text1:{required:
1, validate_if:'text2'}});
85 ok(e, "Got validate_if error");
86 ok(e.text1_error == "The field text1 is required.", "Got the right required error");
88 var v = {text1:{required:
1, validate_if:'text2 was_valid'}, text2:{validate_if:'text3'}};
90 ok(! e, "Got no error on validate_if with was_valid");
91 e = validate({text2:
1}, v);
92 ok(! e, "Got no error on validate_if with was_valid with non-validated data");
93 e = validate({text3:
1}, v);
94 ok(! e, "Got no error on validate_if with was_valid with validated - bad data");
95 e = validate({text2:
1, text3:
1}, v);
96 ok(! e, "Got error on validate_if with was_valid with validated - good data");
97 e = validate({text1:
1, text2:
1, text3:
1}, v);
98 ok(! e, "No error on validate_if with was_valid with validated - good data");
100 v = {text1:{required:
1, validate_if:'text2 had_error'}, text2:{required:
1}};
102 ok(e, "Got error on validate_if with had_error");
103 e = validate({text2:
1}, v);
104 ok(! e, "No error on validate_if with had_error and bad_data");
105 e = validate({text1:
1}, v);
106 ok(e && ! e.text1_error, "No error on validate_if with had_error and good data");
109 e = validate({}, {text1:{required_if:'text2'}});
110 ok(! e, "No required_if error");
111 e = validate({text2:
1}, {text1:{required_if:'text2'}});
112 ok(e, "Required_if error");
113 ok(e.text1_error == "The field text1 is required.", "Got the right required error");
116 e = validate({checkbox2:[
1,
2]}, {checkbox2:{required:
1}});
117 ok(e, "Got max_values error "+e.checkbox2_error);
118 ok(e.checkbox2_error == "The field checkbox2 had more than
1 value.", "Got the right max_values error");
119 e = validate({checkbox2:[
1]}, {checkbox2:{max_values:
2}});
120 ok(! e, "No max_values error");
121 e = validate({checkbox2:[
1,
2]}, {checkbox2:{max_values:
2}});
122 ok(! e, "No max_values error");
125 e = validate({checkbox2:[
1]}, {checkbox2:{min_values:
2}});
126 ok(e, "Got min_values error");
127 ok(e.checkbox2_error == "The field checkbox2 had less than
2 values.", "Got the right min_values error");
128 e = validate({checkbox2:[
1,
2]}, {checkbox2:{min_values:
2, max_values:
10}});
129 ok(! e, "No min_values error");
132 v = {text1:{'enum':[
1,
2,
3]}};
134 ok(e, "Got enum error");
135 ok(e.text1_error == "The field text1 is not in the given list.", "Got the right enum error");
136 e = validate({text1:
1}, v);
137 ok(! e, "No enum error");
139 v = {text1:{'enum':[
1,
2,
3],match:'m/
3/'}};
140 e = validate({text1:
1}, v);
142 ok(e.text1_error == "The field text1 contains invalid characters.", 'enum shortcircuit');
144 e = validate({text1:
4}, v);
146 ok(e.text1_error == "The field text1 is not in the given list.", 'enum shortcircuit2');
148 v = {text2:{'enum':"
1 ||
2||
3"}};
149 e = validate({text2:
1}, v);
150 ok(! e, "No enum error");
151 e = validate({text2:
4}, v);
152 ok(e, "Got enum error");
155 v = {text1:{equals:'text2'}};
157 ok(! e, 'No equals error');
159 e = validate({text1:
1}, v);
160 ok(e, "Got an equals error");
161 ok(e.text1_error == "The field text1 did not equal the field text2.", "Got the right equals error");
162 e = validate({text1:
1, text2:
2}, v);
163 ok(e, "Got equals error");
164 e = validate({text1:
1, text2:
1}, v);
165 ok(! e, "No equals error");
166 v = {text1:{equals:'"text2"'}};
167 e = validate({text1:
1, text2:
1}, v);
168 ok(e, "Got equals error");
169 e = validate({text1:'text2', text2:
1}, v);
170 ok(! e, "No equals error");
173 v = {text1:{min_len:
1}};
175 ok(e, "Got min_len error");
176 ok(e.text1_error == "The field text1 was less than
1 character.", "Got the right min_len error");
177 v = {text1:{min_len:
10}};
179 ok(e, "Got min_len error");
180 ok(e.text1_error == "The field text1 was less than
10 characters.", "Got the right min_len error");
181 e = validate({text1:"
123456789"}, v);
182 ok(e, "Got a min_len error");
183 e = validate({text1:"
1234567890"}, v);
184 ok(! e, "No min_len error");
187 v = {text1:{max_len:
10}};
189 ok(! e, "No max_len error");
190 e = validate({text1:""}, v);
191 ok(! e, "No max_len error");
192 e = validate({text1:"
1234567890"}, v);
193 ok(! e, "No max_len error");
194 e = validate({text1:"
12345678901"}, v);
195 ok(e, "Got a max_len error");
196 ok(e.text1_error == "The field text1 was more than
10 characters.", "Got the right max_len error");
197 v = {text1:{max_len:
1}};
198 e = validate({text1:"
12345678901"}, v);
199 ok(e, "Got a max_len error");
200 ok(e.text1_error == "The field text1 was more than
1 character.", "Got the right max_len error");
203 v = {text1:{match:'m/^\\w+$/'}};
204 e = validate({text1:"abc"}, v);
205 ok(! e, "No match error");
206 e = validate({text1:"abc."}, v);
207 ok(e, "Got a match error");
208 ok(e.text1_error == "The field text1 contains invalid characters.", "Got the right match error");
210 v = {text1:{match_2:'m/^\\w+$/',match_2_error:'Bad'}};
211 e = validate({text1:"abc"}, v);
212 ok(! e, "No match error");
213 e = validate({text1:"abc."}, v);
214 ok(e, "Got a match error");
215 ok(e.text1_error == "Bad", "Got the right match error");
217 v = {text1:{match:['m/^\\w+$/', 'm/^[a-z]+$/']}};
218 e = validate({text1:"abc"}, v);
219 ok(! e, "No match error with multiple");
220 e = validate({text1:"abc1"}, v);
221 ok(e, "Got a match error with multiple");
223 v = {text1:{match:'m/^\\w+$/ || m/^[a-z]+$/'}};
224 e = validate({text1:"abc"}, v);
225 ok(! e, "No match error with multiple match string");
226 e = validate({text1:"abc1"}, v);
227 ok(e, "Got a match error with multiple match string");
229 v = {text1:{match:'! m/^\\w+$/'}};
230 e = validate({text1:"abc"}, v);
231 ok(e, "Got a match error with not match string");
232 e = validate({text1:"abc."}, v);
233 ok(! e, "No match error with not match string");
235 v = {text1:{match:'m/^\\w+$/'}};
237 ok(e, "Got an error with non-existing field");
238 v = {text1:{match:'! m/^\w+$/'}};
240 ok(! e, "No match error with non-existing field and not match string");
243 v = {text1:{compare:'
> 0'}};
245 ok(e,
"Got an error");
246 ok(e.text1_error ==
"The field text1 did not fit comparison.",
"Got the right compare error");
247 v = {text1:{compare:'==
0'}};
249 ok(! e,
"No compare error == 0");
250 v = {text1:{compare:'==
1'}};
252 ok(e,
"Got compare error == 1");
253 v = {text1:{compare:'<
0'}};
255 ok(e,
"Got compare error < 0");
256 v = {text1:{compare:'
> 10'}};
257 e = validate({text1:
11}, v);
258 ok(! e,
"No compare error > 10");
259 e = validate({text1:
10}, v);
260 ok(e,
"Got compare error > 10");
261 v = {text1:{compare:'==
10'}};
262 e = validate({text1:
11}, v);
263 ok(e,
"Got compare error == 10");
264 e = validate({text1:
10}, v);
265 ok(! e,
"No compare error == 10");
266 v = {text1:{compare:'<
10'}};
267 e = validate({text1:
9}, v);
268 ok(! e,
"No compare error < 10");
269 e = validate({text1:
10}, v);
270 ok(e,
"Got compare error < 10");
271 v = {text1:{compare:'
>=
10'}};
272 e = validate({text1:
10}, v);
273 ok(! e, "No compare error
>=
10");
274 e = validate({text1:
9}, v);
275 ok(e, "Got compare error
>=
10");
276 v = {text1:{compare:'!=
10'}};
277 e = validate({text1:
10}, v);
278 ok(e, "Got compare error
>=
10");
279 e = validate({text1:
9}, v);
280 ok(! e, "No compare error
>=
10");
281 v = {text1:{compare:'<=
10'}};
282 e = validate({text1:
11}, v);
283 ok(e, "Got compare error <=
10");
284 e = validate({text1:
10}, v);
285 ok(! e, "No compare error <=
10");
286 v = {text1:{compare:'gt ""'}};
288 ok(e, "Got compare error gt ''");
289 v = {text1:{compare:'eq ""'}};
291 ok(! e, "No compare error eq ''");
292 v = {text1:{compare:'lt ""'}};
294 ok(e, "Got compare error lt ''");
295 v = {text1:{compare:'gt "c"'}};
296 e = validate({text1:'d'}, v);
297 ok(! e, "No compare error gt 'c'");
298 e = validate({text1:'c'}, v);
299 ok(e, "Got compare error gt 'c'");
300 v = {text1:{compare:'eq c'}};
301 e = validate({text1:'d'}, v);
302 ok(e, "Got compare error eq c");
303 e = validate({text1:'c'}, v);
304 ok(! e, "No compare error lt c");
305 v = {text1:{compare:'lt c'}};
306 e = validate({text1:'b'}, v);
307 ok(! e, "No compare error lt c");
308 e = validate({text1:'c'}, v);
309 ok(e, "Got compare error lt c");
310 v = {text1:{compare:'ge c'}};
311 e = validate({text1:'c'}, v);
312 ok(! e, "No compare error ge c");
313 e = validate({text1:'b'}, v);
314 ok(e, "Got compare error ge c");
315 v = {text1:{compare:'ne c'}};
316 e = validate({text1:'c'}, v);
317 ok(e, "Got compare error ne c");
318 e = validate({text1:'b'}, v);
319 ok(! e, "No compare error ne c");
320 v = {text1:{compare:'le c'}};
321 e = validate({text1:'d'}, v);
322 ok(e, "Got compare error le c");
323 e = validate({text1:'c'}, v);
324 ok(! e, "No compare error le c");
327 v = {text1:{custom_js:" value ? true : false "}};
329 ok(e, "Got a custom_js error for eval type");
330 ok(e.text1_error == "The field text1 did not match custom_js check.", "Got the right custom_js error for eval type");
331 e = validate({text1:"str"}, v);
332 ok(! e, "No custom_js error for eval type");
333 v = {text1:{custom_js:function (args) { return args.value ? true : false }}};
335 ok(e, "Got a custom_js error for function type");
336 ok(e.text1_error == "The field text1 did not match custom_js check.", "Got the right custom_js error for function type");
337 e = validate({text1:"str"}, v);
338 ok(! e, "No custom_js error for function type");
340 e = validate({text1: "str"}, {text1: {custom_js:"throw 'Always fail ('+value+')'"}});
341 ok(e, 'Got an error');
342 ok(e.text1_error == "Always fail (str)", "Passed along the message from throw");
343 e = validate({text1: "str2"}, {text1: {custom_js:function (args) { throw "Always fail2 ("+args.value+")" }}});
344 ok(e, 'Got an error');
345 ok(e.text1_error == "Always fail2 (str2)", "Passed along the message from throw");
350 v = {text1: {type: 'ip', match: 'm/^
203\./'}};
351 e = validate({text1: '
209.108.25'}, v);
352 ok(e, 'type ip - with short circuit');
353 ok(e.text1_error == 'The field text1 did not match type ip.', 'type ip - was: '+e.text1_error); // make sure they short circuit
354 e = validate({text1: '
209.108.25.111'}, v);
355 ok(e, 'type ip - but had match error');
356 ok(e.text1_error == 'The field text1 contains invalid characters.', 'type ip');
357 e = validate({text1: '
203.108.25.111'}, v);
360 v = {text1:{type:'email'}};
361 e = validate({text1:'foo.bar.com'}, v)
362 ok(e, "Got an email error");
363 ok(e.text1_error == "The field text1 did not match type email.", "Got the right type email error");
364 e = validate({text1:'f oo@bar.com'}, v)
365 ok(e, "Got an email error");
366 ok(e.text1_error == "The field text1 did not match type email.", "Got the right type email error");
367 e = validate({text1:'foo@bar.com'}, v)
368 ok(!e, "No email error");
369 e = validate({text1:'+foo@bar.com'}, v)
370 ok(!e, "No email error");
371 e = validate({text1:'+bar.com'}, {text1:{type:'domain'}});
372 ok(e, "Got a domain error");
373 e = validate({text1:'foo-.bar.com'}, {text1:{type:'domain'}});
374 ok(e, "Got a domain error");
375 e = validate({text1:'foo..bar.com'}, {text1:{type:'domain'}});
376 ok(e, "Got a domain error");
377 e = validate({text1:'.foo.bar.com'}, {text1:{type:'domain'}});
378 ok(e, "Got a domain error");
379 e = validate({text1:'foo.bar.com'}, {text1:{type:'domain'}});
380 ok(!e, "No domain error");
381 e = validate({text1:'http://bar.com/'}, {text1:{type:'url'}});
382 ok(!e, "No url error");
383 e = validate({text1:'https://bar.com/foo.html'}, {text1:{type:'url'}});
384 ok(!e, "No url error");
385 e = validate({text1:'http://bar.com.:
8080/'}, {text1:{type:'url'}});
386 ok(!e, "No url error");
387 e = validate({text1:'http://bar.com/fo sdf'}, {text1:{type:'url'}});
388 ok(e, "Got url error");
389 e = validate({text1:'
234234234'}, {text1:{type:'cc'}});
390 ok(e, "Got cc error");
391 e = validate({text1:'
4242-
4242-
4242-
4242'}, {text1:{type:'cc'}});
392 ok(!e, "No cc error");
393 e = validate({text1:'
4242 4242 4242 4242'}, {text1:{type:'cc'}});
394 ok(!e, "No cc error");
395 e = validate({text1:'
4241-
4242-
4242-
4242'}, {text1:{type:'cc'}});
396 ok(e, "Got cc error");
397 e = validate({text1:'
4241-
4242-
4242'}, {text1:{type:'cc'}});
398 ok(e, "Got cc error");
399 for (var $_ in {"
0":
1, "
2":
1, "
23":
1, "-
0":
1, "-
2":
1, "-
23":
1, "
0.0":
1, "
.1":
1, "
0.1":
1, "
0.10":
1, "
1.0":
1, "
1.01":
1})
400 ok(!validate({text1: $_}, {text1: {type: 'num'}}), "Type num "+$_)
401 for (var $_ in {"
0":
1, "
2":
1, "
23":
1, "-
0":
1, "-
2":
1, "-
23":
1, "
2147483647":
1, "-
2147483648":
1})
402 ok(!validate({text1: $_}, {text1: {type: 'int'}}), "Type int "+$_);
403 for (var $_ in {"
0":
1, "
2":
1, "
23":
1, "
4294967295":
1})
404 ok(!validate({text1: $_}, {text1: {type: 'uint'}}), "Type uint "+$_);
405 for (var $_ in {"
0a":
1, "a2":
1, "-
0a":
1, "
0.
.0":
1, "
00":
1, "
001":
1, "
1.":
1})
406 ok(validate({text1: $_}, {text1: {type: 'num'}}), "Type num invalid "+$_);
407 for (var $_ in {"
1.1":
1, "
0.1":
1, "
0.0":
1, "-
1.1":
1, "
0a":
1, "a2":
1, "a":
1, "
00":
1, "
001":
1, "
2147483648":
1, "-
2147483649":
1})
408 ok(validate({text1: $_}, {text1: {type: 'int'}}), "Type int invalid "+$_);
409 for (var $_ in {"-
1":
1, "-
0":
1, "
1.1":
1, "
0.1":
1, "
0.0":
1, "-
1.1":
1, "
0a":
1, "a2":
1, "a":
1, "
00":
1, "
001":
1, "
4294967296":
1})
410 ok(validate({text1: $_}, {text1: {type: 'uint'}}), "Type uint invalid "+$_);
413 v = {text1:{min_in_set:'
2 of text1 text2 password1', max_values:
5}};
414 e = validate({text1:
1}, v);
415 ok(e, "Had a min_in_set error");
416 ok(e.text1_error == "Not enough fields were chosen from the set (
2 of text1, text2, password1)", "Got the right error");
417 e = validate({text1:
1, text2:
1}, v);
418 ok(! e, "No min_in_set error");
419 e = validate({text1:
1, text2:
0}, v);
420 ok(! e, "No min_in_set error");
423 v = {text1:{max_in_set:'
2 of text1 text2 password1'}};
424 e = validate({text1:
1}, v);
425 ok(! e, "No max_in_set error");
426 e = validate({text1:
1, text2:
1}, v);
427 ok(! e, "No max_in_set error");
428 e = validate({text1:
1, text2:
1, password1:
1}, v);
429 ok(e, "Got a max_in_set error");
431 // validate_if revisited (but negated - uses max_in_set)
432 v = {text1:{required:
1, validate_if:'! text2'}};
434 ok(e, "Got an error because validate_if failed");
435 e = validate({text2:
1}, v);
436 ok(! e, "Didn't run required because of validate_if");
439 v = {text1:{required:
1, 'default':'hmmmm'}};
441 ok(! e, "Didn't get an error because default value was found");
442 ok(document.the_form.text1.value == "hmmmm", "Default value got set in form");
444 // do_not_trim / case / replace
445 e = validate({text1:" hey "}, {text1:{required:
1}});
446 ok(! e, "No error - just trimmed");
447 ok(document.the_form.text1.value == "hey", "Got right trimmed value ("+document.the_form.text1.value+")");
448 e = validate({text1:"hey\n"}, {text1:{required:
1}});
449 ok(! e, "No error - just trimmed");
450 ok(document.the_form.text1.value == "hey", "Got right trimmed value ("+document.the_form.text1.value+")");
451 e = validate({text1:" "}, {text1:{required:
1}});
452 ok(e, "Got a required error because chars where trimmed");
453 e = validate({text1:" "}, {text1:{required:
1,do_not_trim:
1}});
454 ok(! e, "No error because we didn't trim");
455 e = validate({textarea1:"hey\n"}, {textarea1:{required:
1,do_not_trim:
1}});
456 ok(! e, "No error - just trimmed");
457 ok(document.the_form.textarea1.value.match(/hey\r?\n/), "Got right trimmed value ("+document.the_form.textarea1.value+")");
458 e = validate({textarea1:"\they\n"}, {textarea1:{required:
1,do_not_trim:
1,trim_control_chars:
1}});
459 ok(! e, "No error - just trimmed");
460 ok(document.the_form.textarea1.value == " hey", "Got right trimmed value ("+document.the_form.textarea1.value+")");
461 e = validate({textarea1:"hey"}, {textarea1:{to_upper_case:
1}});
462 ok(! e, "No error - just upper cased");
463 ok(document.the_form.textarea1.value == "HEY", "Got right uppercase value");
464 e = validate({textarea1:"HEY"}, {textarea1:{to_lower_case:
1}});
465 ok(! e, "No error - just lower cased");
466 ok(document.the_form.textarea1.value == "hey", "Got right lowercase value");
468 e = validate({textarea1:"wow"}, {textarea1:{replace:"s/w/W/g", replace2:"s/O/
0/i"}});
469 ok(! e, "No error - just replaced");
470 ok(document.the_form.textarea1.value == "W0W", "Got right value ("+document.the_form.textarea1.value+")");
473 //alert(_form_value(form.text2));
474 //form.text2[
0].value = "text1";
475 //form.text2[
1].value = "text2";
476 //ok(
1, "We can set and get values for duplicate named items");
478 var end = new Date();
480 var sec = (end - begin)/
1000;
482 var str = nok ? "
<span style=color:red
>Failed tests: "+nok+"
</span><br>\n" : "
<span style=color:green
>All tests passed
</span><br>\n";
483 str += "(Seconds: "+sec+")
<br>\n";
484 var el = document.getElementById("output")
485 el.innerHTML = str + el.innerHTML;
488 window.onload = run_tests;