methods.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. function methodTest( methodName ) {
  2. var v = jQuery( "#form" ).validate(),
  3. method = $.validator.methods[ methodName ],
  4. element = $( "#firstname" )[ 0 ];
  5. return function( value, param ) {
  6. element.value = value;
  7. return method.call( v, value, element, param );
  8. };
  9. }
  10. /**
  11. * Creates a dummy DOM file input with FileList
  12. * @param filename
  13. * @param mimeType
  14. * @returns {{}}
  15. */
  16. function acceptFileDummyInput( filename, mimeType ) {
  17. function dummy() {
  18. return file;
  19. }
  20. // https://developer.mozilla.org/en-US/docs/Web/API/FileList
  21. var file = {
  22. name: filename,
  23. size: 500001,
  24. type: mimeType
  25. },
  26. fileList = {
  27. 0: file,
  28. length: 1,
  29. item: dummy
  30. };
  31. return {
  32. type: "file",
  33. files: fileList,
  34. nodeName: "INPUT",
  35. value: "/tmp/fake_value",
  36. hasAttribute: function() { return false; }
  37. };
  38. }
  39. QUnit.module( "methods" );
  40. QUnit.test( "default messages", function( assert ) {
  41. var m = $.validator.methods;
  42. $.each( m, function( key ) {
  43. assert.ok( jQuery.validator.messages[ key ], key + " has a default message." );
  44. } );
  45. } );
  46. QUnit.test( "digit", function( assert ) {
  47. var method = methodTest( "digits" );
  48. assert.ok( method( "123" ), "Valid digits" );
  49. assert.ok( !method( "123.000" ), "Invalid digits" );
  50. assert.ok( !method( "123.000,00" ), "Invalid digits" );
  51. assert.ok( !method( "123.0.0,0" ), "Invalid digits" );
  52. assert.ok( !method( "x123" ), "Invalid digits" );
  53. assert.ok( !method( "100.100,0,0" ), "Invalid digits" );
  54. } );
  55. QUnit.test( "url", function( assert ) {
  56. var method = methodTest( "url" );
  57. assert.ok( method( "http://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
  58. assert.ok( method( "https://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
  59. assert.ok( method( "ftp://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
  60. assert.ok( method( "http://www.føtex.dk/" ), "Valid url, danish unicode characters" );
  61. assert.ok( method( "http://bösendorfer.de/" ), "Valid url, german unicode characters" );
  62. assert.ok( method( "http://142.42.1.1" ), "Valid IP Address" );
  63. assert.ok( method( "http://pro.photography" ), "Valid long TLD" );
  64. assert.ok( method( "//code.jquery.com/jquery-1.11.3.min.js" ), "Valid protocol-relative url" );
  65. assert.ok( method( "//142.42.1.1" ), "Valid protocol-relative IP Address" );
  66. assert.ok( !method( "htp://code.jquery.com/jquery-1.11.3.min.js" ), "Invalid protocol" );
  67. assert.ok( !method( "http://192.168.8." ), "Invalid IP Address" );
  68. assert.ok( !method( "http://bassistance" ), "Invalid url" ); // Valid
  69. assert.ok( !method( "http://bassistance." ), "Invalid url" ); // Valid
  70. assert.ok( !method( "http://bassistance,de" ), "Invalid url" );
  71. assert.ok( !method( "http://bassistance;de" ), "Invalid url" );
  72. assert.ok( !method( "http://.bassistancede" ), "Invalid url" );
  73. assert.ok( !method( "bassistance.de" ), "Invalid url" );
  74. } );
  75. QUnit.test( "url2 (tld optional)", function( assert ) {
  76. var method = methodTest( "url2" );
  77. assert.ok( method( "http://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
  78. assert.ok( method( "https://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
  79. assert.ok( method( "ftp://bassistance.de/jquery/plugin.php?bla=blu" ), "Valid url" );
  80. assert.ok( method( "http://www.føtex.dk/" ), "Valid url, danish unicode characters" );
  81. assert.ok( method( "http://bösendorfer.de/" ), "Valid url, german unicode characters" );
  82. assert.ok( method( "http://192.168.8.5" ), "Valid IP Address" );
  83. assert.ok( !method( "http://192.168.8." ), "Invalid IP Address" );
  84. assert.ok( method( "http://bassistance" ), "Invalid url" );
  85. assert.ok( method( "http://bassistance." ), "Invalid url" );
  86. assert.ok( !method( "http://bassistance,de" ), "Invalid url" );
  87. assert.ok( !method( "http://bassistance;de" ), "Invalid url" );
  88. assert.ok( !method( "http://.bassistancede" ), "Invalid url" );
  89. assert.ok( !method( "bassistance.de" ), "Invalid url" );
  90. } );
  91. QUnit.test( "email", function( assert ) {
  92. var method = methodTest( "email" );
  93. assert.ok( method( "name@domain.tld" ), "Valid email" );
  94. assert.ok( method( "name@domain.tl" ), "Valid email" );
  95. assert.ok( method( "bart+bart@tokbox.com" ), "Valid email" );
  96. assert.ok( method( "bart+bart@tokbox.travel" ), "Valid email" );
  97. assert.ok( method( "n@d.tld" ), "Valid email" );
  98. assert.ok( method( "bla.blu@g.mail.com" ), "Valid email" );
  99. assert.ok( method( "name@domain" ), "Valid email" );
  100. assert.ok( method( "name.@domain.tld" ), "Valid email" );
  101. assert.ok( method( "name@website.a" ), "Valid email" );
  102. assert.ok( method( "name@pro.photography" ), "Valid email" );
  103. assert.ok( !method( "ole@føtex.dk" ), "Invalid email" );
  104. assert.ok( !method( "jörn@bassistance.de" ), "Invalid email" );
  105. assert.ok( !method( "name" ), "Invalid email" );
  106. assert.ok( !method( "test@test-.com" ), "Invalid email" );
  107. assert.ok( !method( "name@" ), "Invalid email" );
  108. assert.ok( !method( "name,@domain.tld" ), "Invalid email" );
  109. assert.ok( !method( "name;@domain.tld" ), "Invalid email" );
  110. assert.ok( !method( "name;@domain.tld." ), "Invalid email" );
  111. } );
  112. QUnit.test( "number", function( assert ) {
  113. var method = methodTest( "number" );
  114. assert.ok( method( "123" ), "Valid number" );
  115. assert.ok( method( "-123" ), "Valid number" );
  116. assert.ok( method( "123,000" ), "Valid number" );
  117. assert.ok( method( "-123,000" ), "Valid number" );
  118. assert.ok( method( "123,000.00" ), "Valid number" );
  119. assert.ok( method( "-123,000.00" ), "Valid number" );
  120. assert.ok( !method( "-" ), "Invalid number" );
  121. assert.ok( !method( "123.000,00" ), "Invalid number" );
  122. assert.ok( !method( "123.0.0,0" ), "Invalid number" );
  123. assert.ok( !method( "x123" ), "Invalid number" );
  124. assert.ok( !method( "100.100,0,0" ), "Invalid number" );
  125. assert.ok( method( "" ), "Blank is valid" );
  126. assert.ok( method( "123" ), "Valid decimal" );
  127. assert.ok( method( "123000" ), "Valid decimal" );
  128. assert.ok( method( "123000.12" ), "Valid decimal" );
  129. assert.ok( method( "-123000.12" ), "Valid decimal" );
  130. assert.ok( method( "123.000" ), "Valid decimal" );
  131. assert.ok( method( "123,000.00" ), "Valid decimal" );
  132. assert.ok( method( "-123,000.00" ), "Valid decimal" );
  133. assert.ok( method( ".100" ), "Valid decimal" );
  134. assert.ok( !method( "1230,000.00" ), "Invalid decimal" );
  135. assert.ok( !method( "123.0.0,0" ), "Invalid decimal" );
  136. assert.ok( !method( "x123" ), "Invalid decimal" );
  137. assert.ok( !method( "100.100,0,0" ), "Invalid decimal" );
  138. } );
  139. /* Disabled for now, need to figure out how to test localized methods
  140. QUnit.test("numberDE", function( assert ) {
  141. var method = methodTest("numberDE");
  142. assert.ok( method( "123" ), "Valid numberDE" );
  143. assert.ok( method( "-123" ), "Valid numberDE" );
  144. assert.ok( method( "123.000" ), "Valid numberDE" );
  145. assert.ok( method( "-123.000" ), "Valid numberDE" );
  146. assert.ok( method( "123.000,00" ), "Valid numberDE" );
  147. assert.ok( method( "-123.000,00" ), "Valid numberDE" );
  148. assert.ok(!method( "123,000.00" ), "Invalid numberDE" );
  149. assert.ok(!method( "123,0,0.0" ), "Invalid numberDE" );
  150. assert.ok(!method( "x123" ), "Invalid numberDE" );
  151. assert.ok(!method( "100,100.0.0" ), "Invalid numberDE" );
  152. assert.ok( method( "" ), "Blank is valid" );
  153. assert.ok( method( "123" ), "Valid decimalDE" );
  154. assert.ok( method( "123000" ), "Valid decimalDE" );
  155. assert.ok( method( "123000,12" ), "Valid decimalDE" );
  156. assert.ok( method( "-123000,12" ), "Valid decimalDE" );
  157. assert.ok( method( "123.000" ), "Valid decimalDE" );
  158. assert.ok( method( "123.000,00" ), "Valid decimalDE" );
  159. assert.ok( method( "-123.000,00" ), "Valid decimalDE" )
  160. assert.ok(!method( "123.0.0,0" ), "Invalid decimalDE" );
  161. assert.ok(!method( "x123" ), "Invalid decimalDE" );
  162. assert.ok(!method( "100,100.0.0" ), "Invalid decimalDE" );
  163. });
  164. */
  165. QUnit.test( "date", function( assert ) {
  166. var method = methodTest( "date" );
  167. assert.ok( method( "06/06/1990" ), "Valid date" );
  168. assert.ok( method( "6/6/06" ), "Valid date" );
  169. assert.ok( !method( "1990x-06-06" ), "Invalid date" );
  170. } );
  171. QUnit.test( "dateISO", function( assert ) {
  172. var method = methodTest( "dateISO" );
  173. assert.ok( method( "1990-06-06" ), "Valid date" );
  174. assert.ok( method( "1990-01-01" ), "Valid date" );
  175. assert.ok( method( "1990-01-31" ), "Valid date" );
  176. assert.ok( method( "1990-12-01" ), "Valid date" );
  177. assert.ok( method( "1990-12-31" ), "Valid date" );
  178. assert.ok( method( "1990/06/06" ), "Valid date" );
  179. assert.ok( method( "1990-6-6" ), "Valid date" );
  180. assert.ok( method( "1990/6/6" ), "Valid date" );
  181. assert.ok( !method( "1990-106-06" ), "Invalid date" );
  182. assert.ok( !method( "190-06-06" ), "Invalid date" );
  183. assert.ok( !method( "1990-00-06" ), "Invalid date" );
  184. assert.ok( !method( "1990-13-01" ), "Invalid date" );
  185. assert.ok( !method( "1990-01-00" ), "Invalid date" );
  186. assert.ok( !method( "1990-01-32" ), "Invalid date" );
  187. assert.ok( !method( "1990-13-32" ), "Invalid date" );
  188. } );
  189. /* Disabled for now, need to figure out how to test localized methods
  190. QUnit.test("dateDE", function( assert ) {
  191. var method = methodTest("dateDE");
  192. assert.ok( method( "03.06.1984" ), "Valid dateDE" );
  193. assert.ok( method( "3.6.84" ), "Valid dateDE" );
  194. assert.ok(!method( "6-6-06" ), "Invalid dateDE" );
  195. assert.ok(!method( "1990-06-06" ), "Invalid dateDE" );
  196. assert.ok(!method( "06/06/1990" ), "Invalid dateDE" );
  197. assert.ok(!method( "6/6/06" ), "Invalid dateDE" );
  198. });
  199. */
  200. QUnit.test( "required", function( assert ) {
  201. var v = jQuery( "#form" ).validate(),
  202. method = $.validator.methods.required,
  203. e = $( "#text1, #text1b, #hidden2, #select1, #select2" );
  204. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ] ), "Valid text input" );
  205. assert.ok( !method.call( v, e[ 1 ].value, e[ 1 ] ), "Invalid text input" );
  206. assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ] ), "Invalid text input" );
  207. assert.ok( !method.call( v, e[ 3 ].value, e[ 3 ] ), "Invalid select" );
  208. assert.ok( method.call( v, e[ 4 ].value, e[ 4 ] ), "Valid select" );
  209. e = $( "#area1, #area2, #pw1, #pw2" );
  210. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ] ), "Valid textarea" );
  211. assert.ok( !method.call( v, e[ 1 ].value, e[ 1 ] ), "Invalid textarea" );
  212. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ] ), "Valid password input" );
  213. assert.ok( !method.call( v, e[ 3 ].value, e[ 3 ] ), "Invalid password input" );
  214. e = $( "#radio1, #radio2, #radio3" );
  215. assert.ok( !method.call( v, e[ 0 ].value, e[ 0 ] ), "Invalid radio" );
  216. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ] ), "Valid radio" );
  217. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ] ), "Valid radio" );
  218. e = $( "#check1, #check2" );
  219. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ] ), "Valid checkbox" );
  220. assert.ok( !method.call( v, e[ 1 ].value, e[ 1 ] ), "Invalid checkbox" );
  221. e = $( "#select1, #select2, #select3, #select4" );
  222. assert.ok( !method.call( v, e[ 0 ].value, e[ 0 ] ), "Invalid select" );
  223. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ] ), "Valid select" );
  224. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ] ), "Valid select" );
  225. assert.ok( method.call( v, e[ 3 ].value, e[ 3 ] ), "Valid select" );
  226. } );
  227. QUnit.test( "required with dependencies", function( assert ) {
  228. var v = jQuery( "#form" ).validate(),
  229. method = $.validator.methods.required,
  230. e = $( "#hidden2, #select1, #area2, #radio1, #check2" );
  231. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], "asffsaa" ), "Valid text input due to dependency not met" );
  232. assert.ok( !method.call( v, e[ 0 ].value, e[ 0 ], "input" ), "Invalid text input" );
  233. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], function() { return false; } ), "Valid text input due to dependency not met" );
  234. assert.ok( !method.call( v, e[ 0 ].value, e[ 0 ], function() { return true; } ), "Invalid text input" );
  235. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], "asfsfa" ), "Valid select due to dependency not met" );
  236. assert.ok( !method.call( v, e[ 1 ].value, e[ 1 ], "input" ), "Invalid select" );
  237. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ], "asfsafsfa" ), "Valid textarea due to dependency not met" );
  238. assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ], "input" ), "Invalid textarea" );
  239. assert.ok( method.call( v, e[ 3 ].value, e[ 3 ], "asfsafsfa" ), "Valid radio due to dependency not met" );
  240. assert.ok( !method.call( v, e[ 3 ].value, e[ 3 ], "input" ), "Invalid radio" );
  241. assert.ok( method.call( v, e[ 4 ].value, e[ 4 ], "asfsafsfa" ), "Valid checkbox due to dependency not met" );
  242. assert.ok( !method.call( v, e[ 4 ].value, e[ 4 ], "input" ), "Invalid checkbox" );
  243. } );
  244. QUnit.test( "minlength", function( assert ) {
  245. var v = jQuery( "#form" ).validate(),
  246. method = $.validator.methods.minlength,
  247. param = 2,
  248. e = $( "#text1, #text1c, #text2, #text3" );
  249. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid text input" );
  250. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Valid text input" );
  251. assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ], param ), "Invalid text input" );
  252. assert.ok( method.call( v, e[ 3 ].value, e[ 3 ], param ), "Valid text input" );
  253. e = $( "#check1, #check2, #check3" );
  254. assert.ok( !method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid checkbox" );
  255. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Valid checkbox" );
  256. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ], param ), "Invalid checkbox" );
  257. e = $( "#select1, #select2, #select3, #select4, #select5" );
  258. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid select " + e[ 0 ].id );
  259. assert.ok( !method.call( v, e[ 1 ].value, e[ 1 ], param ), "Invalid select " + e[ 1 ].id );
  260. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ], param ), "Valid select " + e[ 2 ].id );
  261. assert.ok( method.call( v, e[ 3 ].value, e[ 3 ], param ), "Valid select " + e[ 3 ].id );
  262. assert.ok( method.call( v, e[ 4 ].value, e[ 4 ], param ), "Valid select " + e[ 4 ].id );
  263. } );
  264. QUnit.test( "maxlength", function( assert ) {
  265. var v = jQuery( "#form" ).validate(),
  266. method = $.validator.methods.maxlength,
  267. param = 4,
  268. e = $( "#text1, #text2, #text3" );
  269. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid text input" );
  270. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Valid text input" );
  271. assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ], param ), "Invalid text input" );
  272. e = $( "#check1, #check2, #check3" );
  273. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid checkbox" );
  274. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Invalid checkbox" );
  275. assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ], param ), "Invalid checkbox" );
  276. e = $( "#select1, #select2, #select3, #select4" );
  277. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid select" );
  278. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Valid select" );
  279. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ], param ), "Valid select" );
  280. assert.ok( !method.call( v, e[ 3 ].value, e[ 3 ], param ), "Invalid select" );
  281. } );
  282. QUnit.test( "rangelength", function( assert ) {
  283. var v = jQuery( "#form" ).validate(),
  284. method = $.validator.methods.rangelength,
  285. param = [ 2, 4 ],
  286. e = $( "#text1, #text2, #text3" );
  287. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid text input" );
  288. assert.ok( !method.call( v, e[ 1 ].value, e[ 1 ], param ), "Invalid text input" );
  289. assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ], param ), "Invalid text input" );
  290. } );
  291. QUnit.test( "min", function( assert ) {
  292. var v = jQuery( "#form" ).validate(),
  293. method = $.validator.methods.min,
  294. param = 8,
  295. e = $( "#value1, #value2, #value3" );
  296. assert.ok( !method.call( v, e[ 0 ].value, e[ 0 ], param ), "Invalid text input" );
  297. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Valid text input" );
  298. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ], param ), "Valid text input" );
  299. } );
  300. QUnit.test( "max", function( assert ) {
  301. var v = jQuery( "#form" ).validate(),
  302. method = $.validator.methods.max,
  303. param = 12,
  304. e = $( "#value1, #value2, #value3" );
  305. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid text input" );
  306. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Valid text input" );
  307. assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ], param ), "Invalid text input" );
  308. } );
  309. QUnit.test( "range", function( assert ) {
  310. var v = jQuery( "#form" ).validate(),
  311. method = $.validator.methods.range,
  312. param = [ 4, 12 ],
  313. e = $( "#value1, #value2, #value3" );
  314. assert.ok( !method.call( v, e[ 0 ].value, e[ 0 ], param ), "Invalid text input" );
  315. assert.ok( method.call( v, e[ 1 ].value, e[ 1 ], param ), "Valid text input" );
  316. assert.ok( !method.call( v, e[ 2 ].value, e[ 2 ], param ), "Invalid text input" );
  317. } );
  318. QUnit.test( "step", function( assert ) {
  319. var v = jQuery( "#form" ).validate(),
  320. method = $.validator.methods.step,
  321. param = 1000,
  322. e = $( "#value1, #value2, #value3, #value4" );
  323. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Valid text input" );
  324. assert.ok( !method.call( v, e[ 1 ].value, e[ 1 ], param ), "Invalid text input" );
  325. assert.ok( method.call( v, e[ 2 ].value, e[ 2 ], param ), "Valid text input" );
  326. } );
  327. QUnit.test( "#1760 - step modulo/remainder regression tests", function( assert ) {
  328. var v = jQuery( "#form" ).validate(),
  329. method = $.validator.methods.step,
  330. param = 0.00125,
  331. e = $( "#value4" );
  332. for ( var i = 1; i <= 1000; i++ ) {
  333. e[ 0 ].value = ( param * 100000 * i ) / 100000;
  334. assert.ok( method.call( v, e[ 0 ].value, e[ 0 ], param ), "Ensure " + e[ 0 ].value + " % " + param + " === 0 is valid" );
  335. }
  336. } );
  337. QUnit.test( "equalTo", function( assert ) {
  338. var v = jQuery( "#form" ).validate(),
  339. method = $.validator.methods.equalTo,
  340. e = $( "#text1, #text2" );
  341. assert.ok( method.call( v, "Test", e[ 0 ], "#text1" ), "Text input" );
  342. assert.ok( method.call( v, "T", e[ 1 ], "#text2" ), "Another one" );
  343. } );
  344. QUnit.test( "extension", function( assert ) {
  345. var method = methodTest( "extension" ),
  346. v;
  347. assert.ok( method( "picture.gif" ), "Valid default accept type" );
  348. assert.ok( method( "picture.jpg" ), "Valid default accept type" );
  349. assert.ok( method( "picture.jpeg" ), "Valid default accept type" );
  350. assert.ok( method( "picture.png" ), "Valid default accept type" );
  351. assert.ok( !method( "picture.pgn" ), "Invalid default accept type" );
  352. v = jQuery( "#form" ).validate();
  353. method = function( value, param ) {
  354. return $.validator.methods.extension.call( v, value, $( "#text1" )[ 0 ], param );
  355. };
  356. assert.ok( method( "picture.doc", "doc" ), "Valid custom accept type" );
  357. assert.ok( method( "picture.pdf", "doc|pdf" ), "Valid custom accept type" );
  358. assert.ok( method( "picture.pdf", "pdf|doc" ), "Valid custom accept type" );
  359. assert.ok( !method( "picture.pdf", "doc" ), "Invalid custom accept type" );
  360. assert.ok( !method( "picture.doc", "pdf" ), "Invalid custom accept type" );
  361. assert.ok( method( "picture.pdf", "doc,pdf" ), "Valid custom accept type, comma separated" );
  362. assert.ok( method( "picture.pdf", "pdf,doc" ), "Valid custom accept type, comma separated" );
  363. assert.ok( !method( "picture.pdf", "gop,top" ), "Invalid custom accept type, comma separated" );
  364. } );
  365. QUnit.test( "remote", function( assert ) {
  366. assert.expect( 7 );
  367. var e = $( "#username" ),
  368. v = $( "#userForm" ).validate( {
  369. rules: {
  370. username: {
  371. required: true,
  372. remote: "users.php"
  373. }
  374. },
  375. messages: {
  376. username: {
  377. required: "Please",
  378. remote: jQuery.validator.format( "{0} in use" )
  379. }
  380. },
  381. submitHandler: function() {
  382. assert.ok( false, "submitHandler may never be called when validating only elements" );
  383. }
  384. } ),
  385. done = assert.async();
  386. $( document ).ajaxStop( function() {
  387. $( document ).unbind( "ajaxStop" );
  388. assert.equal( v.size(), 1, "There must be one error" );
  389. assert.equal( v.errorList[ 0 ].message, "Peter in use" );
  390. $( document ).ajaxStop( function() {
  391. $( document ).unbind( "ajaxStop" );
  392. assert.equal( v.size(), 1, "There must be one error" );
  393. assert.equal( v.errorList[ 0 ].message, "Peter2 in use" );
  394. done();
  395. } );
  396. e.val( "Peter2" );
  397. assert.strictEqual( v.element( e ), true, "new value, new request; dependency-mismatch considered as valid though" );
  398. } );
  399. assert.strictEqual( v.element( e ), false, "invalid element, nothing entered yet" );
  400. e.val( "Peter" );
  401. assert.strictEqual( v.element( e ), true, "still invalid, because remote validation must block until it returns; dependency-mismatch considered as valid though" );
  402. } );
  403. QUnit.test( "remote, pending class added to element while call outstanding", function( assert ) {
  404. assert.expect( 3 );
  405. var e = $( "#username" ),
  406. done = assert.async(),
  407. v = $( "#userForm" ).validate( {
  408. rules: {
  409. username: {
  410. remote: {
  411. url: "users.php",
  412. complete: function() {
  413. assert.strictEqual( e.hasClass( "pending" ), false, "not pending since ajax call complete" );
  414. done();
  415. }
  416. }
  417. }
  418. }
  419. } );
  420. assert.strictEqual( e.hasClass( "pending" ), false, "not pending since no data entered" );
  421. e.val( "Peter" );
  422. // This fires off the validation:
  423. v.element( e );
  424. assert.strictEqual( e.hasClass( "pending" ), true, "pending while validation outstanding" );
  425. } );
  426. QUnit.test( "remote, customized ajax options", function( assert ) {
  427. assert.expect( 2 );
  428. var done = assert.async();
  429. $( "#userForm" ).validate( {
  430. rules: {
  431. username: {
  432. required: true,
  433. remote: {
  434. url: "users.php",
  435. type: "POST",
  436. beforeSend: function( request, settings ) {
  437. assert.deepEqual( settings.type, "POST" );
  438. assert.deepEqual( settings.data, "username=asdf&email=email.com" );
  439. },
  440. data: {
  441. email: function() {
  442. return "email.com";
  443. }
  444. },
  445. complete: function() {
  446. done();
  447. }
  448. }
  449. }
  450. }
  451. } );
  452. $( "#username" ).val( "asdf" );
  453. $( "#userForm" ).valid();
  454. } );
  455. QUnit.test( "remote extensions", function( assert ) {
  456. assert.expect( 5 );
  457. var e = $( "#username" ),
  458. v = $( "#userForm" ).validate( {
  459. rules: {
  460. username: {
  461. required: true,
  462. remote: "users2.php"
  463. }
  464. },
  465. messages: {
  466. username: {
  467. required: "Please"
  468. }
  469. },
  470. submitHandler: function() {
  471. assert.ok( false, "submitHandler may never be called when validating only elements" );
  472. }
  473. } ),
  474. done = assert.async();
  475. $( document ).ajaxStop( function() {
  476. $( document ).unbind( "ajaxStop" );
  477. if ( v.size() !== 0 ) {
  478. assert.ok( "There must be one error" );
  479. assert.equal( v.errorList[ 0 ].message, "asdf is already taken, please try something else" );
  480. v.element( e );
  481. assert.equal( v.errorList[ 0 ].message, "asdf is already taken, please try something else", "message doesn't change on revalidation" );
  482. }
  483. done();
  484. } );
  485. assert.strictEqual( v.element( e ), false, "invalid element, nothing entered yet" );
  486. e.val( "asdf" );
  487. assert.strictEqual( v.element( e ), true, "still invalid, because remote validation must block until it returns; dependency-mismatch considered as valid though" );
  488. } );
  489. QUnit.test( "remote, data previous querystring", function( assert ) {
  490. assert.expect( 4 );
  491. var succeeded = 0,
  492. $f = $( "#firstnamec" ),
  493. $l = $( "#lastnamec" ),
  494. done1 = assert.async(),
  495. done2 = assert.async(),
  496. done3 = assert.async(),
  497. v = $( "#testForm1clean" ).validate( {
  498. rules: {
  499. lastname: {
  500. remote: {
  501. url: "users.php",
  502. type: "POST",
  503. data: {
  504. firstname: function() {
  505. return $f.val();
  506. }
  507. },
  508. complete: function() {
  509. succeeded++;
  510. }
  511. }
  512. }
  513. }
  514. } );
  515. $f.val( "first-name" );
  516. $l.val( "last-name" );
  517. assert.strictEqual( succeeded, 0, "no valid call means no successful validation" );
  518. v.element( $l );
  519. setTimeout( function() {
  520. assert.strictEqual( succeeded, 1, "first valid check should submit given first name" );
  521. done1();
  522. v.element( $l );
  523. setTimeout( function() {
  524. assert.strictEqual( succeeded, 1, "second valid check should not resubmit given same first name" );
  525. done2();
  526. $f.val( "different-first-name" );
  527. v.element( $l );
  528. setTimeout( function() {
  529. assert.strictEqual( succeeded, 2, "third valid check should resubmit given different first name" );
  530. done3();
  531. } );
  532. } );
  533. } );
  534. } );
  535. QUnit.test( "remote, highlight all invalid fields", function( assert ) {
  536. assert.expect( 3 );
  537. var done = assert.async(),
  538. $form = $( "#testForm1" ),
  539. $firstnameField = $form.find( "input[name='firstname']" ),
  540. $lastnameField = $form.find( "input[name='lastname']" ),
  541. $somethingField = $form.find( "input[name='something']" ),
  542. validateOptions = {
  543. rules: {
  544. firstname: {
  545. required: true
  546. },
  547. lastname: {
  548. required: true
  549. },
  550. something: {
  551. required: true,
  552. remote: {
  553. url: "response.php",
  554. type: "post",
  555. data: {
  556. responseText: "false"
  557. }
  558. }
  559. }
  560. }
  561. };
  562. $firstnameField.val( "" );
  563. $lastnameField.val( "" );
  564. $somethingField.val( "something value" );
  565. $form.validate( validateOptions );
  566. $form.valid();
  567. setTimeout( function() {
  568. assert.equal( $firstnameField.hasClass( "error" ), true, "Field 'firstname' should have a '.error' class" );
  569. assert.equal( $lastnameField.hasClass( "error" ), true, "Field 'lastname' should have a '.error' class" );
  570. assert.equal( $somethingField.hasClass( "error" ), true, "Field 'something' should have a '.error' class" );
  571. done();
  572. }, 500 );
  573. } );
  574. QUnit.test( "remote, unhighlighted should be invoked after being highlighted/invalid", function( assert ) {
  575. assert.expect( 6 );
  576. var done1 = assert.async(),
  577. done2 = assert.async(),
  578. $form = $( "#testForm25" ),
  579. $somethingField = $form.find( "input[name='something25']" ),
  580. responseText = "false",
  581. response = function() { return responseText; },
  582. validateOptions = {
  583. highlight: function( e ) {
  584. $( e ).addClass( "error" );
  585. assert.ok( true, "highlight should be called" );
  586. },
  587. unhighlight: function( e ) {
  588. $( e ).removeClass( "error" );
  589. assert.ok( true, "unhighlight should be called" );
  590. },
  591. rules: {
  592. something25: {
  593. required: true,
  594. remote: {
  595. url: "response.php",
  596. type: "post",
  597. data: {
  598. responseText: response
  599. },
  600. async: false
  601. }
  602. }
  603. }
  604. };
  605. $somethingField.val( "something value" );
  606. var v = $form.validate( validateOptions );
  607. v.element( $somethingField );
  608. setTimeout( function() {
  609. assert.equal( $somethingField.hasClass( "error" ), true, "Field 'something' should have the error class" );
  610. done1();
  611. $somethingField.val( "something value 2" );
  612. responseText = "true";
  613. v.element( $somethingField );
  614. setTimeout( function() {
  615. assert.equal( $somethingField.hasClass( "error" ), false, "Field 'something' should not have the error class" );
  616. done2();
  617. }, 500 );
  618. }, 500 );
  619. } );
  620. QUnit.test( "Fix #697: remote validation uses wrong error messages", function( assert ) {
  621. var e = $( "#username" ),
  622. done1 = assert.async(),
  623. done2 = assert.async(),
  624. done3 = assert.async(),
  625. v = $( "#userForm" ).validate( {
  626. rules: {
  627. username: {
  628. required: true,
  629. remote: {
  630. url: "users.php"
  631. }
  632. }
  633. },
  634. messages: {
  635. username: {
  636. remote: $.validator.format( "{0} in use" )
  637. }
  638. }
  639. } );
  640. $( "#userForm" ).valid();
  641. e.val( "Peter" );
  642. v.element( e );
  643. setTimeout( function() {
  644. assert.equal( v.errorList[ 0 ].message, "Peter in use" );
  645. done1();
  646. e.val( "something" );
  647. v.element( e );
  648. e.val( "Peter" );
  649. v.element( e );
  650. setTimeout( function() {
  651. assert.equal( v.errorList[ 0 ].message, "Peter in use" );
  652. done2();
  653. e.val( "asdf" );
  654. v.element( e );
  655. setTimeout( function() {
  656. assert.equal( v.errorList[ 0 ].message, "asdf in use", "error message should be updated" );
  657. done3();
  658. } );
  659. } );
  660. } );
  661. } );
  662. QUnit.module( "additional methods" );
  663. QUnit.test( "phone (us)", function( assert ) {
  664. var method = methodTest( "phoneUS" );
  665. assert.ok( method( "1(212)-999-2345" ), "Valid US phone number" );
  666. assert.ok( method( "212 999 2344" ), "Valid US phone number" );
  667. assert.ok( method( "212-999-0983" ), "Valid US phone number" );
  668. assert.ok( !method( "111-123-5434" ), "Invalid US phone number. Area Code cannot start with 1" );
  669. assert.ok( !method( "212 123 4567" ), "Invalid US phone number. NXX cannot start with 1" );
  670. assert.ok( !method( "234-911-5678" ), "Invalid US phone number, because the exchange code cannot be in the form N11" );
  671. assert.ok( !method( "911-333-5678" ), "Invalid US phone number, because the area code cannot be in the form N11" );
  672. assert.ok( method( "234-912-5678" ), "Valid US phone number" );
  673. } );
  674. QUnit.test( "phoneUK", function( assert ) {
  675. var method = methodTest( "phoneUK" );
  676. assert.ok( method( "0117 333 5555" ), "Valid UK Phone Number" );
  677. assert.ok( method( "0121 555 5555" ), "Valid UK Phone Number" );
  678. assert.ok( method( "01633 555555" ), "Valid UK Phone Number" );
  679. assert.ok( method( "01298 28555" ), "Valid UK Phone Number" );
  680. assert.ok( method( "015395 55555" ), "Valid UK Phone Number" );
  681. assert.ok( method( "016977 3999" ), "Valid UK Phone Number" );
  682. assert.ok( method( "020 3000 5555" ), "Valid UK Phone Number" );
  683. assert.ok( method( "024 7500 5555" ), "Valid UK Phone Number" );
  684. assert.ok( method( "0333 555 5555" ), "Valid UK Phone Number" );
  685. assert.ok( method( "0500 555555" ), "Valid UK Phone Number" );
  686. assert.ok( method( "055 3555 5555" ), "Valid UK Phone Number" );
  687. assert.ok( method( "07122 555555" ), "Valid UK Phone Number" );
  688. assert.ok( method( "07222 555555" ), "Valid UK Phone Number" );
  689. assert.ok( method( "07322 555555" ), "Valid UK Phone Number" );
  690. assert.ok( method( "0800 555 5555" ), "Valid UK Phone Number" );
  691. assert.ok( method( "0800 355555" ), "Valid UK Phone Number" );
  692. assert.ok( method( "0843 555 5555" ), "Valid UK Phone Number" );
  693. assert.ok( method( "0872 555 5555" ), "Valid UK Phone Number" );
  694. assert.ok( method( "0903 555 5555" ), "Valid UK Phone Number" );
  695. assert.ok( method( "0983 555 5555" ), "Valid UK Phone Number" );
  696. assert.ok( method( "(07122) 555555" ), "Valid UK Phone Number" );
  697. assert.ok( method( "(07222) 555555" ), "Valid UK Phone Number" );
  698. assert.ok( method( "(07322) 555555" ), "Valid UK Phone Number" );
  699. assert.ok( method( "+44 7122 555 555" ), "Valid UK Phone Number" );
  700. assert.ok( method( "+44 7222 555 555" ), "Valid UK Phone Number" );
  701. assert.ok( method( "+44 7322 555 555" ), "Valid UK Phone Number" );
  702. assert.ok( !method( "7222 555555" ), "Invalid UK Phone Number" );
  703. assert.ok( !method( "+44 07222 555555" ), "Invalid UK Phone Number" );
  704. } );
  705. QUnit.test( "mobileUK", function( assert ) {
  706. var method = methodTest( "mobileUK" );
  707. assert.ok( method( "07134234323" ), "Valid UK Mobile Number" );
  708. assert.ok( method( "07334234323" ), "Valid UK Mobile Number" );
  709. assert.ok( method( "07624234323" ), "Valid UK Mobile Number" );
  710. assert.ok( method( "07734234323" ), "Valid UK Mobile Number" );
  711. assert.ok( method( "+447134234323" ), "Valid UK Mobile Number" );
  712. assert.ok( method( "+447334234323" ), "Valid UK Mobile Number" );
  713. assert.ok( method( "+447624234323" ), "Valid UK Mobile Number" );
  714. assert.ok( method( "+447734234323" ), "Valid UK Mobile Number" );
  715. assert.ok( !method( "07034234323" ), "Invalid UK Mobile Number" );
  716. assert.ok( !method( "0753423432" ), "Invalid UK Mobile Number" );
  717. assert.ok( !method( "07604234323" ), "Invalid UK Mobile Number" );
  718. assert.ok( !method( "077342343234" ), "Invalid UK Mobile Number" );
  719. assert.ok( !method( "044342343234" ), "Invalid UK Mobile Number" );
  720. assert.ok( !method( "+44753423432" ), "Invalid UK Mobile Number" );
  721. assert.ok( !method( "+447604234323" ), "Invalid UK Mobile Number" );
  722. assert.ok( !method( "+4477342343234" ), "Invalid UK Mobile Number" );
  723. assert.ok( !method( "+4444342343234" ), "Invalid UK Mobile Number" );
  724. } );
  725. QUnit.test( "dateITA", function( assert ) {
  726. var method = methodTest( "dateITA" );
  727. assert.ok( method( "01/01/1900" ), "Valid date ITA" );
  728. assert.ok( method( "17/10/2010" ), "Valid date ITA" );
  729. assert.ok( !method( "01/13/1990" ), "Invalid date ITA" );
  730. assert.ok( !method( "01.01.1900" ), "Invalid date ITA" );
  731. assert.ok( !method( "01/01/199" ), "Invalid date ITA" );
  732. } );
  733. QUnit.test( "dateFA", function( assert ) {
  734. var method = methodTest( "dateFA" );
  735. assert.ok( method( "1342/12/29" ), "Valid date FA" );
  736. assert.ok( method( "1342/12/30" ), "Valid date FA" );
  737. assert.ok( method( "1361/6/31" ), "Valid date FA" );
  738. assert.ok( method( "1321/11/30" ), "Valid date FA" );
  739. assert.ok( method( "1361/1/1" ), "Valid date FA" );
  740. assert.ok( method( "1020/3/3" ), "Valid date FA" );
  741. assert.ok( method( "1020/03/3" ), "Valid date FA" );
  742. assert.ok( method( "1020/3/03" ), "Valid date FA" );
  743. assert.ok( method( "1020/03/03" ), "Valid date FA" );
  744. assert.ok( method( "1001/7/30" ), "Valid date FA" );
  745. assert.ok( !method( "1000/1/32" ), "Invalid date FA" );
  746. assert.ok( !method( "1323/12/31" ), "Invalid date FA" );
  747. assert.ok( !method( "1361/0/11" ), "Invalid date FA" );
  748. assert.ok( !method( "63/4/4" ), "Invalid date FA" );
  749. assert.ok( !method( "15/6/1361" ), "Invalid date FA" );
  750. } );
  751. QUnit.test( "iban", function( assert ) {
  752. var method = methodTest( "iban" );
  753. assert.ok( method( "NL20INGB0001234567" ), "Valid IBAN" );
  754. assert.ok( method( "DE68 2105 0170 0012 3456 78" ), "Valid IBAN" );
  755. assert.ok( method( "NL20 INGB0001234567" ), "Valid IBAN: invalid spacing" );
  756. assert.ok( method( "NL20 INGB 00 0123 4567" ), "Valid IBAN: invalid spacing" );
  757. assert.ok( method( "XX40INGB000123456712341234" ), "Valid (more or less) IBAN: unknown country, but checksum OK" );
  758. assert.ok( !method( "1" ), "Invalid IBAN: too short" );
  759. assert.ok( !method( "NL20INGB000123456" ), "Invalid IBAN: too short" );
  760. assert.ok( !method( "NL20INGB00012345678" ), "Invalid IBAN: too long" );
  761. assert.ok( !method( "NL20INGB0001234566" ), "Invalid IBAN: checksum incorrect" );
  762. assert.ok( !method( "DE68 2105 0170 0012 3456 7" ), "Invalid IBAN: too short" );
  763. assert.ok( !method( "DE68 2105 0170 0012 3456 789" ), "Invalid IBAN: too long" );
  764. assert.ok( !method( "DE68 2105 0170 0012 3456 79" ), "Invalid IBAN: checksum incorrect" );
  765. assert.ok( !method( "NL54INGB00012345671234" ), "Invalid IBAN too long, BUT CORRECT CHECKSUM" );
  766. assert.ok( !method( "XX00INGB000123456712341234" ), "Invalid IBAN: unknown country and checksum incorrect" );
  767. // Sample IBANs for different countries
  768. assert.ok( method( "AL47 2121 1009 0000 0002 3569 8741" ), "Valid IBAN - AL" );
  769. assert.ok( method( "AD12 0001 2030 2003 5910 0100" ), "Valid IBAN - AD" );
  770. assert.ok( method( "AT61 1904 3002 3457 3201" ), "Valid IBAN - AT" );
  771. assert.ok( method( "AZ21 NABZ 0000 0000 1370 1000 1944" ), "Valid IBAN - AZ" );
  772. assert.ok( method( "BH67 BMAG 0000 1299 1234 56" ), "Valid IBAN - BH" );
  773. assert.ok( method( "BE62 5100 0754 7061" ), "Valid IBAN - BE" );
  774. assert.ok( method( "BA39 1290 0794 0102 8494" ), "Valid IBAN - BA" );
  775. assert.ok( method( "BG80 BNBG 9661 1020 3456 78" ), "Valid IBAN - BG" );
  776. assert.ok( method( "HR12 1001 0051 8630 0016 0" ), "Valid IBAN - HR" );
  777. assert.ok( method( "CH93 0076 2011 6238 5295 7" ), "Valid IBAN - CH" );
  778. assert.ok( method( "CY17 0020 0128 0000 0012 0052 7600" ), "Valid IBAN - CY" );
  779. assert.ok( method( "CZ65 0800 0000 1920 0014 5399" ), "Valid IBAN - CZ" );
  780. assert.ok( method( "DK50 0040 0440 1162 43" ), "Valid IBAN - DK" );
  781. assert.ok( method( "EE38 2200 2210 2014 5685" ), "Valid IBAN - EE" );
  782. assert.ok( method( "FO97 5432 0388 8999 44" ), "Valid IBAN - FO" );
  783. assert.ok( method( "FI21 1234 5600 0007 85" ), "Valid IBAN - FI" );
  784. assert.ok( method( "FR14 2004 1010 0505 0001 3M02 606" ), "Valid IBAN - FR" );
  785. assert.ok( method( "GE29 NB00 0000 0101 9049 17" ), "Valid IBAN - GE" );
  786. assert.ok( method( "DE89 3704 0044 0532 0130 00" ), "Valid IBAN - DE" );
  787. assert.ok( method( "GI75 NWBK 0000 0000 7099 453" ), "Valid IBAN - GI" );
  788. assert.ok( method( "GR16 0110 1250 0000 0001 2300 695" ), "Valid IBAN - GR" );
  789. assert.ok( method( "GL56 0444 9876 5432 10" ), "Valid IBAN - GL" );
  790. assert.ok( method( "HU42 1177 3016 1111 1018 0000 0000" ), "Valid IBAN - HU" );
  791. assert.ok( method( "IS14 0159 2600 7654 5510 7303 39" ), "Valid IBAN - IS" );
  792. assert.ok( method( "IE29 AIBK 9311 5212 3456 78" ), "Valid IBAN - IE" );
  793. assert.ok( method( "IL62 0108 0000 0009 9999 999" ), "Valid IBAN - IL" );
  794. assert.ok( method( "IT40 S054 2811 1010 0000 0123 456" ), "Valid IBAN - IT" );
  795. assert.ok( method( "LV80 BANK 0000 4351 9500 1" ), "Valid IBAN - LV" );
  796. assert.ok( method( "LB62 0999 0000 0001 0019 0122 9114" ), "Valid IBAN - LB" );
  797. assert.ok( method( "LI21 0881 0000 2324 013A A" ), "Valid IBAN - LI" );
  798. assert.ok( method( "LT12 1000 0111 0100 1000" ), "Valid IBAN - LT" );
  799. assert.ok( method( "LU28 0019 4006 4475 0000" ), "Valid IBAN - LU" );
  800. assert.ok( method( "MK07 2501 2000 0058 984" ), "Valid IBAN - MK" );
  801. assert.ok( method( "MT84 MALT 0110 0001 2345 MTLC AST0 01S" ), "Valid IBAN - MT" );
  802. assert.ok( method( "MU17 BOMM 0101 1010 3030 0200 000M UR" ), "Valid IBAN - MU" );
  803. assert.ok( method( "MD24 AG00 0225 1000 1310 4168" ), "Valid IBAN - MD" );
  804. assert.ok( method( "MC93 2005 2222 1001 1223 3M44 555" ), "Valid IBAN - MC" );
  805. assert.ok( method( "ME25 5050 0001 2345 6789 51" ), "Valid IBAN - ME" );
  806. assert.ok( method( "NL39 RABO 0300 0652 64" ), "Valid IBAN - NL" );
  807. assert.ok( method( "NO93 8601 1117 947" ), "Valid IBAN - NO" );
  808. assert.ok( method( "PK36 SCBL 0000 0011 2345 6702" ), "Valid IBAN - PK" );
  809. assert.ok( method( "PL60 1020 1026 0000 0422 7020 1111" ), "Valid IBAN - PL" );
  810. assert.ok( method( "PT50 0002 0123 1234 5678 9015 4" ), "Valid IBAN - PT" );
  811. assert.ok( method( "RO49 AAAA 1B31 0075 9384 0000" ), "Valid IBAN - RO" );
  812. assert.ok( method( "SM86 U032 2509 8000 0000 0270 100" ), "Valid IBAN - SM" );
  813. assert.ok( method( "SA03 8000 0000 6080 1016 7519" ), "Valid IBAN - SA" );
  814. assert.ok( method( "RS35 2600 0560 1001 6113 79" ), "Valid IBAN - RS" );
  815. assert.ok( method( "SK31 1200 0000 1987 4263 7541" ), "Valid IBAN - SK" );
  816. assert.ok( method( "SI56 1910 0000 0123 438" ), "Valid IBAN - SI" );
  817. assert.ok( method( "ES80 2310 0001 1800 0001 2345" ), "Valid IBAN - ES" );
  818. assert.ok( method( "SE35 5000 0000 0549 1000 0003" ), "Valid IBAN - SE" );
  819. assert.ok( method( "CH93 0076 2011 6238 5295 7" ), "Valid IBAN - CH" );
  820. assert.ok( method( "TN59 1000 6035 1835 9847 8831" ), "Valid IBAN - TN" );
  821. assert.ok( method( "TR33 0006 1005 1978 6457 8413 26" ), "Valid IBAN - TR" );
  822. assert.ok( method( "AE07 0331 2345 6789 0123 456" ), "Valid IBAN - AE" );
  823. assert.ok( method( "GB29 NWBK 6016 1331 9268 19" ), "Valid IBAN - GB" );
  824. } );
  825. /**
  826. * BIC tests (For BIC definition take a look on the implementation itself)
  827. */
  828. QUnit.test( "bic", function( assert ) {
  829. var method = methodTest( "bic" );
  830. assert.ok( !method( "PBNKDEF" ), "Invalid BIC: too short" );
  831. assert.ok( !method( "DEUTDEFFA1" ), "Invalid BIC: disallowed length" );
  832. assert.ok( !method( "PBNKDEFFXXX1" ), "Invalid BIC: too long" );
  833. assert.ok( !method( "1BNKDEFF" ), "Invalid BIC: invalid digit" );
  834. assert.ok( !method( "PBNKDE1F" ), "Invalid BIC: invalid digit" );
  835. assert.ok( !method( "PBNKDEFO" ), "Invalid BIC: invalid char" );
  836. assert.ok( !method( "INGDDEFFXAA" ), "Invalid BIC: invalid char" );
  837. assert.ok( !method( "DEUTDEF0" ), "Invalid BIC: invalid digit" );
  838. assert.ok( method( "DEUTDEFF" ), "Valid BIC" );
  839. assert.ok( method( "DEUTDEFFXXX" ), "Valid BIC" );
  840. assert.ok( method( "PBNKDE2F" ), "Valid BIC" );
  841. assert.ok( method( "INGDDEFF101" ), "Valid BIC" );
  842. assert.ok( method( "INGDDEF2134" ), "Valid BIC" );
  843. assert.ok( method( "INGDDE91XXX" ), "Valid BIC" );
  844. assert.ok( method( "INGDDEF2" ), "Valid BIC" );
  845. assert.ok( method( "AAFFFRP1" ), "Valid BIC" );
  846. assert.ok( method( "DEUTDEFFAB1" ), "Valid BIC" );
  847. assert.ok( method( "DEUTDEFFAXX" ), "Valid BIC" );
  848. assert.ok( method( "SSKNDE77XXX" ), "Valid BIC" );
  849. // BIC accept also lowercased values
  850. assert.ok( !method( "pbnkdef" ), "Invalid BIC: too short" );
  851. assert.ok( !method( "deutdeffa1" ), "Invalid BIC: disallowed length" );
  852. assert.ok( !method( "pbnkdeffxxx1" ), "Invalid BIC: too long" );
  853. assert.ok( !method( "1bnkdeff" ), "Invalid BIC: invalid digit" );
  854. assert.ok( !method( "ingddeffxaa" ), "Invalid BIC: invalid char" );
  855. assert.ok( method( "deutdeff" ), "Valid BIC (lowercase value)" );
  856. assert.ok( method( "deutdeffxxx" ), "Valid BIC (lowercase value)" );
  857. assert.ok( method( "pbnkde2f" ), "Valid BIC (lowercase value)" );
  858. assert.ok( method( "ingdde91xxx" ), "Valid BIC (lowercase value)" );
  859. assert.ok( method( "ingddef2" ), "Valid BIC (lowercase value)" );
  860. assert.ok( method( "deutdeffab1" ), "Valid BIC (lowercase value)" );
  861. } );
  862. QUnit.test( "postcodeUK", function( assert ) {
  863. var method = methodTest( "postcodeUK" );
  864. assert.ok( method( "AA9A 9AA" ), "Valid postcode" );
  865. assert.ok( method( "A9A 9AA" ), "Valid postcode" );
  866. assert.ok( method( "A9 9AA" ), "Valid postcode" );
  867. assert.ok( method( "A99 9AA" ), "Valid postcode" );
  868. assert.ok( method( "AA9 9AA" ), "Valid postcode" );
  869. assert.ok( method( "AA99 9AA" ), "Valid postcode" );
  870. // Channel Island
  871. assert.ok( !method( "AAAA 9AA" ), "Invalid postcode" );
  872. assert.ok( !method( "AA-2640" ), "Invalid postcode" );
  873. assert.ok( !method( "AAA AAA" ), "Invalid postcode" );
  874. assert.ok( !method( "AA AAAA" ), "Invalid postcode" );
  875. assert.ok( !method( "A AAAA" ), "Invalid postcode" );
  876. assert.ok( !method( "AAAAA" ), "Invalid postcode" );
  877. assert.ok( !method( "999 999" ), "Invalid postcode" );
  878. assert.ok( !method( "99 9999" ), "Invalid postcode" );
  879. assert.ok( !method( "9 9999" ), "Invalid postcode" );
  880. assert.ok( !method( "99999" ), "Invalid postcode" );
  881. } );
  882. QUnit.test( "dateNL", function( assert ) {
  883. var method = methodTest( "dateNL" );
  884. assert.ok( method( "01-01-1900" ), "Valid date NL" );
  885. assert.ok( method( "01.01.1900" ), "Valid date NL" );
  886. assert.ok( method( "01/01/1900" ), "Valid date NL" );
  887. assert.ok( method( "01-01-00" ), "Valid date NL" );
  888. assert.ok( method( "1-01-1900" ), "Valid date NL" );
  889. assert.ok( method( "10-10-1900" ), "Valid date NL" );
  890. assert.ok( !method( "0-01-1900" ), "Invalid date NL" );
  891. assert.ok( !method( "00-01-1900" ), "Invalid date NL" );
  892. assert.ok( !method( "35-01-1990" ), "Invalid date NL" );
  893. assert.ok( !method( "01.01.190" ), "Invalid date NL" );
  894. } );
  895. QUnit.test( "phoneNL", function( assert ) {
  896. var method = methodTest( "phoneNL" );
  897. assert.ok( method( "0701234567" ), "Valid phone NL" );
  898. assert.ok( method( "0687654321" ), "Valid phone NL" );
  899. assert.ok( method( "020-1234567" ), "Valid phone NL" );
  900. assert.ok( method( "020 - 12 34 567" ), "Valid phone NL" );
  901. assert.ok( method( "010-2345678" ), "Valid phone NL" );
  902. assert.ok( method( "+3120-1234567" ), "Valid phone NL" );
  903. assert.ok( method( "+31(0)10-2345678" ), "Valid phone NL" );
  904. assert.ok( !method( "020-123456" ), "Invalid phone NL: too short" );
  905. assert.ok( !method( "020-12345678" ), "Invalid phone NL: too long" );
  906. assert.ok( !method( "-0201234567" ), "Invalid phone NL" );
  907. assert.ok( !method( "+310201234567" ), "Invalid phone NL: no 0 after +31 allowed" );
  908. } );
  909. QUnit.test( "mobileNL", function( assert ) {
  910. var method = methodTest( "mobileNL" );
  911. assert.ok( method( "0612345678" ), "Valid NL Mobile Number" );
  912. assert.ok( method( "06-12345678" ), "Valid NL Mobile Number" );
  913. assert.ok( method( "06-12 345 678" ), "Valid NL Mobile Number" );
  914. assert.ok( method( "+316-12345678" ), "Valid NL Mobile Number" );
  915. assert.ok( method( "+31(0)6-12345678" ), "Valid NL Mobile Number" );
  916. assert.ok( !method( "abcdefghij" ), "Invalid NL Mobile Number: text" );
  917. assert.ok( !method( "0123456789" ), "Invalid NL Mobile Number: should start with 06" );
  918. assert.ok( !method( "0823456789" ), "Invalid NL Mobile Number: should start with 06" );
  919. assert.ok( !method( "06-1234567" ), "Invalid NL Mobile Number: too short" );
  920. assert.ok( !method( "06-123456789" ), "Invalid NL Mobile Number: too long" );
  921. assert.ok( !method( "-0612345678" ), "Invalid NL Mobile Number" );
  922. assert.ok( !method( "+310612345678" ), "Invalid NL Mobile Number: no 0 after +31 allowed" );
  923. } );
  924. QUnit.test( "postalcodeNL", function( assert ) {
  925. var method = methodTest( "postalcodeNL" );
  926. assert.ok( method( "1234AB" ), "Valid NL Postal Code" );
  927. assert.ok( method( "1234ab" ), "Valid NL Postal Code" );
  928. assert.ok( method( "1234 AB" ), "Valid NL Postal Code" );
  929. assert.ok( method( "6789YZ" ), "Valid NL Postal Code" );
  930. assert.ok( !method( "123AA" ), "Invalid NL Postal Code: not enough digits" );
  931. assert.ok( !method( "12345ZZ" ), "Invalid NL Postal Code: too many digits" );
  932. assert.ok( !method( "1234 AA" ), "Invalid NL Postal Code: too many spaces" );
  933. assert.ok( !method( "AA1234" ), "Invalid NL Postal Code" );
  934. assert.ok( !method( "1234-AA" ), "Invalid NL Postal Code" );
  935. } );
  936. QUnit.test( "bankaccountNL", function( assert ) {
  937. var method = methodTest( "bankaccountNL" );
  938. assert.ok( method( "755490975" ), "Valid NL bank account" );
  939. assert.ok( method( "75 54 90 975" ), "Valid NL bank account" );
  940. assert.ok( method( "123456789" ), "Valid NL bank account" );
  941. assert.ok( method( "12 34 56 789" ), "Valid NL bank account" );
  942. assert.ok( !method( "12 3456789" ), "Valid NL bank account: inconsistent spaces" );
  943. assert.ok( !method( "123 45 67 89" ), "Valid NL bank account: incorrect spaces" );
  944. assert.ok( !method( "755490971" ), "Invalid NL bank account" );
  945. assert.ok( !method( "755490973" ), "Invalid NL bank account" );
  946. assert.ok( !method( "755490979" ), "Invalid NL bank account" );
  947. assert.ok( !method( "123456781" ), "Invalid NL bank account" );
  948. assert.ok( !method( "123456784" ), "Invalid NL bank account" );
  949. assert.ok( !method( "123456788" ), "Invalid NL bank account" );
  950. } );
  951. QUnit.test( "giroaccountNL", function( assert ) {
  952. var method = methodTest( "giroaccountNL" );
  953. assert.ok( method( "123" ), "Valid NL giro account" );
  954. assert.ok( method( "1234567" ), "Valid NL giro account" );
  955. assert.ok( !method( "123456788" ), "Invalid NL giro account" );
  956. } );
  957. QUnit.test( "bankorgiroaccountNL", function( assert ) {
  958. var method = methodTest( "bankorgiroaccountNL" );
  959. assert.ok( method( "123" ), "Valid NL giro account" );
  960. assert.ok( method( "1234567" ), "Valid NL giro account" );
  961. assert.ok( method( "123456789" ), "Valid NL bank account" );
  962. assert.ok( !method( "12345678" ), "Invalid NL bank or giro account" );
  963. assert.ok( !method( "123456788" ), "Invalid NL bank or giro account" );
  964. } );
  965. QUnit.test( "time", function( assert ) {
  966. var method = methodTest( "time" );
  967. assert.ok( method( "00:00" ), "Valid time, lower bound" );
  968. assert.ok( method( "23:59" ), "Valid time, upper bound" );
  969. assert.ok( method( "3:59" ), "Valid time, single digit hour" );
  970. assert.ok( !method( "12" ), "Invalid time" );
  971. assert.ok( !method( "29:59" ), "Invalid time" );
  972. assert.ok( !method( "00:60" ), "Invalid time" );
  973. assert.ok( !method( "24:60" ), "Invalid time" );
  974. assert.ok( !method( "24:00" ), "Invalid time" );
  975. assert.ok( !method( "30:00" ), "Invalid time" );
  976. assert.ok( !method( "29:59" ), "Invalid time" );
  977. assert.ok( !method( "120:00" ), "Invalid time" );
  978. assert.ok( !method( "12:001" ), "Invalid time" );
  979. assert.ok( !method( "12:00a" ), "Invalid time" );
  980. } );
  981. QUnit.test( "time12h", function( assert ) {
  982. var method = methodTest( "time12h" );
  983. assert.ok( method( "12:00 AM" ), "Valid time, lower bound, am" );
  984. assert.ok( method( "11:59 AM" ), "Valid time, upper bound, am" );
  985. assert.ok( method( "12:00AM" ), "Valid time, no space, am" );
  986. assert.ok( method( "12:00PM" ), "Valid time, no space, pm" );
  987. assert.ok( method( "12:00 PM" ), "Valid time, lower bound, pm" );
  988. assert.ok( method( "11:59 PM" ), "Valid time, upper bound, pm" );
  989. assert.ok( method( "11:59 am" ), "Valid time, also accept lowercase" );
  990. assert.ok( method( "11:59 pm" ), "Valid time, also accept lowercase" );
  991. assert.ok( method( "1:59 pm" ), "Valid time, single hour, no leading 0" );
  992. assert.ok( method( "01:59 pm" ), "Valid time, single hour, leading 0" );
  993. assert.ok( !method( "12:00" ), "Invalid time" );
  994. assert.ok( !method( "9" ), "Invalid time" );
  995. assert.ok( !method( "9 am" ), "Invalid time" );
  996. assert.ok( !method( "12:61 am" ), "Invalid time" );
  997. assert.ok( !method( "13:00 am" ), "Invalid time" );
  998. assert.ok( !method( "00:00 am" ), "Invalid time" );
  999. } );
  1000. QUnit.test( "minWords", function( assert ) {
  1001. var method = methodTest( "minWords" );
  1002. assert.ok( method( "hello worlds", 2 ), "plain text, valid" );
  1003. assert.ok( method( "<b>hello</b> world", 2 ), "html, valid" );
  1004. assert.ok( !method( "hello", 2 ), "plain text, invalid" );
  1005. assert.ok( !method( "<b>world</b>", 2 ), "html, invalid" );
  1006. assert.ok( !method( "world <br/>", 2 ), "html, invalid" );
  1007. } );
  1008. QUnit.test( "maxWords", function( assert ) {
  1009. var method = methodTest( "maxWords" );
  1010. assert.ok( method( "hello", 2 ), "plain text, valid" );
  1011. assert.ok( method( "<b>world</b>", 2 ), "html, valid" );
  1012. assert.ok( method( "world <br/>", 2 ), "html, valid" );
  1013. assert.ok( method( "hello worlds", 2 ), "plain text, valid" );
  1014. assert.ok( method( "<b>hello</b> world", 2 ), "html, valid" );
  1015. assert.ok( !method( "hello 123 world", 2 ), "plain text, invalid" );
  1016. assert.ok( !method( "<b>hello</b> 123 world", 2 ), "html, invalid" );
  1017. } );
  1018. QUnit.test( "rangeWords", function( assert ) {
  1019. var method = methodTest( "rangeWords" );
  1020. assert.ok( method( "hello", [ 0, 2 ] ), "plain text, valid" );
  1021. assert.ok( method( "hello worlds", [ 0, 2 ] ), "plain text, valid" );
  1022. assert.ok( method( "<b>hello</b> world", [ 0, 2 ] ), "html, valid" );
  1023. assert.ok( !method( "hello worlds what is up", [ 0, 2 ] ), "plain text, invalid" );
  1024. assert.ok( !method( "<b>Hello</b> <b>world</b> <b>hello</b>", [ 0, 2 ] ), "html, invalid" );
  1025. } );
  1026. QUnit.test( "pattern", function( assert ) {
  1027. var method = methodTest( "pattern" );
  1028. assert.ok( method( "AR1004", "AR\\d{4}" ), "Correct format for the given RegExp" );
  1029. assert.ok( method( "AR1004", /^AR\d{4}$/ ), "Correct format for the given RegExp" );
  1030. assert.ok( !method( "BR1004", /^AR\d{4}$/ ), "Invalid format for the given RegExp" );
  1031. assert.ok( method( "1ABC", "[0-9][A-Z]{3}" ), "Correct format for the given RegExp" );
  1032. assert.ok( !method( "ABC", "[0-9][A-Z]{3}" ), "Invalid format for the given RegExp" );
  1033. assert.ok( !method( "1ABC DEF", "[0-9][A-Z]{3}" ), "Invalid format for the given RegExp" );
  1034. assert.ok( method( "1ABCdef", "[a-zA-Z0-9]+" ), "Correct format for the given RegExp" );
  1035. assert.ok( !method( "1ABC def", "[a-zA-Z0-9]+" ), "Invalid format for the given RegExp" );
  1036. assert.ok( method( "2014-10-02", "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ), "Correct format for the given RegExp" );
  1037. assert.ok( !method( "02-10-2014", "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ), "Invalid format for the given RegExp" );
  1038. } );
  1039. function testCardTypeByNumber( assert, number, cardname, expected ) {
  1040. $( "#cardnumber" ).val( number );
  1041. var actual = $( "#ccform" ).valid();
  1042. assert.equal( actual, expected, $.validator.format( "Expect card number {0} to validate to {1}, actually validated to ", number, expected ) );
  1043. }
  1044. QUnit.test( "creditcardtypes, all", function( assert ) {
  1045. $( "#ccform" ).validate( {
  1046. rules: {
  1047. cardnumber: {
  1048. creditcard: true,
  1049. creditcardtypes: {
  1050. all: true
  1051. }
  1052. }
  1053. }
  1054. } );
  1055. testCardTypeByNumber( assert, "4111-1111-1111-1111", "VISA", true );
  1056. testCardTypeByNumber( assert, "5111-1111-1111-1118", "MasterCard", true );
  1057. testCardTypeByNumber( assert, "6111-1111-1111-1116", "Discover", true );
  1058. testCardTypeByNumber( assert, "3400-0000-0000-009", "AMEX", true );
  1059. testCardTypeByNumber( assert, "4111-1111-1111-1110", "VISA", false );
  1060. testCardTypeByNumber( assert, "5432-1111-1111-1111", "MasterCard", false );
  1061. testCardTypeByNumber( assert, "6611-6611-6611-6611", "Discover", false );
  1062. testCardTypeByNumber( assert, "3777-7777-7777-7777", "AMEX", false );
  1063. } );
  1064. QUnit.test( "creditcardtypes, visa", function( assert ) {
  1065. $( "#ccform" ).validate( {
  1066. rules: {
  1067. cardnumber: {
  1068. creditcard: true,
  1069. creditcardtypes: {
  1070. visa: true
  1071. }
  1072. }
  1073. }
  1074. } );
  1075. testCardTypeByNumber( assert, "4111-1111-1111-1111", "VISA", true );
  1076. testCardTypeByNumber( assert, "5111-1111-1111-1118", "MasterCard", false );
  1077. testCardTypeByNumber( assert, "6111-1111-1111-1116", "Discover", false );
  1078. testCardTypeByNumber( assert, "3400-0000-0000-009", "AMEX", false );
  1079. } );
  1080. QUnit.test( "creditcardtypes, mastercard", function( assert ) {
  1081. $( "#ccform" ).validate( {
  1082. rules: {
  1083. cardnumber: {
  1084. creditcard: true,
  1085. creditcardtypes: {
  1086. mastercard: true
  1087. }
  1088. }
  1089. }
  1090. } );
  1091. testCardTypeByNumber( assert, "5111-1111-1111-1118", "MasterCard", true );
  1092. testCardTypeByNumber( assert, "6111-1111-1111-1116", "Discover", false );
  1093. testCardTypeByNumber( assert, "3400-0000-0000-009", "AMEX", false );
  1094. testCardTypeByNumber( assert, "4111-1111-1111-1111", "VISA", false );
  1095. } );
  1096. function fillFormWithValuesAndExpect( assert, formSelector, inputValues, expected ) {
  1097. var i, actual;
  1098. for ( i = 0; i < inputValues.length; i++ ) {
  1099. $( formSelector + " input:eq(" + i + ")" ).val( inputValues[ i ] );
  1100. }
  1101. actual = $( formSelector ).valid();
  1102. assert.equal( actual, expected, $.validator.format( "Filled inputs of form '{0}' with {1} values ({2})", formSelector, inputValues.length, inputValues.toString() ) );
  1103. }
  1104. QUnit.test( "require_from_group", function( assert ) {
  1105. $( "#productInfo" ).validate( {
  1106. rules: {
  1107. partnumber: { require_from_group: [ 2, ".productInfo" ] },
  1108. description: { require_from_group: [ 2, ".productInfo" ] },
  1109. discount: { require_from_group: [ 2, ".productInfo" ] }
  1110. }
  1111. } );
  1112. fillFormWithValuesAndExpect( assert, "#productInfo", [], false );
  1113. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123 ], false );
  1114. $( "#productInfo input[type='checkbox']" ).attr( "checked", "checked" );
  1115. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123 ], true );
  1116. $( "#productInfo input[type='checkbox']" ).removeAttr( "checked" );
  1117. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget" ], true );
  1118. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget", "red" ], true );
  1119. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget", "red" ], true );
  1120. } );
  1121. QUnit.test( "require_from_group preserve other rules", function( assert ) {
  1122. $( "#productInfo" ).validate( {
  1123. rules: {
  1124. partnumber: { require_from_group: [ 2, ".productInfo" ] },
  1125. description: { require_from_group: [ 2, ".productInfo" ] },
  1126. color: { require_from_group: [ 2, ".productInfo" ] },
  1127. supplier: { required: true }
  1128. }
  1129. } );
  1130. fillFormWithValuesAndExpect( assert, "#productInfo", [], false );
  1131. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123 ], false );
  1132. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget" ], false );
  1133. fillFormWithValuesAndExpect( assert, "#productInfo", [ "", "", "", "Acme" ], false );
  1134. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "", "", "Acme" ], false );
  1135. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget", "", "Acme" ], true );
  1136. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget", "red", "Acme" ], true );
  1137. } );
  1138. QUnit.test( "skip_or_fill_minimum", function( assert ) {
  1139. $( "#productInfo" ).validate( {
  1140. rules: {
  1141. partnumber: { skip_or_fill_minimum: [ 2, ".productInfo" ] },
  1142. description: { skip_or_fill_minimum: [ 2, ".productInfo" ] },
  1143. color: { skip_or_fill_minimum: [ 2, ".productInfo" ] }
  1144. }
  1145. } );
  1146. fillFormWithValuesAndExpect( assert, "#productInfo", [], true );
  1147. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123 ], false );
  1148. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget" ], true );
  1149. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget", "red" ], true );
  1150. } );
  1151. QUnit.test( "skip_or_fill_minimum preserve other rules", function( assert ) {
  1152. $( "#productInfo" ).validate( {
  1153. rules: {
  1154. partnumber: { skip_or_fill_minimum: [ 2, ".productInfo" ] },
  1155. description: { skip_or_fill_minimum: [ 2, ".productInfo" ] },
  1156. color: { skip_or_fill_minimum: [ 2, ".productInfo" ] },
  1157. supplier: { required: true }
  1158. }
  1159. } );
  1160. fillFormWithValuesAndExpect( assert, "#productInfo", [], false );
  1161. fillFormWithValuesAndExpect( assert, "#productInfo", [ "", "", "", "Acme" ], true );
  1162. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "", "", "Acme" ], false );
  1163. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget", "", "Acme" ], true );
  1164. fillFormWithValuesAndExpect( assert, "#productInfo", [ 123, "widget", "red", "Acme" ], true );
  1165. } );
  1166. QUnit.test( "zipcodeUS", function( assert ) {
  1167. var method = methodTest( "zipcodeUS" );
  1168. assert.ok( method( "12345" ), "Valid zip" );
  1169. assert.ok( method( "12345-2345" ), "Valid zip" );
  1170. assert.ok( method( "90210-4567" ), "Valid zip" );
  1171. assert.ok( !method( "1" ), "Invalid zip" );
  1172. assert.ok( !method( "1234" ), "Invalid zip" );
  1173. assert.ok( !method( "123-23" ), "Invalid zip" );
  1174. assert.ok( !method( "12345-43" ), "Invalid zip" );
  1175. assert.ok( !method( "123456-7890" ), "Invalid zip" );
  1176. } );
  1177. QUnit.test( "nifES", function( assert ) {
  1178. var method = methodTest( "nifES" );
  1179. assert.ok( method( "11441059P" ), "NIF valid" );
  1180. assert.ok( method( "80054306T" ), "NIF valid" );
  1181. assert.ok( method( "76048581R" ), "NIF valid" );
  1182. assert.ok( method( "28950849J" ), "NIF valid" );
  1183. assert.ok( method( "34048598L" ), "NIF valid" );
  1184. assert.ok( method( "28311529R" ), "NIF valid" );
  1185. assert.ok( method( "34673804Q" ), "NIF valid" );
  1186. assert.ok( method( "92133247P" ), "NIF valid" );
  1187. assert.ok( method( "77149717N" ), "NIF valid" );
  1188. assert.ok( method( "15762034L" ), "NIF valid" );
  1189. assert.ok( method( "05122654W" ), "NIF valid" );
  1190. assert.ok( method( "05122654w" ), "NIF valid: lower case" );
  1191. assert.ok( method( "M1503708Z" ), "NIF valid. Temporary foreign nif" );
  1192. assert.ok( !method( "1144105R" ), "NIF invalid: less than 8 digits without zero" );
  1193. assert.ok( !method( "11441059 R" ), "NIF invalid: white space" );
  1194. assert.ok( !method( "11441059" ), "NIF invalid: no letter" );
  1195. assert.ok( !method( "11441059PR" ), "NIF invalid: two letters" );
  1196. assert.ok( !method( "11440059R" ), "NIF invalid: wrong number" );
  1197. assert.ok( !method( "11441059S" ), "NIF invalid: wrong letter" );
  1198. assert.ok( !method( "114410598R" ), "NIF invalid: > 8 digits" );
  1199. assert.ok( !method( "11441059-R" ), "NIF invalid: dash" );
  1200. assert.ok( !method( "asdasdasd" ), "NIF invalid: all letters" );
  1201. assert.ok( !method( "11.144.059R" ), "NIF invalid: two dots" );
  1202. assert.ok( !method( "05.122.654R" ), "NIF invalid: starts with 0 and dots" );
  1203. assert.ok( !method( "5.122.654-R" ), "NIF invalid: dots and dash" );
  1204. assert.ok( !method( "05.122.654-R" ), "NIF invalid: starts with zero and dot and dash" );
  1205. } );
  1206. QUnit.test( "nieES", function( assert ) {
  1207. var method = methodTest( "nieES" );
  1208. assert.ok( method( "X0093999K" ), "NIE valid" );
  1209. assert.ok( method( "X1923000Q" ), "NIE valid" );
  1210. assert.ok( method( "Z9669587R" ), "NIE valid" );
  1211. assert.ok( method( "Z8945005B" ), "NIE valid" );
  1212. assert.ok( method( "Z6663465W" ), "NIE valid" );
  1213. assert.ok( method( "Y7875935J" ), "NIE valid" );
  1214. assert.ok( method( "X3390130E" ), "NIE valid" );
  1215. assert.ok( method( "Y7699182S" ), "NIE valid" );
  1216. assert.ok( method( "Y1524243R" ), "NIE valid" );
  1217. assert.ok( method( "X3744072V" ), "NIE valid" );
  1218. assert.ok( method( "X7436800A" ), "NIE valid" );
  1219. assert.ok( method( "X00002153Z" ), "NIE valid" );
  1220. assert.ok( method( "X02323232W" ), "NIE valid" );
  1221. assert.ok( method( "Z0569549M" ), "NIE valid" );
  1222. assert.ok( method( "X0479906B" ), "NIE valid" );
  1223. assert.ok( method( "y7875935j" ), "NIE valid: lower case" );
  1224. assert.ok( !method( "X0093999 K" ), "NIE invalid: white space" );
  1225. assert.ok( !method( "X 0093999 K" ), "NIE invalid: white space" );
  1226. assert.ok( !method( "11441059" ), "NIE invalid: no letter" );
  1227. assert.ok( !method( "11441059PR" ), "NIE invalid: two letters" );
  1228. assert.ok( !method( "11440059R" ), "NIE invalid: wrong number" );
  1229. assert.ok( !method( "11441059S" ), "NIE invalid: wrong letter" );
  1230. assert.ok( !method( "114410598R" ), "NIE invalid: > 8 digits" );
  1231. assert.ok( !method( "11441059-R" ), "NIE invalid: dash" );
  1232. assert.ok( !method( "asdasdasd" ), "NIE invalid: all letters" );
  1233. assert.ok( !method( "11.144.059R" ), "NIE invalid: two dots" );
  1234. assert.ok( !method( "05.122.654R" ), "NIE invalid: starts with 0 and dots" );
  1235. assert.ok( !method( "5.122.654-R" ), "NIE invalid: dots and dash" );
  1236. assert.ok( !method( "05.122.654-R" ), "NIE invalid: starts with zero and dot and dash" );
  1237. } );
  1238. QUnit.test( "cifES", function( assert ) {
  1239. var method = methodTest( "cifES" );
  1240. assert.ok( method( "A58818501" ), "CIF valid" );
  1241. assert.ok( method( "A79082244" ), "CIF valid" );
  1242. assert.ok( method( "A60917978" ), "CIF valid" );
  1243. assert.ok( method( "A39000013" ), "CIF valid" );
  1244. assert.ok( method( "A28315182" ), "CIF valid" );
  1245. assert.ok( method( "A75409573" ), "CIF valid" );
  1246. assert.ok( method( "A34396994" ), "CIF valid" );
  1247. assert.ok( method( "A08153538" ), "CIF valid" );
  1248. assert.ok( method( "A09681396" ), "CIF valid" );
  1249. assert.ok( method( "A06706303" ), "CIF valid" );
  1250. assert.ok( method( "A66242173" ), "CIF valid" );
  1251. assert.ok( method( "A61416699" ), "CIF valid" );
  1252. assert.ok( method( "A99545444" ), "CIF valid" );
  1253. assert.ok( method( "A10407252" ), "CIF valid" );
  1254. assert.ok( method( "A76170885" ), "CIF valid" );
  1255. assert.ok( method( "A83535047" ), "CIF valid" );
  1256. assert.ok( method( "A46031969" ), "CIF valid" );
  1257. assert.ok( method( "A97252910" ), "CIF valid" );
  1258. assert.ok( method( "A79082244" ), "CIF valid" );
  1259. assert.ok( !method( "A7908224D" ), "CIF invalid: digit control must be a number (4)" );
  1260. assert.ok( method( "B71413892" ), "CIF valid" );
  1261. assert.ok( method( "B37484755" ), "CIF valid" );
  1262. assert.ok( method( "B15940893" ), "CIF valid" );
  1263. assert.ok( method( "B55429161" ), "CIF valid" );
  1264. assert.ok( method( "B93337087" ), "CIF valid" );
  1265. assert.ok( method( "B43522192" ), "CIF valid" );
  1266. assert.ok( method( "B38624334" ), "CIF valid" );
  1267. assert.ok( method( "B21920426" ), "CIF valid" );
  1268. assert.ok( method( "B74940156" ), "CIF valid" );
  1269. assert.ok( method( "B46125746" ), "CIF valid" );
  1270. assert.ok( method( "B67077537" ), "CIF valid" );
  1271. assert.ok( method( "B21283155" ), "CIF valid" );
  1272. assert.ok( method( "B57104176" ), "CIF valid" );
  1273. assert.ok( method( "B25060179" ), "CIF valid" );
  1274. assert.ok( method( "B06536338" ), "CIF valid" );
  1275. assert.ok( method( "B50964592" ), "CIF valid" );
  1276. assert.ok( method( "B15653330" ), "CIF valid" );
  1277. assert.ok( method( "B83524710" ), "CIF valid" );
  1278. assert.ok( !method( "B8352471J" ), "CIF invalid: digit control must be a number (0)" );
  1279. assert.ok( !method( "C27827551" ), "CIF invalid: wrong digit control" );
  1280. assert.ok( !method( "C27827552" ), "CIF invalid: wrong digit control" );
  1281. assert.ok( !method( "C27827553" ), "CIF invalid: wrong digit control" );
  1282. assert.ok( !method( "C27827554" ), "CIF invalid: wrong digit control" );
  1283. assert.ok( !method( "C27827555" ), "CIF invalid: wrong digit control" );
  1284. assert.ok( !method( "C27827556" ), "CIF invalid: wrong digit control" );
  1285. assert.ok( !method( "C27827557" ), "CIF invalid: wrong digit control" );
  1286. assert.ok( !method( "C27827558" ), "CIF invalid: wrong digit control" );
  1287. assert.ok( !method( "C27827550" ), "CIF invalid: wrong digit control" );
  1288. assert.ok( !method( "C2782755A" ), "CIF invalid: wrong digit control" );
  1289. assert.ok( !method( "C2782755B" ), "CIF invalid: wrong digit control" );
  1290. assert.ok( !method( "C2782755C" ), "CIF invalid: wrong digit control" );
  1291. assert.ok( !method( "C2782755D" ), "CIF invalid: wrong digit control" );
  1292. assert.ok( !method( "C2782755E" ), "CIF invalid: wrong digit control" );
  1293. assert.ok( !method( "C2782755F" ), "CIF invalid: wrong digit control" );
  1294. assert.ok( !method( "C2782755G" ), "CIF invalid: wrong digit control" );
  1295. assert.ok( !method( "C2782755H" ), "CIF invalid: wrong digit control" );
  1296. assert.ok( !method( "C2782755J" ), "CIF invalid: wrong digit control" );
  1297. assert.ok( method( "C2782755I" ), "CIF valid. Digit control can be either a number or letter" );
  1298. assert.ok( method( "C27827559" ), "CIF valid. Digit control can be either a number or letter" );
  1299. assert.ok( method( "E48911572" ), "CIF valid" );
  1300. assert.ok( method( "E93928703" ), "CIF valid" );
  1301. assert.ok( method( "E17472952" ), "CIF valid" );
  1302. assert.ok( !method( "E1747295B" ), "CIF invalid: digit control must be a number (2)" );
  1303. assert.ok( method( "F41190612" ), "CIF valid" );
  1304. assert.ok( method( "F4119061B" ), "CIF valid. Digit control can be either a number or letter" );
  1305. assert.ok( method( "G72102064" ), "CIF valid" );
  1306. assert.ok( method( "G32937757" ), "CIF valid" );
  1307. assert.ok( method( "G8984953C" ), "CIF valid" );
  1308. assert.ok( method( "G3370454E" ), "CIF valid" );
  1309. assert.ok( method( "G33704545" ), "CIF valid. Digit control can be either a number or letter" );
  1310. assert.ok( method( "H48911572" ), "CIF valid" );
  1311. assert.ok( method( "H93928703" ), "CIF valid" );
  1312. assert.ok( method( "H17472952" ), "CIF valid" );
  1313. assert.ok( !method( "H1747295B" ), "CIF invalid: digit control must be a number (2)" );
  1314. assert.ok( !method( "I48911572" ), "CIF invalid: starts with I" );
  1315. assert.ok( method( "J85081081" ), "CIF valid" );
  1316. assert.ok( method( "J8508108A" ), "CIF valid" );
  1317. assert.ok( method( "K3902238I" ), "CIF valid" );
  1318. assert.ok( !method( "K39022389" ), "CIF invalid. Digit control must be a letter (I)" );
  1319. assert.ok( method( "M9916080F" ), "CIF valid" );
  1320. assert.ok( method( "M1566151E" ), "CIF valid" );
  1321. assert.ok( method( "M15661515" ), "CIF valid" );
  1322. assert.ok( method( "M4778730D" ), "CIF valid" );
  1323. assert.ok( method( "N1172218H" ), "CIF valid" );
  1324. assert.ok( method( "N4094938J" ), "CIF valid" );
  1325. assert.ok( method( "N40949380" ), "CIF valid. Digit control can be either a number or letter" );
  1326. assert.ok( method( "P5141387J" ), "CIF valid" );
  1327. assert.ok( method( "P9803881C" ), "CIF valid" );
  1328. assert.ok( !method( "P98038813" ), "CIF invalid: digit control must be a letter (C)" );
  1329. assert.ok( method( "Q5141387J" ), "CIF valid" );
  1330. assert.ok( method( "Q9803881C" ), "CIF valid" );
  1331. assert.ok( !method( "Q98038813" ), "CIF invalid: digit control must be a letter (C)" );
  1332. assert.ok( method( "S5141387J" ), "CIF valid" );
  1333. assert.ok( method( "S9803881C" ), "CIF valid" );
  1334. assert.ok( !method( "S98038813" ), "CIF invalid: digit control must be a letter (C)" );
  1335. assert.ok( method( "s98038813" ), "CIF valid: lower case" );
  1336. assert.ok( !method( "X48911572" ), "CIF invalid: starts with X" );
  1337. assert.ok( !method( "Y48911572" ), "CIF invalid: starts with Y" );
  1338. assert.ok( !method( "Z48911572" ), "CIF invalid: starts with Z" );
  1339. assert.ok( !method( "Z98038813" ), "CIF invalid: wrong letter" );
  1340. assert.ok( !method( "B 43522192" ), "CIF invalid: white spaces" );
  1341. assert.ok( !method( "43522192" ), "CIF invalid: missing letter" );
  1342. assert.ok( !method( "BB43522192" ), "CIF invalid: two letters" );
  1343. assert.ok( !method( "B53522192" ), "CIF invalid: wrong number" );
  1344. assert.ok( !method( "B433522192" ), "CIF invalid: > 8 digits" );
  1345. assert.ok( !method( "B3522192" ), "CIF invalid: < 8 digits" );
  1346. assert.ok( !method( "B-43522192" ), "CIF invalid: dash" );
  1347. assert.ok( !method( "Basdasdas" ), "CIF invalid: all letters" );
  1348. assert.ok( !method( "B43.522.192" ), "CIF invalid: dots" );
  1349. assert.ok( !method( "B-43.522.192" ), "CIF invalid: dots and dash" );
  1350. } );
  1351. QUnit.test( "nipPL", function( assert ) {
  1352. var method = methodTest( "nipPL" );
  1353. assert.ok( method( "3514242002" ), "NIP valid" );
  1354. assert.ok( method( "8117892840" ), "NIP valid" );
  1355. assert.ok( method( "7249598309" ), "NIP valid" );
  1356. assert.ok( method( "6853539166" ), "NIP valid" );
  1357. assert.ok( method( "5715750580" ), "NIP valid" );
  1358. assert.ok( method( "3496120813" ), "NIP valid" );
  1359. assert.ok( method( "1565710251" ), "NIP valid" );
  1360. assert.ok( method( "8190761165" ), "NIP valid" );
  1361. assert.ok( method( "9487499667" ), "NIP valid" );
  1362. assert.ok( method( "9283384684" ), "NIP valid" );
  1363. assert.ok( method( "3887569138" ), "NIP valid" );
  1364. assert.ok( method( "3962898856" ), "NIP valid" );
  1365. assert.ok( !method( "76355753" ), "NIP invalid: too short" );
  1366. assert.ok( !method( "454" ), "NIP invalid: too short" );
  1367. assert.ok( !method( "234565545" ), "NIP invalid: too short" );
  1368. assert.ok( !method( "543455" ), "NIP invalid: too short" );
  1369. assert.ok( !method( "6345634563456" ), "NIP invalid: too long" );
  1370. assert.ok( !method( "53453453455335" ), "NIP invalid: too long" );
  1371. assert.ok( !method( "543453760902" ), "NIP invalid: too long" );
  1372. assert.ok( !method( "43090012454" ), "NIP invalid: too long" );
  1373. assert.ok( !method( "3958250194" ), "NIP invalid: wrong checksum" );
  1374. assert.ok( !method( "3928541049" ), "NIP invalid: wrong checksum" );
  1375. assert.ok( !method( "5920397295" ), "NIP invalid: wrong checksum" );
  1376. assert.ok( !method( "9502947712" ), "NIP invalid: wrong checksum" );
  1377. } );
  1378. QUnit.test( "maxWords", function( assert ) {
  1379. var method = methodTest( "maxWords" ),
  1380. maxWords = 6;
  1381. assert.ok( method( "I am a sentence", maxWords ), "Max Words" );
  1382. assert.ok( !method( "I'm way too long for this sentence!", maxWords ), "Too many words" );
  1383. assert.ok( method( "Don’t “count” me as too long", maxWords ), "Right amount of words with smartquotes" );
  1384. assert.ok( !method( "But you can “count” me as too long", maxWords ), "Too many words with smartquotes" );
  1385. assert.ok( method( "<div>Don’t “count” me as too long</div>", maxWords ), "Right amount of words with smartquotes w/ HTML" );
  1386. assert.ok( !method( "<div>But you can “count” me as too long</div>", maxWords ), "Too many words with smartquotes w/ HTML" );
  1387. } );
  1388. QUnit.test( "minWords", function( assert ) {
  1389. var method = methodTest( "minWords" ),
  1390. minWords = 6;
  1391. assert.ok( !method( "I am a short sentence", minWords ), "Max Words" );
  1392. assert.ok( method( "I'm way too long for this sentence!", minWords ), "Too many words" );
  1393. assert.ok( !method( "Don’t “count” me as short.", minWords ), "Right amount of words with smartquotes" );
  1394. assert.ok( method( "But you can “count” me as too short", minWords ), "Too many words with smartquotes" );
  1395. assert.ok( !method( "<div>“Count” me as too short.</div>", minWords ), "Right amount of words with smartquotes w/ HTML" );
  1396. assert.ok( method( "<div>But you can “count” me as too long</div>", minWords ), "Too many words with smartquotes w/ HTML" );
  1397. } );
  1398. QUnit.test( "rangeWords", function( assert ) {
  1399. var method = methodTest( "rangeWords" ),
  1400. rangeWords = [ 3, 6 ];
  1401. assert.ok( !method( "I'm going to be longer than “six words!”", rangeWords ), "Longer than 6 with smartquotes" );
  1402. assert.ok( method( "I'm just the right amount!", rangeWords ), "In between" );
  1403. assert.ok( method( "Super short sentence’s.", rangeWords ), "Low end" );
  1404. assert.ok( !method( "I", rangeWords ), "Too short" );
  1405. assert.ok( method( "<div>“Count” me as perfect.</div>", rangeWords ), "Right amount of words with smartquotes w/ HTML" );
  1406. assert.ok( !method( "<div>But you can “count” me as too long</div>", rangeWords ), "Too many words with smartquotes w/ HTML" );
  1407. } );
  1408. QUnit.test( "currency", function( assert ) { // Works with any symbol
  1409. var method = methodTest( "currency" );
  1410. assert.ok( method( "£9", "£" ), "Symbol no decimal" );
  1411. assert.ok( method( "£9.9", "£" ), "£, one decimal" );
  1412. assert.ok( method( "£9.99", "£" ), "£, two decimal" );
  1413. assert.ok( method( "£9.90", "£" ), "Valid currency" );
  1414. assert.ok( method( "£9,999.9", "£" ), "£, thousand, comma separator, one decimal" );
  1415. assert.ok( method( "£9,999.99", "£" ), "£, thousand, comma separator, two decimal" );
  1416. assert.ok( method( "£9,999,999.9", "£" ), "£, million, comma separators, one decimal" );
  1417. assert.ok( method( "9", [ "£", false ] ), "Valid currency" );
  1418. assert.ok( method( "9.9", [ "£", false ] ), "Valid currency" );
  1419. assert.ok( method( "9.99", [ "£", false ] ), "Valid currency" );
  1420. assert.ok( method( "9.90", [ "£", false ] ), "Valid currency" );
  1421. assert.ok( method( "9,999.9", [ "£", false ] ), "Valid currency" );
  1422. assert.ok( method( "9,999.99", [ "£", false ] ), "Valid currency" );
  1423. assert.ok( method( "9,999,999.9", [ "£", false ] ), "Valid currency" );
  1424. assert.ok( !method( "9,", "£" ), "Invalid currency" );
  1425. assert.ok( !method( "9,99.99", "£" ), "Invalid currency" );
  1426. assert.ok( !method( "9,", "£" ), "Invalid currency" );
  1427. assert.ok( !method( "9.999", "£" ), "Invalid currency" );
  1428. assert.ok( !method( "9.999", "£" ), "Invalid currency" );
  1429. assert.ok( !method( "9.99,9", "£" ), "Invalid currency" );
  1430. } );
  1431. QUnit.test( "postalCodeCA", function( assert ) {
  1432. var method = methodTest( "postalCodeCA" );
  1433. assert.ok( method( "H0H0H0" ), "Valid Canadian postal code: all upper case with no space" );
  1434. assert.ok( method( "H0H 0H0" ), "Valid Canadian postal code: all upper case with one space" );
  1435. assert.ok( method( "H0H 0H0" ), "Valid Canadian postal code: all upper case with multiple spaces" );
  1436. assert.ok( method( "h0h 0h0" ), "Valid Canadian postal code: all lower case with space" );
  1437. assert.ok( method( "h0h0h0" ), "Valid Canadian postal code: all lower case with no space" );
  1438. assert.ok( !method( "H0H-0H0" ), "Invalid Canadian postal code: dash used as separator" );
  1439. assert.ok( !method( "H0H 0H" ), "Invalid Canadian postal code: too short" );
  1440. assert.ok( !method( "Z0H 0H0" ), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVXY' are valid first characters for the Forward Sorting Area" );
  1441. assert.ok( !method( "H0D 0H0" ), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVWXYZ' are valid third characters for the Forward Sorting Area" );
  1442. assert.ok( !method( "H0H 0D0" ), "Invalid Canadian postal code: only 'ABCEGHJKLMNPRSTVWXYZ' are valid second characters for the Local Delivery Unit" );
  1443. } );
  1444. QUnit.test( "stateUS", function( assert ) {
  1445. var method = methodTest( "stateUS" );
  1446. assert.ok( method( "AZ" ), "Valid US state" );
  1447. assert.ok( method( "OH" ), "Valid US state" );
  1448. assert.ok( method( "DC" ), "Valid US state" );
  1449. assert.ok( method( "PR", { includeTerritories: true } ), "Valid US territory" );
  1450. assert.ok( method( "AA", { includeMilitary: true } ), "Valid US military zone" );
  1451. assert.ok( method( "me", { caseSensitive: false } ), "Valid US state" );
  1452. assert.ok( !method( "az", { caseSensitive: true } ), "Must be capital letters" );
  1453. assert.ok( !method( "mp", { caseSensitive: false, includeTerritories: false } ), "US territories not allowed" );
  1454. } );
  1455. QUnit.test( "postalcodeBR", function( assert ) {
  1456. var method = methodTest( "postalcodeBR" );
  1457. assert.ok( method( "99999-999" ), "Valid BR Postal Code" );
  1458. assert.ok( method( "99999999" ), "Valid BR Postal Code" );
  1459. assert.ok( method( "99.999-999" ), "Valid BR Postal Code" );
  1460. assert.ok( !method( "99.999999" ), "Invalid BR Postal Code" );
  1461. } );
  1462. QUnit.test( "cpfBR", function( assert ) {
  1463. var method = methodTest( "cpfBR" );
  1464. assert.ok( method( "11144477735" ), "Valid CPF Number" );
  1465. assert.ok( method( "263.946.533-30" ), "Valid CPF Number" );
  1466. assert.ok( method( "325 861 044 47" ), "Valid CPF Number" );
  1467. assert.ok( method( "859-684-732-40" ), "Valid CPF Number" );
  1468. assert.ok( !method( "99999999999" ), "Invalid CPF Number: dump data" );
  1469. assert.ok( !method( "1114447773" ), "Invalid CPF Number: < 11 digits" );
  1470. assert.ok( !method( "111444777355" ), "Invalid CPF Number: > 11 digits" );
  1471. assert.ok( !method( "11144477715" ), "Invalid CPF Number: 1st check number failed" );
  1472. assert.ok( !method( "11144477737" ), "Invalid CPF Number: 2nd check number failed" );
  1473. } );
  1474. QUnit.test( "file accept - image wildcard", function( assert ) {
  1475. var input = acceptFileDummyInput( "test.png", "image/png" ),
  1476. $form = $( "<form />" ),
  1477. proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "image/*" );
  1478. assert.ok( proxy(), "the selected file for upload has specified mime type" );
  1479. } );
  1480. QUnit.test( "file accept - specified mime type", function( assert ) {
  1481. var input = acceptFileDummyInput( "test.kml", "application/vnd.google-earth.kml+xml" ),
  1482. $form = $( "<form />" ),
  1483. proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "application/vnd.google-earth.kml+xml" );
  1484. assert.ok( proxy(), "the selected file for upload has specified mime type" );
  1485. } );
  1486. QUnit.test( "file accept - multiple mimetypes", function( assert ) {
  1487. var input = acceptFileDummyInput( "test.png", "image/png" ),
  1488. $form = $( "<form />" ),
  1489. proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "image/png,video/jpeg" );
  1490. assert.ok( proxy(), "the selected file for upload has specified mime type" );
  1491. } );
  1492. QUnit.test( "file accept - multiple mimetypes with wildcard", function( assert ) {
  1493. var input = acceptFileDummyInput( "test.mp3", "audio/mpeg" ),
  1494. $form = $( "<form />" ),
  1495. proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "image/*,audio/*" );
  1496. assert.ok( proxy(), "the selected file for upload has specified mime type" );
  1497. } );
  1498. QUnit.test( "file accept - invalid mime type", function( assert ) {
  1499. var input = acceptFileDummyInput( "test.kml", "foobar/vnd.google-earth.kml+xml" ),
  1500. $form = $( "<form />" ),
  1501. proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "application/vnd.google-earth.kml+xml" );
  1502. assert.equal( proxy(), false, "the selected file for upload has invalid mime type" );
  1503. } );