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");
89 e = validate({}, {text1:{required_if:'text2'}});
90 ok(! e, "No required_if error");
91 e = validate({text2:
1}, {text1:{required_if:'text2'}});
92 ok(e, "Required_if error");
93 ok(e.text1_error == "The field text1 is required.", "Got the right required error");
96 e = validate({checkbox2:[
1,
2]}, {checkbox2:{required:
1}});
97 ok(e, "Got max_values error "+e.checkbox2_error);
98 ok(e.checkbox2_error == "The field checkbox2 had more than
1 value.", "Got the right max_values error");
99 e = validate({checkbox2:[
1]}, {checkbox2:{max_values:
2}});
100 ok(! e, "No max_values error");
101 e = validate({checkbox2:[
1,
2]}, {checkbox2:{max_values:
2}});
102 ok(! e, "No max_values error");
105 e = validate({checkbox2:[
1]}, {checkbox2:{min_values:
2}});
106 ok(e, "Got min_values error");
107 ok(e.checkbox2_error == "The field checkbox2 had less than
2 values.", "Got the right min_values error");
108 e = validate({checkbox2:[
1,
2]}, {checkbox2:{min_values:
2, max_values:
10}});
109 ok(! e, "No min_values error");
112 var v = {text1:{'enum':[
1,
2,
3]}};
114 ok(e, "Got enum error");
115 ok(e.text1_error == "The field text1 is not in the given list.", "Got the right enum error");
116 e = validate({text1:
1}, v);
117 ok(! e, "No enum error");
118 e = validate({text1:
4}, v);
119 ok(e, "Got enum error");
121 v = {text2:{'enum':"
1 ||
2||
3"}};
122 e = validate({text2:
1}, v);
123 ok(! e, "No enum error");
124 e = validate({text2:
4}, v);
125 ok(e, "Got enum error");
128 v = {text1:{equals:'text2'}};
130 ok(! e, 'No equals error');
132 e = validate({text1:
1}, v);
133 ok(e, "Got an equals error");
134 ok(e.text1_error == "The field text1 did not equal the field text2.", "Got the right equals error");
135 e = validate({text1:
1, text2:
2}, v);
136 ok(e, "Got equals error");
137 e = validate({text1:
1, text2:
1}, v);
138 ok(! e, "No equals error");
139 v = {text1:{equals:'"text2"'}};
140 e = validate({text1:
1, text2:
1}, v);
141 ok(e, "Got equals error");
142 e = validate({text1:'text2', text2:
1}, v);
143 ok(! e, "No equals error");
146 v = {text1:{min_len:
1}};
148 ok(e, "Got min_len error");
149 ok(e.text1_error == "The field text1 was less than
1 character.", "Got the right min_len error");
150 v = {text1:{min_len:
10}};
152 ok(e, "Got min_len error");
153 ok(e.text1_error == "The field text1 was less than
10 characters.", "Got the right min_len error");
154 e = validate({text1:"
123456789"}, v);
155 ok(e, "Got a min_len error");
156 e = validate({text1:"
1234567890"}, v);
157 ok(! e, "No min_len error");
160 v = {text1:{max_len:
10}};
162 ok(! e, "No max_len error");
163 e = validate({text1:""}, v);
164 ok(! e, "No max_len error");
165 e = validate({text1:"
1234567890"}, v);
166 ok(! e, "No max_len error");
167 e = validate({text1:"
12345678901"}, v);
168 ok(e, "Got a max_len error");
169 ok(e.text1_error == "The field text1 was more than
10 characters.", "Got the right max_len error");
170 v = {text1:{max_len:
1}};
171 e = validate({text1:"
12345678901"}, v);
172 ok(e, "Got a max_len error");
173 ok(e.text1_error == "The field text1 was more than
1 character.", "Got the right max_len error");
176 v = {text1:{match:'m/^\\w+$/'}};
177 e = validate({text1:"abc"}, v);
178 ok(! e, "No match error");
179 e = validate({text1:"abc."}, v);
180 ok(e, "Got a match error");
181 ok(e.text1_error == "The field text1 contains invalid characters.", "Got the right match error");
183 v = {text1:{match:['m/^\\w+$/', 'm/^[a-z]+$/']}};
184 e = validate({text1:"abc"}, v);
185 ok(! e, "No match error with multiple");
186 e = validate({text1:"abc1"}, v);
187 ok(e, "Got a match error with multiple");
189 v = {text1:{match:'m/^\\w+$/ || m/^[a-z]+$/'}};
190 e = validate({text1:"abc"}, v);
191 ok(! e, "No match error with multiple match string");
192 e = validate({text1:"abc1"}, v);
193 ok(e, "Got a match error with multiple match string");
195 v = {text1:{match:'! m/^\\w+$/'}};
196 e = validate({text1:"abc"}, v);
197 ok(e, "Got a match error with not match string");
198 e = validate({text1:"abc."}, v);
199 ok(! e, "No match error with not match string");
201 v = {text1:{match:'m/^\\w+$/'}};
203 ok(e, "Got an error with non-existing field");
204 v = {text1:{match:'! m/^\w+$/'}};
206 ok(! e, "No match error with non-existing field and not match string");
209 v = {text1:{compare:'
> 0'}};
211 ok(e,
"Got an error");
212 ok(e.text1_error ==
"The field text1 did not fit comparison.",
"Got the right compare error");
213 v = {text1:{compare:'==
0'}};
215 ok(! e,
"No compare error == 0");
216 v = {text1:{compare:'==
1'}};
218 ok(e,
"Got compare error == 1");
219 v = {text1:{compare:'<
0'}};
221 ok(e,
"Got compare error < 0");
222 v = {text1:{compare:'
> 10'}};
223 e = validate({text1:
11}, v);
224 ok(! e,
"No compare error > 10");
225 e = validate({text1:
10}, v);
226 ok(e,
"Got compare error > 10");
227 v = {text1:{compare:'==
10'}};
228 e = validate({text1:
11}, v);
229 ok(e,
"Got compare error == 10");
230 e = validate({text1:
10}, v);
231 ok(! e,
"No compare error == 10");
232 v = {text1:{compare:'<
10'}};
233 e = validate({text1:
9}, v);
234 ok(! e,
"No compare error < 10");
235 e = validate({text1:
10}, v);
236 ok(e,
"Got compare error < 10");
237 v = {text1:{compare:'
>=
10'}};
238 e = validate({text1:
10}, v);
239 ok(! e, "No compare error
>=
10");
240 e = validate({text1:
9}, v);
241 ok(e, "Got compare error
>=
10");
242 v = {text1:{compare:'!=
10'}};
243 e = validate({text1:
10}, v);
244 ok(e, "Got compare error
>=
10");
245 e = validate({text1:
9}, v);
246 ok(! e, "No 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:'gt ""'}};
254 ok(e, "Got compare error gt ''");
255 v = {text1:{compare:'eq ""'}};
257 ok(! e, "No compare error eq ''");
258 v = {text1:{compare:'lt ""'}};
260 ok(e, "Got compare error lt ''");
261 v = {text1:{compare:'gt "c"'}};
262 e = validate({text1:'d'}, v);
263 ok(! e, "No compare error gt 'c'");
264 e = validate({text1:'c'}, v);
265 ok(e, "Got compare error gt 'c'");
266 v = {text1:{compare:'eq c'}};
267 e = validate({text1:'d'}, v);
268 ok(e, "Got compare error eq c");
269 e = validate({text1:'c'}, v);
270 ok(! e, "No compare error lt c");
271 v = {text1:{compare:'lt c'}};
272 e = validate({text1:'b'}, v);
273 ok(! e, "No compare error lt c");
274 e = validate({text1:'c'}, v);
275 ok(e, "Got compare error lt c");
276 v = {text1:{compare:'ge c'}};
277 e = validate({text1:'c'}, v);
278 ok(! e, "No compare error ge c");
279 e = validate({text1:'b'}, v);
280 ok(e, "Got compare error ge c");
281 v = {text1:{compare:'ne c'}};
282 e = validate({text1:'c'}, v);
283 ok(e, "Got compare error ne c");
284 e = validate({text1:'b'}, v);
285 ok(! e, "No compare error ne c");
286 v = {text1:{compare:'le c'}};
287 e = validate({text1:'d'}, v);
288 ok(e, "Got compare error le c");
289 e = validate({text1:'c'}, v);
290 ok(! e, "No compare error le c");
293 v = {text1:{custom_js:" value ? true : false "}};
295 ok(e, "Got a custom_js error for eval type");
296 ok(e.text1_error == "The field text1 did not match custom_js check.", "Got the right custom_js error for eval type");
297 e = validate({text1:"str"}, v);
298 ok(! e, "No custom_js error for eval type");
299 v = {text1:{custom_js:function (args) { return args.value ? true : false }}};
301 ok(e, "Got a custom_js error for function type");
302 ok(e.text1_error == "The field text1 did not match custom_js check.", "Got the right custom_js error for function type");
303 e = validate({text1:"str"}, v);
304 ok(! e, "No custom_js error for function type");
307 v = {text1:{type:'ip'}};
308 e = validate({text1:'
209.108.25'}, v);
309 ok(e, "Got type ip error");
310 ok(e.text1_error == "The field text1 did not match type ip.", "Got the right type ip error");
311 e = validate({text1:'
209.108.25.111'}, v);
312 ok(! e, "No type ip error");
313 v = {text1:{type:'email'}};
314 e = validate({text1:'foo.bar.com'}, v)
315 ok(e, "Got an email error");
316 ok(e.text1_error == "The field text1 did not match type email.", "Got the right type email error");
317 e = validate({text1:'f oo@bar.com'}, v)
318 ok(e, "Got an email error");
319 ok(e.text1_error == "The field text1 did not match type email.", "Got the right type email error");
320 e = validate({text1:'foo@bar.com'}, v)
321 ok(!e, "No email error");
322 e = validate({text1:'+foo@bar.com'}, v)
323 ok(!e, "No email error");
324 e = validate({text1:'+bar.com'}, {text1:{type:'domain'}});
325 ok(e, "Got a domain error");
326 e = validate({text1:'foo-.bar.com'}, {text1:{type:'domain'}});
327 ok(e, "Got a domain error");
328 e = validate({text1:'foo..bar.com'}, {text1:{type:'domain'}});
329 ok(e, "Got a domain error");
330 e = validate({text1:'.foo.bar.com'}, {text1:{type:'domain'}});
331 ok(e, "Got a domain error");
332 e = validate({text1:'foo.bar.com'}, {text1:{type:'domain'}});
333 ok(!e, "No domain error");
334 e = validate({text1:'http://bar.com/'}, {text1:{type:'url'}});
335 ok(!e, "No url error");
336 e = validate({text1:'https://bar.com/foo.html'}, {text1:{type:'url'}});
337 ok(!e, "No url error");
338 e = validate({text1:'http://bar.com.:
8080/'}, {text1:{type:'url'}});
339 ok(!e, "No url error");
340 e = validate({text1:'http://bar.com/fo sdf'}, {text1:{type:'url'}});
341 ok(e, "Got url error");
342 e = validate({text1:'
234234234'}, {text1:{type:'cc'}});
343 ok(e, "Got cc error");
344 e = validate({text1:'
4242-
4242-
4242-
4242'}, {text1:{type:'cc'}});
345 ok(!e, "No cc error");
346 e = validate({text1:'
4242 4242 4242 4242'}, {text1:{type:'cc'}});
347 ok(!e, "No cc error");
348 e = validate({text1:'
4241-
4242-
4242-
4242'}, {text1:{type:'cc'}});
349 ok(e, "Got cc error");
350 e = validate({text1:'
4241-
4242-
4242'}, {text1:{type:'cc'}});
351 ok(e, "Got cc error");
354 v = {text1:{min_in_set:'
2 of text1 text2 password1', max_values:
5}};
355 e = validate({text1:
1}, v);
356 ok(e, "Had a min_in_set error");
357 ok(e.text1_error == "Not enough fields were chosen from the set (
2 of text1, text2, password1)", "Got the right error");
358 e = validate({text1:
1, text2:
1}, v);
359 ok(! e, "No min_in_set error");
360 e = validate({text1:
1, text2:
0}, v);
361 ok(! e, "No min_in_set error");
364 v = {text1:{max_in_set:'
2 of text1 text2 password1'}};
365 e = validate({text1:
1}, v);
366 ok(! e, "No max_in_set error");
367 e = validate({text1:
1, text2:
1}, v);
368 ok(! e, "No max_in_set error");
369 e = validate({text1:
1, text2:
1, password1:
1}, v);
370 ok(e, "Got a max_in_set error");
372 // validate_if revisited (but negated - uses max_in_set)
373 v = {text1:{required:
1, validate_if:'! text2'}};
375 ok(e, "Got an error because validate_if failed");
376 e = validate({text2:
1}, v);
377 ok(! e, "Didn't run required because of validate_if");
380 v = {text1:{required:
1, 'default':'hmmmm'}};
382 ok(! e, "Didn't get an error because default value was found");
383 ok(document.the_form.text1.value == "hmmmm", "Default value got set in form");
385 // do_not_trim / case / replace
386 e = validate({text1:" hey "}, {text1:{required:
1}});
387 ok(! e, "No error - just trimmed");
388 ok(document.the_form.text1.value == "hey", "Got right trimmed value ("+document.the_form.text1.value+")");
389 e = validate({text1:"hey\n"}, {text1:{required:
1}});
390 ok(! e, "No error - just trimmed");
391 ok(document.the_form.text1.value == "hey", "Got right trimmed value ("+document.the_form.text1.value+")");
392 e = validate({text1:" "}, {text1:{required:
1}});
393 ok(e, "Got a required error because chars where trimmed");
394 e = validate({text1:" "}, {text1:{required:
1,do_not_trim:
1}});
395 ok(! e, "No error because we didn't trim");
396 e = validate({textarea1:"hey\n"}, {textarea1:{required:
1,do_not_trim:
1}});
397 ok(! e, "No error - just trimmed");
398 ok(document.the_form.textarea1.value.match(/hey\r?\n/), "Got right trimmed value ("+document.the_form.textarea1.value+")");
399 e = validate({textarea1:"\they\n"}, {textarea1:{required:
1,do_not_trim:
1,trim_control_chars:
1}});
400 ok(! e, "No error - just trimmed");
401 ok(document.the_form.textarea1.value == " hey", "Got right trimmed value ("+document.the_form.textarea1.value+")");
402 e = validate({textarea1:"hey"}, {textarea1:{to_upper_case:
1}});
403 ok(! e, "No error - just upper cased");
404 ok(document.the_form.textarea1.value == "HEY", "Got right uppercase value");
405 e = validate({textarea1:"HEY"}, {textarea1:{to_lower_case:
1}});
406 ok(! e, "No error - just lower cased");
407 ok(document.the_form.textarea1.value == "hey", "Got right lowercase value");
409 e = validate({textarea1:"wow"}, {textarea1:{replace:"s/w/W/g", replace2:"s/O/
0/i"}});
410 ok(! e, "No error - just replaced");
411 ok(document.the_form.textarea1.value == "W0W", "Got right value ("+document.the_form.textarea1.value+")");
414 //alert(_form_value(form.text2));
415 //form.text2[
0].value = "text1";
416 //form.text2[
1].value = "text2";
417 //ok(
1, "We can set and get values for duplicate named items");
419 var end = new Date();
421 var sec = (end - begin)/
1000;
423 var str = nok ? "
<span style=color:red
>Failed tests: "+nok+"
</span><br>\n" : "
<span style=color:green
>All tests passed
</span><br>\n";
424 str += "(Seconds: "+sec+")
<br>\n";
425 var el = document.getElementById("output")
426 el.innerHTML = str + el.innerHTML;
429 window.onload = run_tests;