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");
138 e = validate({text1:
4}, v);
139 ok(e, "Got enum error");
141 v = {text2:{'enum':"
1 ||
2||
3"}};
142 e = validate({text2:
1}, v);
143 ok(! e, "No enum error");
144 e = validate({text2:
4}, v);
145 ok(e, "Got enum error");
148 v = {text1:{equals:'text2'}};
150 ok(! e, 'No equals error');
152 e = validate({text1:
1}, v);
153 ok(e, "Got an equals error");
154 ok(e.text1_error == "The field text1 did not equal the field text2.", "Got the right equals error");
155 e = validate({text1:
1, text2:
2}, v);
156 ok(e, "Got equals error");
157 e = validate({text1:
1, text2:
1}, v);
158 ok(! e, "No equals error");
159 v = {text1:{equals:'"text2"'}};
160 e = validate({text1:
1, text2:
1}, v);
161 ok(e, "Got equals error");
162 e = validate({text1:'text2', text2:
1}, v);
163 ok(! e, "No equals error");
166 v = {text1:{min_len:
1}};
168 ok(e, "Got min_len error");
169 ok(e.text1_error == "The field text1 was less than
1 character.", "Got the right min_len error");
170 v = {text1:{min_len:
10}};
172 ok(e, "Got min_len error");
173 ok(e.text1_error == "The field text1 was less than
10 characters.", "Got the right min_len error");
174 e = validate({text1:"
123456789"}, v);
175 ok(e, "Got a min_len error");
176 e = validate({text1:"
1234567890"}, v);
177 ok(! e, "No min_len error");
180 v = {text1:{max_len:
10}};
182 ok(! e, "No max_len error");
183 e = validate({text1:""}, v);
184 ok(! e, "No max_len error");
185 e = validate({text1:"
1234567890"}, v);
186 ok(! e, "No max_len error");
187 e = validate({text1:"
12345678901"}, v);
188 ok(e, "Got a max_len error");
189 ok(e.text1_error == "The field text1 was more than
10 characters.", "Got the right max_len error");
190 v = {text1:{max_len:
1}};
191 e = validate({text1:"
12345678901"}, v);
192 ok(e, "Got a max_len error");
193 ok(e.text1_error == "The field text1 was more than
1 character.", "Got the right max_len error");
196 v = {text1:{match:'m/^\\w+$/'}};
197 e = validate({text1:"abc"}, v);
198 ok(! e, "No match error");
199 e = validate({text1:"abc."}, v);
200 ok(e, "Got a match error");
201 ok(e.text1_error == "The field text1 contains invalid characters.", "Got the right match error");
203 v = {text1:{match:['m/^\\w+$/', 'm/^[a-z]+$/']}};
204 e = validate({text1:"abc"}, v);
205 ok(! e, "No match error with multiple");
206 e = validate({text1:"abc1"}, v);
207 ok(e, "Got a match error with multiple");
209 v = {text1:{match:'m/^\\w+$/ || m/^[a-z]+$/'}};
210 e = validate({text1:"abc"}, v);
211 ok(! e, "No match error with multiple match string");
212 e = validate({text1:"abc1"}, v);
213 ok(e, "Got a match error with multiple match string");
215 v = {text1:{match:'! m/^\\w+$/'}};
216 e = validate({text1:"abc"}, v);
217 ok(e, "Got a match error with not match string");
218 e = validate({text1:"abc."}, v);
219 ok(! e, "No match error with not match string");
221 v = {text1:{match:'m/^\\w+$/'}};
223 ok(e, "Got an error with non-existing field");
224 v = {text1:{match:'! m/^\w+$/'}};
226 ok(! e, "No match error with non-existing field and not match string");
229 v = {text1:{compare:'
> 0'}};
231 ok(e,
"Got an error");
232 ok(e.text1_error ==
"The field text1 did not fit comparison.",
"Got the right compare error");
233 v = {text1:{compare:'==
0'}};
235 ok(! e,
"No compare error == 0");
236 v = {text1:{compare:'==
1'}};
238 ok(e,
"Got compare error == 1");
239 v = {text1:{compare:'<
0'}};
241 ok(e,
"Got compare error < 0");
242 v = {text1:{compare:'
> 10'}};
243 e = validate({text1:
11}, v);
244 ok(! e,
"No compare error > 10");
245 e = validate({text1:
10}, v);
246 ok(e,
"Got compare error > 10");
247 v = {text1:{compare:'==
10'}};
248 e = validate({text1:
11}, v);
249 ok(e,
"Got compare error == 10");
250 e = validate({text1:
10}, v);
251 ok(! e,
"No compare error == 10");
252 v = {text1:{compare:'<
10'}};
253 e = validate({text1:
9}, v);
254 ok(! e,
"No compare error < 10");
255 e = validate({text1:
10}, v);
256 ok(e,
"Got compare error < 10");
257 v = {text1:{compare:'
>=
10'}};
258 e = validate({text1:
10}, v);
259 ok(! e, "No compare error
>=
10");
260 e = validate({text1:
9}, v);
261 ok(e, "Got compare error
>=
10");
262 v = {text1:{compare:'!=
10'}};
263 e = validate({text1:
10}, v);
264 ok(e, "Got compare error
>=
10");
265 e = validate({text1:
9}, v);
266 ok(! e, "No compare error
>=
10");
267 v = {text1:{compare:'<=
10'}};
268 e = validate({text1:
11}, v);
269 ok(e, "Got compare error <=
10");
270 e = validate({text1:
10}, v);
271 ok(! e, "No compare error <=
10");
272 v = {text1:{compare:'gt ""'}};
274 ok(e, "Got compare error gt ''");
275 v = {text1:{compare:'eq ""'}};
277 ok(! e, "No compare error eq ''");
278 v = {text1:{compare:'lt ""'}};
280 ok(e, "Got compare error lt ''");
281 v = {text1:{compare:'gt "c"'}};
282 e = validate({text1:'d'}, v);
283 ok(! e, "No compare error gt 'c'");
284 e = validate({text1:'c'}, v);
285 ok(e, "Got compare error gt 'c'");
286 v = {text1:{compare:'eq c'}};
287 e = validate({text1:'d'}, v);
288 ok(e, "Got compare error eq c");
289 e = validate({text1:'c'}, v);
290 ok(! e, "No compare error lt c");
291 v = {text1:{compare:'lt c'}};
292 e = validate({text1:'b'}, v);
293 ok(! e, "No compare error lt c");
294 e = validate({text1:'c'}, v);
295 ok(e, "Got compare error lt c");
296 v = {text1:{compare:'ge c'}};
297 e = validate({text1:'c'}, v);
298 ok(! e, "No compare error ge c");
299 e = validate({text1:'b'}, v);
300 ok(e, "Got compare error ge c");
301 v = {text1:{compare:'ne c'}};
302 e = validate({text1:'c'}, v);
303 ok(e, "Got compare error ne c");
304 e = validate({text1:'b'}, v);
305 ok(! e, "No compare error ne c");
306 v = {text1:{compare:'le c'}};
307 e = validate({text1:'d'}, v);
308 ok(e, "Got compare error le c");
309 e = validate({text1:'c'}, v);
310 ok(! e, "No compare error le c");
313 v = {text1:{custom_js:" value ? true : false "}};
315 ok(e, "Got a custom_js error for eval type");
316 ok(e.text1_error == "The field text1 did not match custom_js check.", "Got the right custom_js error for eval type");
317 e = validate({text1:"str"}, v);
318 ok(! e, "No custom_js error for eval type");
319 v = {text1:{custom_js:function (args) { return args.value ? true : false }}};
321 ok(e, "Got a custom_js error for function type");
322 ok(e.text1_error == "The field text1 did not match custom_js check.", "Got the right custom_js error for function type");
323 e = validate({text1:"str"}, v);
324 ok(! e, "No custom_js error for function type");
327 v = {text1:{type:'ip'}};
328 e = validate({text1:'
209.108.25'}, v);
329 ok(e, "Got type ip error");
330 ok(e.text1_error == "The field text1 did not match type ip.", "Got the right type ip error");
331 e = validate({text1:'
209.108.25.111'}, v);
332 ok(! e, "No type ip error");
333 v = {text1:{type:'email'}};
334 e = validate({text1:'foo.bar.com'}, v)
335 ok(e, "Got an email error");
336 ok(e.text1_error == "The field text1 did not match type email.", "Got the right type email error");
337 e = validate({text1:'f oo@bar.com'}, v)
338 ok(e, "Got an email error");
339 ok(e.text1_error == "The field text1 did not match type email.", "Got the right type email error");
340 e = validate({text1:'foo@bar.com'}, v)
341 ok(!e, "No email error");
342 e = validate({text1:'+foo@bar.com'}, v)
343 ok(!e, "No email error");
344 e = validate({text1:'+bar.com'}, {text1:{type:'domain'}});
345 ok(e, "Got a domain error");
346 e = validate({text1:'foo-.bar.com'}, {text1:{type:'domain'}});
347 ok(e, "Got a domain error");
348 e = validate({text1:'foo..bar.com'}, {text1:{type:'domain'}});
349 ok(e, "Got a domain error");
350 e = validate({text1:'.foo.bar.com'}, {text1:{type:'domain'}});
351 ok(e, "Got a domain error");
352 e = validate({text1:'foo.bar.com'}, {text1:{type:'domain'}});
353 ok(!e, "No domain error");
354 e = validate({text1:'http://bar.com/'}, {text1:{type:'url'}});
355 ok(!e, "No url error");
356 e = validate({text1:'https://bar.com/foo.html'}, {text1:{type:'url'}});
357 ok(!e, "No url error");
358 e = validate({text1:'http://bar.com.:
8080/'}, {text1:{type:'url'}});
359 ok(!e, "No url error");
360 e = validate({text1:'http://bar.com/fo sdf'}, {text1:{type:'url'}});
361 ok(e, "Got url error");
362 e = validate({text1:'
234234234'}, {text1:{type:'cc'}});
363 ok(e, "Got cc error");
364 e = validate({text1:'
4242-
4242-
4242-
4242'}, {text1:{type:'cc'}});
365 ok(!e, "No cc error");
366 e = validate({text1:'
4242 4242 4242 4242'}, {text1:{type:'cc'}});
367 ok(!e, "No cc error");
368 e = validate({text1:'
4241-
4242-
4242-
4242'}, {text1:{type:'cc'}});
369 ok(e, "Got cc error");
370 e = validate({text1:'
4241-
4242-
4242'}, {text1:{type:'cc'}});
371 ok(e, "Got cc error");
374 v = {text1:{min_in_set:'
2 of text1 text2 password1', max_values:
5}};
375 e = validate({text1:
1}, v);
376 ok(e, "Had a min_in_set error");
377 ok(e.text1_error == "Not enough fields were chosen from the set (
2 of text1, text2, password1)", "Got the right error");
378 e = validate({text1:
1, text2:
1}, v);
379 ok(! e, "No min_in_set error");
380 e = validate({text1:
1, text2:
0}, v);
381 ok(! e, "No min_in_set error");
384 v = {text1:{max_in_set:'
2 of text1 text2 password1'}};
385 e = validate({text1:
1}, v);
386 ok(! e, "No max_in_set error");
387 e = validate({text1:
1, text2:
1}, v);
388 ok(! e, "No max_in_set error");
389 e = validate({text1:
1, text2:
1, password1:
1}, v);
390 ok(e, "Got a max_in_set error");
392 // validate_if revisited (but negated - uses max_in_set)
393 v = {text1:{required:
1, validate_if:'! text2'}};
395 ok(e, "Got an error because validate_if failed");
396 e = validate({text2:
1}, v);
397 ok(! e, "Didn't run required because of validate_if");
400 v = {text1:{required:
1, 'default':'hmmmm'}};
402 ok(! e, "Didn't get an error because default value was found");
403 ok(document.the_form.text1.value == "hmmmm", "Default value got set in form");
405 // do_not_trim / case / replace
406 e = validate({text1:" hey "}, {text1:{required:
1}});
407 ok(! e, "No error - just trimmed");
408 ok(document.the_form.text1.value == "hey", "Got right trimmed value ("+document.the_form.text1.value+")");
409 e = validate({text1:"hey\n"}, {text1:{required:
1}});
410 ok(! e, "No error - just trimmed");
411 ok(document.the_form.text1.value == "hey", "Got right trimmed value ("+document.the_form.text1.value+")");
412 e = validate({text1:" "}, {text1:{required:
1}});
413 ok(e, "Got a required error because chars where trimmed");
414 e = validate({text1:" "}, {text1:{required:
1,do_not_trim:
1}});
415 ok(! e, "No error because we didn't trim");
416 e = validate({textarea1:"hey\n"}, {textarea1:{required:
1,do_not_trim:
1}});
417 ok(! e, "No error - just trimmed");
418 ok(document.the_form.textarea1.value.match(/hey\r?\n/), "Got right trimmed value ("+document.the_form.textarea1.value+")");
419 e = validate({textarea1:"\they\n"}, {textarea1:{required:
1,do_not_trim:
1,trim_control_chars:
1}});
420 ok(! e, "No error - just trimmed");
421 ok(document.the_form.textarea1.value == " hey", "Got right trimmed value ("+document.the_form.textarea1.value+")");
422 e = validate({textarea1:"hey"}, {textarea1:{to_upper_case:
1}});
423 ok(! e, "No error - just upper cased");
424 ok(document.the_form.textarea1.value == "HEY", "Got right uppercase value");
425 e = validate({textarea1:"HEY"}, {textarea1:{to_lower_case:
1}});
426 ok(! e, "No error - just lower cased");
427 ok(document.the_form.textarea1.value == "hey", "Got right lowercase value");
429 e = validate({textarea1:"wow"}, {textarea1:{replace:"s/w/W/g", replace2:"s/O/
0/i"}});
430 ok(! e, "No error - just replaced");
431 ok(document.the_form.textarea1.value == "W0W", "Got right value ("+document.the_form.textarea1.value+")");
434 //alert(_form_value(form.text2));
435 //form.text2[
0].value = "text1";
436 //form.text2[
1].value = "text2";
437 //ok(
1, "We can set and get values for duplicate named items");
439 var end = new Date();
441 var sec = (end - begin)/
1000;
443 var str = nok ? "
<span style=color:red
>Failed tests: "+nok+"
</span><br>\n" : "
<span style=color:green
>All tests passed
</span><br>\n";
444 str += "(Seconds: "+sec+")
<br>\n";
445 var el = document.getElementById("output")
446 el.innerHTML = str + el.innerHTML;
449 window.onload = run_tests;