Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 (function($) {
A 2
3 function methodTest( methodName ) {
4     var v = jQuery("#form").validate();
5     var method = $.validator.methods[methodName];
6     var element = $("#firstname")[0];
7     return function(value, param) {
8         element.value = value;
9         return method.call( v, value, element, param );
10     };
11 }
12
13 module("methods");
14
15 test("default messages", function() {
16     var m = $.validator.methods;
17     $.each(m, function(key) {
18         ok( jQuery.validator.messages[key], key + " has a default message." );
19     });
20 });
21
22 test("digit", function() {
23     var method = methodTest("digits");
24     ok( method( "123" ), "Valid digits" );
25     ok(!method( "123.000" ), "Invalid digits" );
26     ok(!method( "123.000,00" ), "Invalid digits" );
27     ok(!method( "123.0.0,0" ), "Invalid digits" );
28     ok(!method( "x123" ), "Invalid digits" );
29     ok(!method( "100.100,0,0" ), "Invalid digits" );
30 });
31
32 test("url", function() {
33     var method = methodTest("url");
34     ok( method( "http://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
35     ok( method( "https://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
36     ok( method( "ftp://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
37     ok( method( "http://www.føtex.dk/" ), "Valid url, danish unicode characters" );
38     ok( method( "http://bösendorfer.de/" ), "Valid url, german unicode characters" );
39     ok( method( "http://192.168.8.5" ), "Valid IP Address" );
40     ok(!method( "http://192.168.8." ), "Invalid IP Address" );
41     ok(!method( "http://bassistance" ), "Invalid url" ); // valid
42     ok(!method( "http://bassistance." ), "Invalid url" ); // valid
43     ok(!method( "http://bassistance,de" ), "Invalid url" );
44     ok(!method( "http://bassistance;de" ), "Invalid url" );
45     ok(!method( "http://.bassistancede" ), "Invalid url" );
46     ok(!method( "bassistance.de" ), "Invalid url" );
47 });
48
49 test("url2 (tld optional)", function() {
50     var method = methodTest("url2");
51     ok( method( "http://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
52     ok( method( "https://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
53     ok( method( "ftp://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
54     ok( method( "http://www.føtex.dk/" ), "Valid url, danish unicode characters" );
55     ok( method( "http://bösendorfer.de/" ), "Valid url, german unicode characters" );
56     ok( method( "http://192.168.8.5" ), "Valid IP Address" );
57     ok(!method( "http://192.168.8." ), "Invalid IP Address" );
58     ok( method( "http://bassistance" ), "Invalid url" );
59     ok( method( "http://bassistance." ), "Invalid url" );
60     ok(!method( "http://bassistance,de" ), "Invalid url" );
61     ok(!method( "http://bassistance;de" ), "Invalid url" );
62     ok(!method( "http://.bassistancede" ), "Invalid url" );
63     ok(!method( "bassistance.de" ), "Invalid url" );
64 });
65
66 test("email", function() {
67     var method = methodTest("email");
68     ok( method( "name@domain.tld" ), "Valid email" );
69     ok( method( "name@domain.tl" ), "Valid email" );
70     ok( method( "bart+bart@tokbox.com" ), "Valid email" );
71     ok( method( "bart+bart@tokbox.travel" ), "Valid email" );
72     ok( method( "n@d.tld" ), "Valid email" );
73     ok( method( "ole@føtex.dk"), "Valid email" );
74     ok( method( "jörn@bassistance.de"), "Valid email" );
75     ok( method( "bla.blu@g.mail.com"), "Valid email" );
76     ok( method( "\"Scott Gonzalez\"@example.com" ), "Valid email" );
77     ok( method( "\"Scott González\"@example.com" ), "Valid email" );
78     ok( method( "\"name.\"@domain.tld" ), "Valid email" ); // valid without top label
79     ok( method( "\"name,\"@domain.tld" ), "Valid email" ); // valid without top label
80     ok( method( "\"name;\"@domain.tld" ), "Valid email" ); // valid without top label
81     ok(!method( "name" ), "Invalid email" );
82     ok(!method( "name@" ), "Invalid email" );
83     ok(!method( "name@domain" ), "Invalid email" );
84     ok(!method( "name.@domain.tld" ), "Invalid email" );
85     ok(!method( "name,@domain.tld" ), "Invalid email" );
86     ok(!method( "name;@domain.tld" ), "Invalid email" );
87     ok(!method( "name;@domain.tld." ), "Invalid email" );
88 });
89
90 test("email2 (tld optional)", function() {
91     var method = methodTest("email2");
92     ok( method( "name@domain.tld" ), "Valid email" );
93     ok( method( "name@domain.tl" ), "Valid email" );
94     ok( method( "bart+bart@tokbox.com" ), "Valid email" );
95     ok( method( "bart+bart@tokbox.travel" ), "Valid email" );
96     ok( method( "n@d.tld" ), "Valid email" );
97     ok( method( "ole@føtex.dk"), "Valid email" );
98     ok( method( "jörn@bassistance.de"), "Valid email" );
99     ok( method( "bla.blu@g.mail.com"), "Valid email" );
100     ok( method( "\"Scott Gonzalez\"@example.com" ), "Valid email" );
101     ok( method( "\"Scott González\"@example.com" ), "Valid email" );
102     ok( method( "\"name.\"@domain.tld" ), "Valid email" ); // valid without top label
103     ok( method( "\"name,\"@domain.tld" ), "Valid email" ); // valid without top label
104     ok( method( "\"name;\"@domain.tld" ), "Valid email" ); // valid without top label
105     ok(!method( "name" ), "Invalid email" );
106     ok(!method( "name@" ), "Invalid email" );
107     ok( method( "name@domain" ), "Invalid email" );
108     ok(!method( "name.@domain.tld" ), "Invalid email" );
109     ok(!method( "name,@domain.tld" ), "Invalid email" );
110     ok(!method( "name;@domain.tld" ), "Invalid email" );
111 });
112
113 test("number", function() {
114     var method = methodTest("number");
115     ok( method( "123" ), "Valid number" );
116     ok( method( "-123" ), "Valid number" );
117     ok( method( "123,000" ), "Valid number" );
118     ok( method( "-123,000" ), "Valid number" );
119     ok( method( "123,000.00" ), "Valid number" );
120     ok( method( "-123,000.00" ), "Valid number" );
121     ok(!method( "123.000,00" ), "Invalid number" );
122     ok(!method( "123.0.0,0" ), "Invalid number" );
123     ok(!method( "x123" ), "Invalid number" );
124     ok(!method( "100.100,0,0" ), "Invalid number" );
125
126     ok( method( "" ), "Blank is valid" );
127     ok( method( "123" ), "Valid decimal" );
128     ok( method( "123000" ), "Valid decimal" );
129     ok( method( "123000.12" ), "Valid decimal" );
130     ok( method( "-123000.12" ), "Valid decimal" );
131     ok( method( "123.000" ), "Valid decimal" );
132     ok( method( "123,000.00" ), "Valid decimal" );
133     ok( method( "-123,000.00" ), "Valid decimal" );
134     ok( method( ".100" ), "Valid decimal" );
135     ok(!method( "1230,000.00" ), "Invalid decimal" );
136     ok(!method( "123.0.0,0" ), "Invalid decimal" );
137     ok(!method( "x123" ), "Invalid decimal" );
138     ok(!method( "100.100,0,0" ), "Invalid decimal" );
139 });
140
141 /* disabled for now, need to figure out how to test localized methods
142 test("numberDE", function() {
143     var method = methodTest("numberDE");
144     ok( method( "123" ), "Valid numberDE" );
145     ok( method( "-123" ), "Valid numberDE" );
146     ok( method( "123.000" ), "Valid numberDE" );
147     ok( method( "-123.000" ), "Valid numberDE" );
148     ok( method( "123.000,00" ), "Valid numberDE" );
149     ok( method( "-123.000,00" ), "Valid numberDE" );
150     ok(!method( "123,000.00" ), "Invalid numberDE" );
151     ok(!method( "123,0,0.0" ), "Invalid numberDE" );
152     ok(!method( "x123" ), "Invalid numberDE" );
153     ok(!method( "100,100.0.0" ), "Invalid numberDE" );
154
155     ok( method( "" ), "Blank is valid" );
156     ok( method( "123" ), "Valid decimalDE" );
157     ok( method( "123000" ), "Valid decimalDE" );
158     ok( method( "123000,12" ), "Valid decimalDE" );
159     ok( method( "-123000,12" ), "Valid decimalDE" );
160     ok( method( "123.000" ), "Valid decimalDE" );
161     ok( method( "123.000,00" ), "Valid decimalDE" );
162     ok( method( "-123.000,00" ), "Valid decimalDE" )
163     ok(!method( "123.0.0,0" ), "Invalid decimalDE" );
164     ok(!method( "x123" ), "Invalid decimalDE" );
165     ok(!method( "100,100.0.0" ), "Invalid decimalDE" );
166 });
167 */
168
169 test("date", function() {
170     var method = methodTest("date");
171     ok( method( "06/06/1990" ), "Valid date" );
172     ok( method( "6/6/06" ), "Valid date" );
173     ok(!method( "1990x-06-06" ), "Invalid date" );
174 });
175
176 test("dateISO", function() {
177     var method = methodTest("dateISO");
178     ok( method( "1990-06-06" ), "Valid date" );
179     ok( method( "1990/06/06" ), "Valid date" );
180     ok( method( "1990-6-6" ), "Valid date" );
181     ok( method( "1990/6/6" ), "Valid date" );
182     ok(!method( "1990-106-06" ), "Invalid date" );
183     ok(!method( "190-06-06" ), "Invalid date" );
184 });
185
186 /* disabled for now, need to figure out how to test localized methods
187 test("dateDE", function() {
188     var method = methodTest("dateDE");
189     ok( method( "03.06.1984" ), "Valid dateDE" );
190     ok( method( "3.6.84" ), "Valid dateDE" );
191     ok(!method( "6-6-06" ), "Invalid dateDE" );
192     ok(!method( "1990-06-06" ), "Invalid dateDE" );
193     ok(!method( "06/06/1990" ), "Invalid dateDE" );
194     ok(!method( "6/6/06" ), "Invalid dateDE" );
195 });
196 */
197
198 test("required", function() {
199     var v = jQuery("#form").validate(),
200         method = $.validator.methods.required,
201         e = $('#text1, #text1b, #hidden2, #select1, #select2');
202     ok( method.call( v, e[0].value, e[0]), "Valid text input" );
203     ok(!method.call( v, e[1].value, e[1]), "Invalid text input" );
204     ok(!method.call( v, e[1].value, e[2]), "Invalid text input" );
205
206     ok(!method.call( v, e[2].value, e[3]), "Invalid select" );
207     ok( method.call( v, e[3].value, e[4]), "Valid select" );
208
209     e = $('#area1, #area2, #pw1, #pw2');
210     ok( method.call( v, e[0].value, e[0]), "Valid textarea" );
211     ok(!method.call( v, e[1].value, e[1]), "Invalid textarea" );
212     ok( method.call( v, e[2].value, e[2]), "Valid password input" );
213     ok(!method.call( v, e[3].value, e[3]), "Invalid password input" );
214
215     e = $('#radio1, #radio2, #radio3');
216     ok(!method.call( v, e[0].value, e[0]), "Invalid radio" );
217     ok( method.call( v, e[1].value, e[1]), "Valid radio" );
218     ok( method.call( v, e[2].value, e[2]), "Valid radio" );
219
220     e = $('#check1, #check2');
221     ok( method.call( v, e[0].value, e[0]), "Valid checkbox" );
222     ok(!method.call( v, e[1].value, e[1]), "Invalid checkbox" );
223
224     e = $('#select1, #select2, #select3, #select4');
225     ok(!method.call( v, e[0].value, e[0]), "Invalid select" );
226     ok( method.call( v, e[1].value, e[1]), "Valid select" );
227     ok( method.call( v, e[2].value, e[2]), "Valid select" );
228     ok( method.call( v, e[3].value, e[3]), "Valid select" );
229 });
230
231 test("required with dependencies", function() {
232     var v = jQuery("#form").validate(),
233         method = $.validator.methods.required,
234         e = $('#hidden2, #select1, #area2, #radio1, #check2');
235     ok( method.call( v, e[0].value, e[0], "asffsaa" ), "Valid text input due to dependency not met" );
236     ok(!method.call( v, e[0].value, e[0], "input" ), "Invalid text input" );
237     ok( method.call( v, e[0].value, e[0], function() { return false; }), "Valid text input due to dependency not met" );
238     ok(!method.call( v, e[0].value, e[0], function() { return true; }), "Invalid text input" );
239     ok( method.call( v, e[1].value, e[1], "asfsfa" ), "Valid select due to dependency not met" );
240     ok(!method.call( v, e[1].value, e[1], "input" ), "Invalid select" );
241     ok( method.call( v, e[2].value, e[2], "asfsafsfa" ), "Valid textarea due to dependency not met" );
242     ok(!method.call( v, e[2].value, e[2], "input" ), "Invalid textarea" );
243     ok( method.call( v, e[3].value, e[3], "asfsafsfa" ), "Valid radio due to dependency not met" );
244     ok(!method.call( v, e[3].value, e[3], "input" ), "Invalid radio" );
245     ok( method.call( v, e[4].value, e[4], "asfsafsfa" ), "Valid checkbox due to dependency not met" );
246     ok(!method.call( v, e[4].value, e[4], "input" ), "Invalid checkbox" );
247 });
248
249 test("minlength", function() {
250     var v = jQuery("#form").validate(),
251         method = $.validator.methods.minlength,
252         param = 2,
253         e = $('#text1, #text1c, #text2, #text3');
254     ok( method.call( v, e[0].value, e[0], param), "Valid text input" );
255     ok(!method.call( v, e[1].value, e[1], param), "Invalid text input" );
256     ok(!method.call( v, e[2].value, e[2], param), "Invalid text input" );
257     ok( method.call( v, e[3].value, e[3], param), "Valid text input" );
258
259     e = $('#check1, #check2, #check3');
260     ok(!method.call( v, e[0].value, e[0], param), "Valid checkbox" );
261     ok( method.call( v, e[1].value, e[1], param), "Valid checkbox" );
262     ok( method.call( v, e[2].value, e[2], param), "Invalid checkbox" );
263
264     e = $('#select1, #select2, #select3, #select4, #select5');
265     ok(method.call( v, e[0].value, e[0], param), "Valid select " + e[0].id );
266     ok(!method.call( v, e[1].value, e[1], param), "Invalid select " + e[1].id );
267     ok( method.call( v, e[2].value, e[2], param), "Valid select " + e[2].id );
268     ok( method.call( v, e[3].value, e[3], param), "Valid select " + e[3].id );
269     ok( method.call( v, e[4].value, e[4], param), "Valid select " + e[4].id );
270 });
271
272 test("maxlength", function() {
273     var v = jQuery("#form").validate();
274     var method = $.validator.methods.maxlength,
275         param = 4,
276         e = $('#text1, #text2, #text3');
277     ok( method.call( v, e[0].value, e[0], param), "Valid text input" );
278     ok( method.call( v, e[1].value, e[1], param), "Valid text input" );
279     ok(!method.call( v, e[2].value, e[2], param), "Invalid text input" );
280
281     e = $('#check1, #check2, #check3');
282     ok( method.call( v, e[0].value, e[0], param), "Valid checkbox" );
283     ok( method.call( v, e[1].value, e[1], param), "Invalid checkbox" );
284     ok(!method.call( v, e[2].value, e[2], param), "Invalid checkbox" );
285
286     e = $('#select1, #select2, #select3, #select4');
287     ok( method.call( v, e[0].value, e[0], param), "Valid select" );
288     ok( method.call( v, e[1].value, e[1], param), "Valid select" );
289     ok( method.call( v, e[2].value, e[2], param), "Valid select" );
290     ok(!method.call( v, e[3].value, e[3], param), "Invalid select" );
291 });
292
293 test("rangelength", function() {
294     var v = jQuery("#form").validate();
295     var method = $.validator.methods.rangelength,
296         param = [2, 4],
297         e = $('#text1, #text2, #text3');
298     ok( method.call( v, e[0].value, e[0], param), "Valid text input" );
299     ok(!method.call( v, e[1].value, e[1], param), "Invalid text input" );
300     ok(!method.call( v, e[2].value, e[2], param), "Invalid text input" );
301 });
302
303 test("min", function() {
304     var v = jQuery("#form").validate();
305     var method = $.validator.methods.min,
306         param = 8,
307         e = $('#value1, #value2, #value3');
308     ok(!method.call( v, e[0].value, e[0], param), "Invalid text input" );
309     ok( method.call( v, e[1].value, e[1], param), "Valid text input" );
310     ok( method.call( v, e[2].value, e[2], param), "Valid text input" );
311 });
312
313 test("max", function() {
314     var v = jQuery("#form").validate();
315     var method = $.validator.methods.max,
316         param = 12,
317         e = $('#value1, #value2, #value3');
318     ok( method.call( v, e[0].value, e[0], param), "Valid text input" );
319     ok( method.call( v, e[1].value, e[1], param), "Valid text input" );
320     ok(!method.call( v, e[2].value, e[2], param), "Invalid text input" );
321 });
322
323 test("range", function() {
324     var v = jQuery("#form").validate();
325     var method = $.validator.methods.range,
326         param = [4,12],
327         e = $('#value1, #value2, #value3');
328     ok(!method.call( v, e[0].value, e[0], param), "Invalid text input" );
329     ok( method.call( v, e[1].value, e[1], param), "Valid text input" );
330     ok(!method.call( v, e[2].value, e[2], param), "Invalid text input" );
331 });
332
333 test("equalTo", function() {
334     var v = jQuery("#form").validate();
335     var method = $.validator.methods.equalTo,
336         e = $('#text1, #text2');
337     ok( method.call( v, "Test", e[0], "#text1" ), "Text input" );
338     ok( method.call( v, "T", e[1], "#text2" ), "Another one" );
339 });
340
341 test("creditcard", function() {
342     var method = methodTest("creditcard");
343     ok( method( "446-667-651" ), "Valid creditcard number" );
344     ok( method( "446 667 651" ), "Valid creditcard number" );
345     ok(!method( "asdf" ), "Invalid creditcard number" );
346 });
347
348 test("extension", function() {
349     var method = methodTest("extension");
350     ok( method( "picture.gif" ), "Valid default accept type" );
351     ok( method( "picture.jpg" ), "Valid default accept type" );
352     ok( method( "picture.jpeg" ), "Valid default accept type" );
353     ok( method( "picture.png" ), "Valid default accept type" );
354     ok(!method( "picture.pgn" ), "Invalid default accept type" );
355
356     var v = jQuery("#form").validate();
357     method = function(value, param) {
358         return $.validator.methods.extension.call(v, value, $('#text1')[0], param);
359     };
360     ok( method( "picture.doc", "doc" ), "Valid custom accept type" );
361     ok( method( "picture.pdf", "doc|pdf" ), "Valid custom accept type" );
362     ok( method( "picture.pdf", "pdf|doc" ), "Valid custom accept type" );
363     ok(!method( "picture.pdf", "doc" ), "Invalid custom accept type" );
364     ok(!method( "picture.doc", "pdf" ), "Invalid custom accept type" );
365
366     ok( method( "picture.pdf", "doc,pdf" ), "Valid custom accept type, comma seperated" );
367     ok( method( "picture.pdf", "pdf,doc" ), "Valid custom accept type, comma seperated" );
368     ok(!method( "picture.pdf", "gop,top" ), "Invalid custom accept type, comma seperated" );
369 });
370
371 test("remote", function() {
372     expect(7);
373     stop();
374     var e = $("#username");
375     var v = $("#userForm").validate({
376         rules: {
377             username: {
378                 required: true,
379                 remote: "users.php"
380             }
381         },
382         messages: {
383             username: {
384                 required: "Please",
385                 remote: jQuery.validator.format("{0} in use")
386             }
387         },
388         submitHandler: function() {
389             ok( false, "submitHandler may never be called when validating only elements");
390         }
391     });
392     $(document).ajaxStop(function() {
393         $(document).unbind("ajaxStop");
394         equal( 1, v.size(), "There must be one error" );
395         equal( "Peter in use", v.errorList[0].message );
396
397         $(document).ajaxStop(function() {
398             $(document).unbind("ajaxStop");
399             equal( 1, v.size(), "There must be one error" );
400             equal( "Peter2 in use", v.errorList[0].message );
401             start();
402         });
403         e.val("Peter2");
404         strictEqual( v.element(e), true, "new value, new request; dependency-mismatch considered as valid though" );
405     });
406     strictEqual( v.element(e), false, "invalid element, nothing entered yet" );
407     e.val("Peter");
408     strictEqual( v.element(e), true, "still invalid, because remote validation must block until it returns; dependency-mismatch considered as valid though" );
409 });
410
411 test("remote, customized ajax options", function() {
412     expect(2);
413     stop();
414     var v = $("#userForm").validate({
415         rules: {
416             username: {
417                 required: true,
418                 remote: {
419                     url: "users.php",
420                     type: "POST",
421                     beforeSend: function(request, settings) {
422                         deepEqual(settings.type, "POST");
423                         deepEqual(settings.data, "username=asdf&email=email.com");
424                     },
425                     data: {
426                         email: function() {
427                             return "email.com";
428                         }
429                     },
430                     complete: function() {
431                         start();
432                     }
433                 }
434             }
435         }
436     });
437     $("#username").val("asdf");
438     $("#userForm").valid();
439 });
440
441
442 test("remote extensions", function() {
443     expect(5);
444     stop();
445     var e = $("#username");
446     var v = $("#userForm").validate({
447         rules: {
448             username: {
449                 required: true,
450                 remote: "users2.php"
451             }
452         },
453         messages: {
454             username: {
455                 required: "Please"
456             }
457         },
458         submitHandler: function() {
459             ok( false, "submitHandler may never be called when validating only elements");
460         }
461     });
462     $(document).ajaxStop(function() {
463         $(document).unbind("ajaxStop");
464         equal( 1, v.size(), "There must be one error" );
465         equal( v.errorList[0].message, "asdf is already taken, please try something else" );
466         v.element(e);
467         equal( v.errorList[0].message, "asdf is already taken, please try something else", "message doesn't change on revalidation" );
468         start();
469     });
470     strictEqual( v.element(e), false, "invalid element, nothing entered yet" );
471     e.val("asdf");
472     strictEqual( v.element(e), true, "still invalid, because remote validation must block until it returns; dependency-mismatch considered as valid though" );
473 });
474
475 test("remote radio correct value sent", function() {
476     expect(1);
477     stop();
478     var e = $("#testForm10Radio2");
479     e.attr('checked', 'checked');
480     var v = $("#testForm10").validate({
481         rules: {
482             testForm10Radio: {
483                 required: true,
484                 remote: {
485                     url: "echo.php",
486                     dataType: "json",
487                     success: function(data) {
488                         equal( data['testForm10Radio'], '2', ' correct radio value sent' );
489                         start();
490                     }
491                 }
492             }
493         }
494     });
495
496     v.element(e);
497 });
498
499 test("remote reset clear old value", function() {
500     expect(1);
501     stop();
502     var e = $("#username");
503     var v = $("#userForm").validate({
504         rules: {
505             username: {
506                 required: true,
507                 remote: {
508                     url: "echo.php",
509                     dataFilter: function(data) {
510                         var json = JSON.parse(data);
511                         if(json.username === 'asdf') {
512                             return "\"asdf is already taken\"";
513                         }
514                         return "\"" + true + "\"";
515                     }
516                 }
517             }
518         }
519     });
520     $(document).ajaxStop(function() {
521         var waitTimeout;
522
523         $(document).unbind("ajaxStop");
524
525
526         $(document).ajaxStop(function() {
527             clearTimeout(waitTimeout);
528             ok( true, "Remote request sent to server" );
529             start();
530         });
531
532
533         v.resetForm();
534         e.val("asdf");
535         waitTimeout = setTimeout(function() {
536             ok( false, "Remote server did not get request");
537             start();
538         }, 200);
539         v.element(e);
540     });
541     e.val("asdf");
542     v.element(e);
543 });
544
545 module("additional methods");
546
547 test("phone (us)", function() {
548     var method = methodTest("phoneUS");
549     ok( method( "1(212)-999-2345" ), "Valid us phone number" );
550     ok( method( "212 999 2344" ), "Valid us phone number" );
551     ok( method( "212-999-0983" ), "Valid us phone number" );
552     ok(!method( "111-123-5434" ), "Invalid us phone number" );
553     ok(!method( "212 123 4567" ), "Invalid us phone number" );
554 });
555
556 test("phoneUK", function() {
557     var method = methodTest("phoneUK");
558     ok( method( "07222 555555" ), "Valid UK Phone Number" );
559     ok( method( "(07222) 555555" ), "Valid UK Phone Number" );
560     ok( method( "+44 7222 555 555" ), "Valid UK Phone Number" );
561     ok(!method( "7222 555555" ), "Invalid UK Phone Number" );
562     ok(!method( "+44 07222 555555" ), "Invalid UK Phone Number" );
563 });
564
565 test("mobileUK", function() {
566     var method = methodTest("mobileUK");
567     ok( method( "07734234323" ), "Valid UK Mobile Number" );
568     ok( method( "+447734234323" ), "Valid UK Mobile Number" );
569     ok(!method( "07034234323" ), "Invalid UK Mobile Number" );
570     ok(!method( "0753423432" ), "Invalid UK Mobile Number" );
571     ok(!method( "07604234323" ), "Invalid UK Mobile Number" );
572     ok(!method( "077342343234" ), "Invalid UK Mobile Number" );
573     ok(!method( "044342343234" ), "Invalid UK Mobile Number" );
574     ok(!method( "+44753423432" ), "Invalid UK Mobile Number" );
575     ok(!method( "+447604234323" ), "Invalid UK Mobile Number" );
576     ok(!method( "+4477342343234" ), "Invalid UK Mobile Number" );
577     ok(!method( "+4444342343234" ), "Invalid UK Mobile Number" );
578 });
579
580 test("dateITA", function() {
581     var method = methodTest("dateITA");
582     ok( method( "01/01/1900" ), "Valid date ITA" );
583     ok(!method( "01/13/1990" ), "Invalid date ITA" );
584     ok(!method( "01.01.1900" ), "Invalid date ITA" );
585     ok(!method( "01/01/199" ), "Invalid date ITA" );
586 });
587
588 test("iban", function() {
589     var method = methodTest("iban");
590     ok( method( "NL20INGB0001234567"), "Valid IBAN");
591     ok( method( "DE68 2105 0170 0012 3456 78"), "Valid IBAN");
592     ok( method( "NL20 INGB0001234567"), "Valid IBAN: invalid spacing");
593     ok( method( "NL20 INGB 00 0123 4567"), "Valid IBAN: invalid spacing");
594     ok( method( "XX40INGB000123456712341234"), "Valid (more or less) IBAN: unknown country, but checksum OK");
595
596     ok(!method( "NL20INGB000123456"), "Invalid IBAN: too short");
597     ok(!method( "NL20INGB00012345678"), "Invalid IBAN: too long");
598     ok(!method( "NL20INGB0001234566"), "Invalid IBAN: checksum incorrect");
599     ok(!method( "DE68 2105 0170 0012 3456 7"), "Invalid IBAN: too short");
600     ok(!method( "DE68 2105 0170 0012 3456 789"), "Invalid IBAN: too long");
601     ok(!method( "DE68 2105 0170 0012 3456 79"), "Invalid IBAN: checksum incorrect");
602
603     ok(!method( "NL54INGB00012345671234"), "Invalid IBAN too long, BUT CORRECT CHECKSUM");
604     ok(!method( "XX00INGB000123456712341234"), "Invalid IBAN: unknown country and checksum incorrect");
605
606     // sample IBANs for different countries
607     ok( method( "AL47 2121 1009 0000 0002 3569 8741"), "Valid IBAN - AL");
608     ok( method( "AD12 0001 2030 2003 5910 0100"), "Valid IBAN - AD");
609     ok( method( "AT61 1904 3002 3457 3201"), "Valid IBAN - AT");
610     ok( method( "AZ21 NABZ 0000 0000 1370 1000 1944"), "Valid IBAN - AZ");
611     ok( method( "BH67 BMAG 0000 1299 1234 56"), "Valid IBAN - BH");
612     ok( method( "BE62 5100 0754 7061"), "Valid IBAN - BE");
613     ok( method( "BA39 1290 0794 0102 8494"), "Valid IBAN - BA");
614     ok( method( "BG80 BNBG 9661 1020 3456 78"), "Valid IBAN - BG");
615     ok( method( "HR12 1001 0051 8630 0016 0"), "Valid IBAN - HR");
616     ok( method( "CH93 0076 2011 6238 5295 7"), "Valid IBAN - CH");
617     ok( method( "CY17 0020 0128 0000 0012 0052 7600"), "Valid IBAN - CY");
618     ok( method( "CZ65 0800 0000 1920 0014 5399"), "Valid IBAN - CZ");
619     ok( method( "DK50 0040 0440 1162 43"), "Valid IBAN - DK");
620     ok( method( "EE38 2200 2210 2014 5685"), "Valid IBAN - EE");
621     ok( method( "FO97 5432 0388 8999 44"), "Valid IBAN - FO");
622     ok( method( "FI21 1234 5600 0007 85"), "Valid IBAN - FI");
623     ok( method( "FR14 2004 1010 0505 0001 3M02 606"), "Valid IBAN - FR");
624     ok( method( "GE29 NB00 0000 0101 9049 17"), "Valid IBAN - GE");
625     ok( method( "DE89 3704 0044 0532 0130 00"), "Valid IBAN - DE");
626     ok( method( "GI75 NWBK 0000 0000 7099 453"), "Valid IBAN - GI");
627     ok( method( "GR16 0110 1250 0000 0001 2300 695"), "Valid IBAN - GR");
628     ok( method( "GL56 0444 9876 5432 10"), "Valid IBAN - GL");
629     ok( method( "HU42 1177 3016 1111 1018 0000 0000"), "Valid IBAN - HU");
630     ok( method( "IS14 0159 2600 7654 5510 7303 39"), "Valid IBAN - IS");
631     ok( method( "IE29 AIBK 9311 5212 3456 78"), "Valid IBAN - IE");
632     ok( method( "IL62 0108 0000 0009 9999 999"), "Valid IBAN - IL");
633     ok( method( "IT40 S054 2811 1010 0000 0123 456"), "Valid IBAN - IT");
634     ok( method( "LV80 BANK 0000 4351 9500 1"), "Valid IBAN - LV");
635     ok( method( "LB62 0999 0000 0001 0019 0122 9114"), "Valid IBAN - LB");
636     ok( method( "LI21 0881 0000 2324 013A A"), "Valid IBAN - LI");
637     ok( method( "LT12 1000 0111 0100 1000"), "Valid IBAN - LT");
638     ok( method( "LU28 0019 4006 4475 0000"), "Valid IBAN - LU");
639     ok( method( "MK07 2501 2000 0058 984"), "Valid IBAN - MK");
640     ok( method( "MT84 MALT 0110 0001 2345 MTLC AST0 01S"), "Valid IBAN - MT");
641     ok( method( "MU17 BOMM 0101 1010 3030 0200 000M UR"), "Valid IBAN - MU");
642     ok( method( "MD24 AG00 0225 1000 1310 4168"), "Valid IBAN - MD");
643     ok( method( "MC93 2005 2222 1001 1223 3M44 555"), "Valid IBAN - MC");
644     ok( method( "ME25 5050 0001 2345 6789 51"), "Valid IBAN - ME");
645     ok( method( "NL39 RABO 0300 0652 64"), "Valid IBAN - NL");
646     ok( method( "NO93 8601 1117 947"), "Valid IBAN - NO");
647     ok( method( "PK36 SCBL 0000 0011 2345 6702"), "Valid IBAN - PK");
648     ok( method( "PL60 1020 1026 0000 0422 7020 1111"), "Valid IBAN - PL");
649     ok( method( "PT50 0002 0123 1234 5678 9015 4"), "Valid IBAN - PT");
650     ok( method( "RO49 AAAA 1B31 0075 9384 0000"), "Valid IBAN - RO");
651     ok( method( "SM86 U032 2509 8000 0000 0270 100"), "Valid IBAN - SM");
652     ok( method( "SA03 8000 0000 6080 1016 7519"), "Valid IBAN - SA");
653     ok( method( "RS35 2600 0560 1001 6113 79"), "Valid IBAN - RS");
654     ok( method( "SK31 1200 0000 1987 4263 7541"), "Valid IBAN - SK");
655     ok( method( "SI56 1910 0000 0123 438"), "Valid IBAN - SI");
656     ok( method( "ES80 2310 0001 1800 0001 2345"), "Valid IBAN - ES");
657     ok( method( "SE35 5000 0000 0549 1000 0003"), "Valid IBAN - SE");
658     ok( method( "CH93 0076 2011 6238 5295 7"), "Valid IBAN - CH");
659     ok( method( "TN59 1000 6035 1835 9847 8831"), "Valid IBAN - TN");
660     ok( method( "TR33 0006 1005 1978 6457 8413 26"), "Valid IBAN - TR");
661     ok( method( "AE07 0331 2345 6789 0123 456"), "Valid IBAN - AE");
662     ok( method( "GB29 NWBK 6016 1331 9268 19"), "Valid IBAN - GB");
663 });
664
665 test("postcodeUK", function() {
666     var method = methodTest("postcodeUK");
667     ok( method( "AA9A 9AA" ), "Valid postcode" );
668     ok( method( "A9A 9AA" ), "Valid postcode" );
669     ok( method( "A9 9AA" ), "Valid postcode" );
670     ok( method( "A99 9AA" ), "Valid postcode" );
671     ok( method( "AA9 9AA" ), "Valid postcode" );
672     ok( method( "AA99 9AA" ), "Valid postcode" );
673
674     // Channel Island
675     ok(!method( "AAAA 9AA" ), "Invalid postcode" );
676     ok(!method( "AA-2640" ), "Invalid postcode" );
677
678     ok(!method( "AAA AAA" ), "Invalid postcode" );
679     ok(!method( "AA AAAA" ), "Invalid postcode" );
680     ok(!method( "A AAAA" ), "Invalid postcode" );
681     ok(!method( "AAAAA" ), "Invalid postcode" );
682     ok(!method( "999 999" ), "Invalid postcode" );
683     ok(!method( "99 9999" ), "Invalid postcode" );
684     ok(!method( "9 9999" ), "Invalid postcode" );
685     ok(!method( "99999" ), "Invalid postcode" );
686 });
687
688 test("dateNL", function() {
689     var method = methodTest("dateNL");
690     ok( method( "01-01-1900" ), "Valid date NL" );
691     ok( method( "01.01.1900" ), "Valid date NL" );
692     ok( method( "01/01/1900" ), "Valid date NL" );
693     ok( method( "01-01-00" ), "Valid date NL" );
694     ok( method( "1-01-1900" ), "Valid date NL" );
695     ok( method( "10-10-1900" ), "Valid date NL" );
696     ok(!method( "0-01-1900" ), "Invalid date NL" );
697     ok(!method( "00-01-1900" ), "Invalid date NL" );
698     ok(!method( "35-01-1990" ), "Invalid date NL" );
699     ok(!method( "01.01.190" ), "Invalid date NL" );
700 });
701
702 test("phoneNL", function() {
703     var method = methodTest("phoneNL");
704     ok( method( "0701234567"), "Valid phone NL");
705     ok( method( "0687654321"), "Valid phone NL");
706     ok( method( "020-1234567"), "Valid phone NL");
707     ok( method( "020 - 12 34 567"), "Valid phone NL");
708     ok( method( "010-2345678"), "Valid phone NL");
709     ok( method( "+3120-1234567"), "Valid phone NL");
710     ok( method( "+31(0)10-2345678"), "Valid phone NL");
711     ok(!method( "020-123456"), "Invalid phone NL: too short");
712     ok(!method( "020-12345678"), "Invalid phone NL: too long");
713     ok(!method( "-0201234567"), "Invalid phone NL");
714     ok(!method( "+310201234567"), "Invalid phone NL: no 0 after +31 allowed");
715 });
716
717 test("mobileNL", function() {
718     var method = methodTest("mobileNL");
719     ok( method( "0612345678"), "Valid NL Mobile Number");
720     ok( method( "06-12345678"), "Valid NL Mobile Number");
721     ok( method( "06-12 345 678"), "Valid NL Mobile Number");
722     ok( method( "+316-12345678"), "Valid NL Mobile Number");
723     ok( method( "+31(0)6-12345678"), "Valid NL Mobile Number");
724     ok(!method( "abcdefghij"), "Invalid NL Mobile Number: text");
725     ok(!method( "0123456789"), "Invalid NL Mobile Number: should start with 06");
726     ok(!method( "0823456789"), "Invalid NL Mobile Number: should start with 06");
727     ok(!method( "06-1234567"), "Invalid NL Mobile Number: too short");
728     ok(!method( "06-123456789"), "Invalid NL Mobile Number: too long");
729     ok(!method( "-0612345678"), "Invalid NL Mobile Number");
730     ok(!method( "+310612345678"), "Invalid NL Mobile Number: no 0 after +31 allowed");
731 });
732
733 test("postalcodeNL", function() {
734     var method = methodTest("postalcodeNL");
735     ok( method( "1234AB"), "Valid NL Postal Code");
736     ok( method( "1234ab"), "Valid NL Postal Code");
737     ok( method( "1234 AB"), "Valid NL Postal Code");
738     ok( method( "6789YZ"), "Valid NL Postal Code");
739     ok(!method( "123AA"), "Invalid NL Postal Code: not enough digits");
740     ok(!method( "12345ZZ"), "Invalid NL Postal Code: too many digits");
741     ok(!method( "1234  AA"), "Invalid NL Postal Code: too many spaces");
742     ok(!method( "AA1234"), "Invalid NL Postal Code");
743     ok(!method( "1234-AA"), "Invalid NL Postal Code");
744 });
745
746 test("bankaccountNL", function() {
747     var method = methodTest("bankaccountNL");
748     ok( method( "755490975"), "Valid NL bank account");
749     ok( method( "75 54 90 975"), "Valid NL bank account");
750     ok( method( "123456789"), "Valid NL bank account");
751     ok( method( "12 34 56 789"), "Valid NL bank account");
752     ok(!method( "12 3456789"), "Valid NL bank account: inconsistent spaces");
753     ok(!method( "123 45 67 89"), "Valid NL bank account: incorrect spaces");
754     ok(!method( "755490971"), "Invalid NL bank account");
755     ok(!method( "755490973"), "Invalid NL bank account");
756     ok(!method( "755490979"), "Invalid NL bank account");
757     ok(!method( "123456781"), "Invalid NL bank account");
758     ok(!method( "123456784"), "Invalid NL bank account");
759     ok(!method( "123456788"), "Invalid NL bank account");
760 });
761
762 test("giroaccountNL", function() {
763     var method = methodTest("giroaccountNL");
764     ok( method( "123"), "Valid NL giro  account");
765     ok( method( "1234567"), "Valid NL giro account");
766     ok(!method( "123456788"), "Invalid NL giro account");
767 });
768
769 test("bankorgiroaccountNL", function() {
770     var method = methodTest("bankorgiroaccountNL");
771     ok( method( "123"), "Valid NL giro account");
772     ok( method( "1234567"), "Valid NL giro account");
773     ok( method( "123456789"), "Valid NL bank account");
774     ok(!method( "12345678"), "Invalid NL bank or giro account");
775     ok(!method( "123456788"), "Invalid NL bank or giro account");
776 });
777
778 test("time", function() {
779     var method = methodTest("time");
780     ok( method( "00:00" ), "Valid time, lower bound" );
781     ok( method( "23:59" ), "Valid time, upper bound" );
782     ok(!method( "12" ), "Invalid time" );
783     ok(!method( "29:59" ), "Invalid time" );
784     ok(!method( "00:60" ), "Invalid time" );
785     ok(!method( "24:60" ), "Invalid time" );
786     ok(!method( "24:00" ), "Invalid time" );
787     ok(!method( "30:00" ), "Invalid time" );
788     ok(!method( "29:59" ), "Invalid time" );
789     ok(!method( "120:00" ), "Invalid time" );
790     ok(!method( "12:001" ), "Invalid time" );
791     ok(!method( "12:00a" ), "Invalid time" );
792 });
793
794 test("time12h", function() {
795     var method = methodTest("time12h");
796     ok( method( "12:00 AM" ), "Valid time, lower bound, am" );
797     ok( method( "11:59 AM" ), "Valid time, upper bound, am" );
798     ok( method( "12:00AM" ), "Valid time, no space, am" );
799     ok( method( "12:00PM" ), "Valid time, no space, pm" );
800     ok( method( "12:00 PM" ), "Valid time, lower bound, pm" );
801     ok( method( "11:59 PM" ), "Valid time, upper bound, pm" );
802     ok( method( "11:59 am" ), "Valid time, also accept lowercase" );
803     ok( method( "11:59 pm" ), "Valid time, also accept lowercase" );
804     ok( method( "1:59 pm" ), "Valid time, single hour, no leading 0" );
805     ok( method( "01:59 pm" ), "Valid time, single hour, leading 0" );
806     ok(!method( "12:00" ), "Invalid time" );
807     ok(!method( "9" ), "Invalid time" );
808     ok(!method( "9 am"), "Invalid time" );
809     ok(!method( "12:61 am" ), "Invalid time" );
810     ok(!method( "13:00 am" ), "Invalid time" );
811     ok(!method( "00:00 am" ), "Invalid time" );
812 });
813
814 test("minWords", function() {
815     var method = methodTest("minWords");
816     ok( method( "hello worlds", 2 ), "plain text, valid" );
817     ok( method( "<b>hello</b> world", 2 ), "html, valid" );
818     ok(!method( "hello", 2 ), "plain text, invalid" );
819     ok(!method( "<b>world</b>", 2 ), "html, invalid" );
820     ok(!method( "world <br/>", 2 ), "html, invalid" );
821 });
822
823 test("maxWords", function() {
824     var method = methodTest("maxWords");
825     ok( method( "hello", 2 ), "plain text, valid" );
826     ok( method( "<b>world</b>", 2 ), "html, valid" );
827     ok( method( "world <br/>", 2 ), "html, valid" );
828     ok( method( "hello worlds", 2 ), "plain text, valid" );
829     ok( method( "<b>hello</b> world", 2 ), "html, valid" );
830     ok(!method( "hello 123 world", 2 ), "plain text, invalid" );
831     ok(!method( "<b>hello</b> 123 world", 2 ), "html, invalid" );
832 });
833
834 test("rangeWords", function() {
835     var method = methodTest("rangeWords");
836     ok( method( "hello", [0, 2] ), "plain text, valid" );
837     ok( method( "hello worlds", [0, 2] ), "plain text, valid" );
838     ok( method( "<b>hello</b> world", [0, 2] ), "html, valid" );
839     ok(!method( "hello worlds what is up", [0, 2] ), "plain text, invalid" );
840     ok(!method( "<b>Hello</b> <b>world</b> <b>hello</b>", [0, 2] ), "html, invalid" );
841 });
842
843 test("pattern", function() {
844     var method = methodTest("pattern");
845     ok( method( "AR1004", "AR\\d{4}" ), "Correct format for the given RegExp" );
846     ok( method( "AR1004", /^AR\d{4}$/ ), "Correct format for the given RegExp" );
847     ok(!method( "BR1004", /^AR\d{4}$/ ), "Invalid format for the given RegExp" );
848 });
849
850 function testCardTypeByNumber(number, cardname, expected) {
851     $("#cardnumber").val(number);
852     var actual = $("#ccform").valid();
853     equal(actual, expected, $.format("Expect card number {0} to validate to {1}, actually validated to ", number, expected));
854 }
855
856 test('creditcardtypes, all', function() {
857     $("#ccform").validate({
858         rules: {
859             cardnumber: {
860                 creditcard: true,
861                 creditcardtypes: {
862                     all: true
863                 }
864             }
865         }
866     });
867
868     testCardTypeByNumber( "4111-1111-1111-1111", "VISA", true );
869     testCardTypeByNumber( "5111-1111-1111-1118", "MasterCard", true );
870     testCardTypeByNumber( "6111-1111-1111-1116", "Discover", true );
871     testCardTypeByNumber( "3400-0000-0000-009", "AMEX", true );
872
873     testCardTypeByNumber( "4111-1111-1111-1110", "VISA", false );
874     testCardTypeByNumber( "5432-1111-1111-1111", "MasterCard", false );
875     testCardTypeByNumber( "6611-6611-6611-6611", "Discover", false );
876     testCardTypeByNumber( "3777-7777-7777-7777", "AMEX", false );
877 });
878
879 test('creditcardtypes, visa', function() {
880     $("#ccform").validate({
881         rules: {
882             cardnumber: {
883                 creditcard: true,
884                 creditcardtypes: {
885                     visa: true
886                 }
887             }
888         }
889     });
890
891     testCardTypeByNumber( "4111-1111-1111-1111", "VISA", true );
892     testCardTypeByNumber( "5111-1111-1111-1118", "MasterCard", false );
893     testCardTypeByNumber( "6111-1111-1111-1116", "Discover", false );
894     testCardTypeByNumber( "3400-0000-0000-009", "AMEX", false );
895 });
896
897 test('creditcardtypes, mastercard', function() {
898     $("#ccform").validate({
899         rules: {
900             cardnumber: {
901                 creditcard: true,
902                 creditcardtypes: {
903                     mastercard: true
904                 }
905             }
906         }
907     });
908
909     testCardTypeByNumber( "5111-1111-1111-1118", "MasterCard", true );
910     testCardTypeByNumber( "6111-1111-1111-1116", "Discover", false );
911     testCardTypeByNumber( "3400-0000-0000-009", "AMEX", false );
912     testCardTypeByNumber( "4111-1111-1111-1111", "VISA", false );
913 });
914
915 function fillFormWithValuesAndExpect(formSelector, inputValues, expected) {
916     for (var i=0; i < inputValues.length; i++) {
917         $(formSelector + ' input:eq(' + i + ')').val(inputValues[i]);
918     }
919     var actual = $(formSelector).valid();
920     equal(actual, expected, $.format("Filled inputs of form '{0}' with {1} values ({2})", formSelector, inputValues.length, inputValues.toString()));
921
922 }
923
924 test('require_from_group', function() {
925     $("#productInfo").validate({
926         rules: {
927             partnumber:  {require_from_group: [2,".productInfo"]},
928             description: {require_from_group: [2,".productInfo"]},
929             discount: {require_from_group: [2,".productInfo"]}
930         }
931     });
932
933     fillFormWithValuesAndExpect('#productInfo', [], false);
934     fillFormWithValuesAndExpect('#productInfo', [123], false);
935     $('#productInfo input[type="checkbox"]').attr('checked', 'checked');
936     fillFormWithValuesAndExpect('#productInfo', [123], true);
937     $('#productInfo input[type="checkbox"]').removeAttr('checked');
938     fillFormWithValuesAndExpect('#productInfo', [123, 'widget'], true);
939     fillFormWithValuesAndExpect('#productInfo', [123, 'widget', 'red'], true);
940     fillFormWithValuesAndExpect('#productInfo', [123, 'widget', 'red'], true);
941 });
942
943 test('skip_or_fill_minimum', function() {
944     $("#productInfo").validate({
945         rules: {
946             partnumber:  {skip_or_fill_minimum: [2,".productInfo"]},
947             description: {skip_or_fill_minimum: [2,".productInfo"]},
948             color:       {skip_or_fill_minimum: [2,".productInfo"]}
949         }
950     });
951
952     fillFormWithValuesAndExpect('#productInfo', [], true);
953     fillFormWithValuesAndExpect('#productInfo', [123], false);
954     fillFormWithValuesAndExpect('#productInfo', [123, 'widget'], true);
955     fillFormWithValuesAndExpect('#productInfo', [123, 'widget', 'red'], true);
956 });
957
958 test("zipcodeUS", function() {
959     var method = methodTest("zipcodeUS");
960     ok( method( "12345" ), "Valid zip" );
961     ok( method( "12345-2345" ), "Valid zip" );
962     ok(!method( "1" ), "Invalid zip" );
963     ok(!method( "1234" ), "Invalid zip" );
964     ok(!method( "123-23" ), "Invalid zip" );
965     ok(!method( "12345-43" ), "Invalid zip" );
966 });
967
968 })(jQuery);