Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

jquery.validate.js 50KB

pirms 2 gadiem
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. /*!
  2. * jQuery Validation Plugin v1.19.3
  3. *
  4. * https://jqueryvalidation.org/
  5. *
  6. * Copyright (c) 2021 Jörn Zaefferer
  7. * Released under the MIT license
  8. */
  9. (function( factory ) {
  10. if ( typeof define === "function" && define.amd ) {
  11. define( ["jquery"], factory );
  12. } else if (typeof module === "object" && module.exports) {
  13. module.exports = factory( require( "jquery" ) );
  14. } else {
  15. factory( jQuery );
  16. }
  17. }(function( $ ) {
  18. $.extend( $.fn, {
  19. // https://jqueryvalidation.org/validate/
  20. validate: function( options ) {
  21. // If nothing is selected, return nothing; can't chain anyway
  22. if ( !this.length ) {
  23. if ( options && options.debug && window.console ) {
  24. console.warn( "Nothing selected, can't validate, returning nothing." );
  25. }
  26. return;
  27. }
  28. // Check if a validator for this form was already created
  29. var validator = $.data( this[ 0 ], "validator" );
  30. if ( validator ) {
  31. return validator;
  32. }
  33. // Add novalidate tag if HTML5.
  34. this.attr( "novalidate", "novalidate" );
  35. validator = new $.validator( options, this[ 0 ] );
  36. $.data( this[ 0 ], "validator", validator );
  37. if ( validator.settings.onsubmit ) {
  38. this.on( "click.validate", ":submit", function( event ) {
  39. // Track the used submit button to properly handle scripted
  40. // submits later.
  41. validator.submitButton = event.currentTarget;
  42. // Allow suppressing validation by adding a cancel class to the submit button
  43. if ( $( this ).hasClass( "cancel" ) ) {
  44. validator.cancelSubmit = true;
  45. }
  46. // Allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
  47. if ( $( this ).attr( "formnovalidate" ) !== undefined ) {
  48. validator.cancelSubmit = true;
  49. }
  50. } );
  51. // Validate the form on submit
  52. this.on( "submit.validate", function( event ) {
  53. if ( validator.settings.debug ) {
  54. // Prevent form submit to be able to see console output
  55. event.preventDefault();
  56. }
  57. function handle() {
  58. var hidden, result;
  59. // Insert a hidden input as a replacement for the missing submit button
  60. // The hidden input is inserted in two cases:
  61. // - A user defined a `submitHandler`
  62. // - There was a pending request due to `remote` method and `stopRequest()`
  63. // was called to submit the form in case it's valid
  64. if ( validator.submitButton && ( validator.settings.submitHandler || validator.formSubmitted ) ) {
  65. hidden = $( "<input type='hidden'/>" )
  66. .attr( "name", validator.submitButton.name )
  67. .val( $( validator.submitButton ).val() )
  68. .appendTo( validator.currentForm );
  69. }
  70. if ( validator.settings.submitHandler && !validator.settings.debug ) {
  71. result = validator.settings.submitHandler.call( validator, validator.currentForm, event );
  72. if ( hidden ) {
  73. // And clean up afterwards; thanks to no-block-scope, hidden can be referenced
  74. hidden.remove();
  75. }
  76. if ( result !== undefined ) {
  77. return result;
  78. }
  79. return false;
  80. }
  81. return true;
  82. }
  83. // Prevent submit for invalid forms or custom submit handlers
  84. if ( validator.cancelSubmit ) {
  85. validator.cancelSubmit = false;
  86. return handle();
  87. }
  88. if ( validator.form() ) {
  89. if ( validator.pendingRequest ) {
  90. validator.formSubmitted = true;
  91. return false;
  92. }
  93. return handle();
  94. } else {
  95. validator.focusInvalid();
  96. return false;
  97. }
  98. } );
  99. }
  100. return validator;
  101. },
  102. // https://jqueryvalidation.org/valid/
  103. valid: function() {
  104. var valid, validator, errorList;
  105. if ( $( this[ 0 ] ).is( "form" ) ) {
  106. valid = this.validate().form();
  107. } else {
  108. errorList = [];
  109. valid = true;
  110. validator = $( this[ 0 ].form ).validate();
  111. this.each( function() {
  112. valid = validator.element( this ) && valid;
  113. if ( !valid ) {
  114. errorList = errorList.concat( validator.errorList );
  115. }
  116. } );
  117. validator.errorList = errorList;
  118. }
  119. return valid;
  120. },
  121. // https://jqueryvalidation.org/rules/
  122. rules: function( command, argument ) {
  123. var element = this[ 0 ],
  124. isContentEditable = typeof this.attr( "contenteditable" ) !== "undefined" && this.attr( "contenteditable" ) !== "false",
  125. settings, staticRules, existingRules, data, param, filtered;
  126. // If nothing is selected, return empty object; can't chain anyway
  127. if ( element == null ) {
  128. return;
  129. }
  130. if ( !element.form && isContentEditable ) {
  131. element.form = this.closest( "form" )[ 0 ];
  132. element.name = this.attr( "name" );
  133. }
  134. if ( element.form == null ) {
  135. return;
  136. }
  137. if ( command ) {
  138. settings = $.data( element.form, "validator" ).settings;
  139. staticRules = settings.rules;
  140. existingRules = $.validator.staticRules( element );
  141. switch ( command ) {
  142. case "add":
  143. $.extend( existingRules, $.validator.normalizeRule( argument ) );
  144. // Remove messages from rules, but allow them to be set separately
  145. delete existingRules.messages;
  146. staticRules[ element.name ] = existingRules;
  147. if ( argument.messages ) {
  148. settings.messages[ element.name ] = $.extend( settings.messages[ element.name ], argument.messages );
  149. }
  150. break;
  151. case "remove":
  152. if ( !argument ) {
  153. delete staticRules[ element.name ];
  154. return existingRules;
  155. }
  156. filtered = {};
  157. $.each( argument.split( /\s/ ), function( index, method ) {
  158. filtered[ method ] = existingRules[ method ];
  159. delete existingRules[ method ];
  160. } );
  161. return filtered;
  162. }
  163. }
  164. data = $.validator.normalizeRules(
  165. $.extend(
  166. {},
  167. $.validator.classRules( element ),
  168. $.validator.attributeRules( element ),
  169. $.validator.dataRules( element ),
  170. $.validator.staticRules( element )
  171. ), element );
  172. // Make sure required is at front
  173. if ( data.required ) {
  174. param = data.required;
  175. delete data.required;
  176. data = $.extend( { required: param }, data );
  177. }
  178. // Make sure remote is at back
  179. if ( data.remote ) {
  180. param = data.remote;
  181. delete data.remote;
  182. data = $.extend( data, { remote: param } );
  183. }
  184. return data;
  185. }
  186. } );
  187. // JQuery trim is deprecated, provide a trim method based on String.prototype.trim
  188. var trim = function( str ) {
  189. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#Polyfill
  190. return str.replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "" );
  191. };
  192. // Custom selectors
  193. $.extend( $.expr.pseudos || $.expr[ ":" ], { // '|| $.expr[ ":" ]' here enables backwards compatibility to jQuery 1.7. Can be removed when dropping jQ 1.7.x support
  194. // https://jqueryvalidation.org/blank-selector/
  195. blank: function( a ) {
  196. return !trim( "" + $( a ).val() );
  197. },
  198. // https://jqueryvalidation.org/filled-selector/
  199. filled: function( a ) {
  200. var val = $( a ).val();
  201. return val !== null && !!trim( "" + val );
  202. },
  203. // https://jqueryvalidation.org/unchecked-selector/
  204. unchecked: function( a ) {
  205. return !$( a ).prop( "checked" );
  206. }
  207. } );
  208. // Constructor for validator
  209. $.validator = function( options, form ) {
  210. this.settings = $.extend( true, {}, $.validator.defaults, options );
  211. this.currentForm = form;
  212. this.init();
  213. };
  214. // https://jqueryvalidation.org/jQuery.validator.format/
  215. $.validator.format = function( source, params ) {
  216. if ( arguments.length === 1 ) {
  217. return function() {
  218. var args = $.makeArray( arguments );
  219. args.unshift( source );
  220. return $.validator.format.apply( this, args );
  221. };
  222. }
  223. if ( params === undefined ) {
  224. return source;
  225. }
  226. if ( arguments.length > 2 && params.constructor !== Array ) {
  227. params = $.makeArray( arguments ).slice( 1 );
  228. }
  229. if ( params.constructor !== Array ) {
  230. params = [ params ];
  231. }
  232. $.each( params, function( i, n ) {
  233. source = source.replace( new RegExp( "\\{" + i + "\\}", "g" ), function() {
  234. return n;
  235. } );
  236. } );
  237. return source;
  238. };
  239. $.extend( $.validator, {
  240. defaults: {
  241. messages: {},
  242. groups: {},
  243. rules: {},
  244. errorClass: "error",
  245. pendingClass: "pending",
  246. validClass: "valid",
  247. errorElement: "label",
  248. focusCleanup: false,
  249. focusInvalid: true,
  250. errorContainer: $( [] ),
  251. errorLabelContainer: $( [] ),
  252. onsubmit: true,
  253. ignore: ":hidden",
  254. ignoreTitle: false,
  255. onfocusin: function( element ) {
  256. this.lastActive = element;
  257. // Hide error label and remove error class on focus if enabled
  258. if ( this.settings.focusCleanup ) {
  259. if ( this.settings.unhighlight ) {
  260. this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.validClass );
  261. }
  262. this.hideThese( this.errorsFor( element ) );
  263. }
  264. },
  265. onfocusout: function( element ) {
  266. if ( !this.checkable( element ) && ( element.name in this.submitted || !this.optional( element ) ) ) {
  267. this.element( element );
  268. }
  269. },
  270. onkeyup: function( element, event ) {
  271. // Avoid revalidate the field when pressing one of the following keys
  272. // Shift => 16
  273. // Ctrl => 17
  274. // Alt => 18
  275. // Caps lock => 20
  276. // End => 35
  277. // Home => 36
  278. // Left arrow => 37
  279. // Up arrow => 38
  280. // Right arrow => 39
  281. // Down arrow => 40
  282. // Insert => 45
  283. // Num lock => 144
  284. // AltGr key => 225
  285. var excludedKeys = [
  286. 16, 17, 18, 20, 35, 36, 37,
  287. 38, 39, 40, 45, 144, 225
  288. ];
  289. if ( event.which === 9 && this.elementValue( element ) === "" || $.inArray( event.keyCode, excludedKeys ) !== -1 ) {
  290. return;
  291. } else if ( element.name in this.submitted || element.name in this.invalid ) {
  292. this.element( element );
  293. }
  294. },
  295. onclick: function( element ) {
  296. // Click on selects, radiobuttons and checkboxes
  297. if ( element.name in this.submitted ) {
  298. this.element( element );
  299. // Or option elements, check parent select in that case
  300. } else if ( element.parentNode.name in this.submitted ) {
  301. this.element( element.parentNode );
  302. }
  303. },
  304. highlight: function( element, errorClass, validClass ) {
  305. if ( element.type === "radio" ) {
  306. this.findByName( element.name ).addClass( errorClass ).removeClass( validClass );
  307. } else {
  308. $( element ).addClass( errorClass ).removeClass( validClass );
  309. }
  310. },
  311. unhighlight: function( element, errorClass, validClass ) {
  312. if ( element.type === "radio" ) {
  313. this.findByName( element.name ).removeClass( errorClass ).addClass( validClass );
  314. } else {
  315. $( element ).removeClass( errorClass ).addClass( validClass );
  316. }
  317. }
  318. },
  319. // https://jqueryvalidation.org/jQuery.validator.setDefaults/
  320. setDefaults: function( settings ) {
  321. $.extend( $.validator.defaults, settings );
  322. },
  323. messages: {
  324. required: "This field is required.",
  325. remote: "Please fix this field.",
  326. email: "Please enter a valid email address.",
  327. url: "Please enter a valid URL.",
  328. date: "Please enter a valid date.",
  329. dateISO: "Please enter a valid date (ISO).",
  330. number: "Please enter a valid number.",
  331. digits: "Please enter only digits.",
  332. equalTo: "Please enter the same value again.",
  333. maxlength: $.validator.format( "Please enter no more than {0} characters." ),
  334. minlength: $.validator.format( "Please enter at least {0} characters." ),
  335. rangelength: $.validator.format( "Please enter a value between {0} and {1} characters long." ),
  336. range: $.validator.format( "Please enter a value between {0} and {1}." ),
  337. max: $.validator.format( "Please enter a value less than or equal to {0}." ),
  338. min: $.validator.format( "Please enter a value greater than or equal to {0}." ),
  339. step: $.validator.format( "Please enter a multiple of {0}." )
  340. },
  341. autoCreateRanges: false,
  342. prototype: {
  343. init: function() {
  344. this.labelContainer = $( this.settings.errorLabelContainer );
  345. this.errorContext = this.labelContainer.length && this.labelContainer || $( this.currentForm );
  346. this.containers = $( this.settings.errorContainer ).add( this.settings.errorLabelContainer );
  347. this.submitted = {};
  348. this.valueCache = {};
  349. this.pendingRequest = 0;
  350. this.pending = {};
  351. this.invalid = {};
  352. this.reset();
  353. var currentForm = this.currentForm,
  354. groups = ( this.groups = {} ),
  355. rules;
  356. $.each( this.settings.groups, function( key, value ) {
  357. if ( typeof value === "string" ) {
  358. value = value.split( /\s/ );
  359. }
  360. $.each( value, function( index, name ) {
  361. groups[ name ] = key;
  362. } );
  363. } );
  364. rules = this.settings.rules;
  365. $.each( rules, function( key, value ) {
  366. rules[ key ] = $.validator.normalizeRule( value );
  367. } );
  368. function delegate( event ) {
  369. var isContentEditable = typeof $( this ).attr( "contenteditable" ) !== "undefined" && $( this ).attr( "contenteditable" ) !== "false";
  370. // Set form expando on contenteditable
  371. if ( !this.form && isContentEditable ) {
  372. this.form = $( this ).closest( "form" )[ 0 ];
  373. this.name = $( this ).attr( "name" );
  374. }
  375. // Ignore the element if it belongs to another form. This will happen mainly
  376. // when setting the `form` attribute of an input to the id of another form.
  377. if ( currentForm !== this.form ) {
  378. return;
  379. }
  380. var validator = $.data( this.form, "validator" ),
  381. eventType = "on" + event.type.replace( /^validate/, "" ),
  382. settings = validator.settings;
  383. if ( settings[ eventType ] && !$( this ).is( settings.ignore ) ) {
  384. settings[ eventType ].call( validator, this, event );
  385. }
  386. }
  387. $( this.currentForm )
  388. .on( "focusin.validate focusout.validate keyup.validate",
  389. ":text, [type='password'], [type='file'], select, textarea, [type='number'], [type='search'], " +
  390. "[type='tel'], [type='url'], [type='email'], [type='datetime'], [type='date'], [type='month'], " +
  391. "[type='week'], [type='time'], [type='datetime-local'], [type='range'], [type='color'], " +
  392. "[type='radio'], [type='checkbox'], [contenteditable], [type='button']", delegate )
  393. // Support: Chrome, oldIE
  394. // "select" is provided as event.target when clicking a option
  395. .on( "click.validate", "select, option, [type='radio'], [type='checkbox']", delegate );
  396. if ( this.settings.invalidHandler ) {
  397. $( this.currentForm ).on( "invalid-form.validate", this.settings.invalidHandler );
  398. }
  399. },
  400. // https://jqueryvalidation.org/Validator.form/
  401. form: function() {
  402. this.checkForm();
  403. $.extend( this.submitted, this.errorMap );
  404. this.invalid = $.extend( {}, this.errorMap );
  405. if ( !this.valid() ) {
  406. $( this.currentForm ).triggerHandler( "invalid-form", [ this ] );
  407. }
  408. this.showErrors();
  409. return this.valid();
  410. },
  411. checkForm: function() {
  412. this.prepareForm();
  413. for ( var i = 0, elements = ( this.currentElements = this.elements() ); elements[ i ]; i++ ) {
  414. this.check( elements[ i ] );
  415. }
  416. return this.valid();
  417. },
  418. // https://jqueryvalidation.org/Validator.element/
  419. element: function( element ) {
  420. var cleanElement = this.clean( element ),
  421. checkElement = this.validationTargetFor( cleanElement ),
  422. v = this,
  423. result = true,
  424. rs, group;
  425. if ( checkElement === undefined ) {
  426. delete this.invalid[ cleanElement.name ];
  427. } else {
  428. this.prepareElement( checkElement );
  429. this.currentElements = $( checkElement );
  430. // If this element is grouped, then validate all group elements already
  431. // containing a value
  432. group = this.groups[ checkElement.name ];
  433. if ( group ) {
  434. $.each( this.groups, function( name, testgroup ) {
  435. if ( testgroup === group && name !== checkElement.name ) {
  436. cleanElement = v.validationTargetFor( v.clean( v.findByName( name ) ) );
  437. if ( cleanElement && cleanElement.name in v.invalid ) {
  438. v.currentElements.push( cleanElement );
  439. result = v.check( cleanElement ) && result;
  440. }
  441. }
  442. } );
  443. }
  444. rs = this.check( checkElement ) !== false;
  445. result = result && rs;
  446. if ( rs ) {
  447. this.invalid[ checkElement.name ] = false;
  448. } else {
  449. this.invalid[ checkElement.name ] = true;
  450. }
  451. if ( !this.numberOfInvalids() ) {
  452. // Hide error containers on last error
  453. this.toHide = this.toHide.add( this.containers );
  454. }
  455. this.showErrors();
  456. // Add aria-invalid status for screen readers
  457. $( element ).attr( "aria-invalid", !rs );
  458. }
  459. return result;
  460. },
  461. // https://jqueryvalidation.org/Validator.showErrors/
  462. showErrors: function( errors ) {
  463. if ( errors ) {
  464. var validator = this;
  465. // Add items to error list and map
  466. $.extend( this.errorMap, errors );
  467. this.errorList = $.map( this.errorMap, function( message, name ) {
  468. return {
  469. message: message,
  470. element: validator.findByName( name )[ 0 ]
  471. };
  472. } );
  473. // Remove items from success list
  474. this.successList = $.grep( this.successList, function( element ) {
  475. return !( element.name in errors );
  476. } );
  477. }
  478. if ( this.settings.showErrors ) {
  479. this.settings.showErrors.call( this, this.errorMap, this.errorList );
  480. } else {
  481. this.defaultShowErrors();
  482. }
  483. },
  484. // https://jqueryvalidation.org/Validator.resetForm/
  485. resetForm: function() {
  486. if ( $.fn.resetForm ) {
  487. $( this.currentForm ).resetForm();
  488. }
  489. this.invalid = {};
  490. this.submitted = {};
  491. this.prepareForm();
  492. this.hideErrors();
  493. var elements = this.elements()
  494. .removeData( "previousValue" )
  495. .removeAttr( "aria-invalid" );
  496. this.resetElements( elements );
  497. },
  498. resetElements: function( elements ) {
  499. var i;
  500. if ( this.settings.unhighlight ) {
  501. for ( i = 0; elements[ i ]; i++ ) {
  502. this.settings.unhighlight.call( this, elements[ i ],
  503. this.settings.errorClass, "" );
  504. this.findByName( elements[ i ].name ).removeClass( this.settings.validClass );
  505. }
  506. } else {
  507. elements
  508. .removeClass( this.settings.errorClass )
  509. .removeClass( this.settings.validClass );
  510. }
  511. },
  512. numberOfInvalids: function() {
  513. return this.objectLength( this.invalid );
  514. },
  515. objectLength: function( obj ) {
  516. /* jshint unused: false */
  517. var count = 0,
  518. i;
  519. for ( i in obj ) {
  520. // This check allows counting elements with empty error
  521. // message as invalid elements
  522. if ( obj[ i ] !== undefined && obj[ i ] !== null && obj[ i ] !== false ) {
  523. count++;
  524. }
  525. }
  526. return count;
  527. },
  528. hideErrors: function() {
  529. this.hideThese( this.toHide );
  530. },
  531. hideThese: function( errors ) {
  532. errors.not( this.containers ).text( "" );
  533. this.addWrapper( errors ).hide();
  534. },
  535. valid: function() {
  536. return this.size() === 0;
  537. },
  538. size: function() {
  539. return this.errorList.length;
  540. },
  541. focusInvalid: function() {
  542. if ( this.settings.focusInvalid ) {
  543. try {
  544. $( this.findLastActive() || this.errorList.length && this.errorList[ 0 ].element || [] )
  545. .filter( ":visible" )
  546. .trigger( "focus" )
  547. // Manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
  548. .trigger( "focusin" );
  549. } catch ( e ) {
  550. // Ignore IE throwing errors when focusing hidden elements
  551. }
  552. }
  553. },
  554. findLastActive: function() {
  555. var lastActive = this.lastActive;
  556. return lastActive && $.grep( this.errorList, function( n ) {
  557. return n.element.name === lastActive.name;
  558. } ).length === 1 && lastActive;
  559. },
  560. elements: function() {
  561. var validator = this,
  562. rulesCache = {};
  563. // Select all valid inputs inside the form (no submit or reset buttons)
  564. return $( this.currentForm )
  565. .find( "input, select, textarea, [contenteditable]" )
  566. .not( ":submit, :reset, :image, :disabled" )
  567. .not( this.settings.ignore )
  568. .filter( function() {
  569. var name = this.name || $( this ).attr( "name" ); // For contenteditable
  570. var isContentEditable = typeof $( this ).attr( "contenteditable" ) !== "undefined" && $( this ).attr( "contenteditable" ) !== "false";
  571. if ( !name && validator.settings.debug && window.console ) {
  572. console.error( "%o has no name assigned", this );
  573. }
  574. // Set form expando on contenteditable
  575. if ( isContentEditable ) {
  576. this.form = $( this ).closest( "form" )[ 0 ];
  577. this.name = name;
  578. }
  579. // Ignore elements that belong to other/nested forms
  580. if ( this.form !== validator.currentForm ) {
  581. return false;
  582. }
  583. // Select only the first element for each name, and only those with rules specified
  584. if ( name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
  585. return false;
  586. }
  587. rulesCache[ name ] = true;
  588. return true;
  589. } );
  590. },
  591. clean: function( selector ) {
  592. return $( selector )[ 0 ];
  593. },
  594. errors: function() {
  595. var errorClass = this.settings.errorClass.split( " " ).join( "." );
  596. return $( this.settings.errorElement + "." + errorClass, this.errorContext );
  597. },
  598. resetInternals: function() {
  599. this.successList = [];
  600. this.errorList = [];
  601. this.errorMap = {};
  602. this.toShow = $( [] );
  603. this.toHide = $( [] );
  604. },
  605. reset: function() {
  606. this.resetInternals();
  607. this.currentElements = $( [] );
  608. },
  609. prepareForm: function() {
  610. this.reset();
  611. this.toHide = this.errors().add( this.containers );
  612. },
  613. prepareElement: function( element ) {
  614. this.reset();
  615. this.toHide = this.errorsFor( element );
  616. },
  617. elementValue: function( element ) {
  618. var $element = $( element ),
  619. type = element.type,
  620. isContentEditable = typeof $element.attr( "contenteditable" ) !== "undefined" && $element.attr( "contenteditable" ) !== "false",
  621. val, idx;
  622. if ( type === "radio" || type === "checkbox" ) {
  623. return this.findByName( element.name ).filter( ":checked" ).val();
  624. } else if ( type === "number" && typeof element.validity !== "undefined" ) {
  625. return element.validity.badInput ? "NaN" : $element.val();
  626. }
  627. if ( isContentEditable ) {
  628. val = $element.text();
  629. } else {
  630. val = $element.val();
  631. }
  632. if ( type === "file" ) {
  633. // Modern browser (chrome & safari)
  634. if ( val.substr( 0, 12 ) === "C:\\fakepath\\" ) {
  635. return val.substr( 12 );
  636. }
  637. // Legacy browsers
  638. // Unix-based path
  639. idx = val.lastIndexOf( "/" );
  640. if ( idx >= 0 ) {
  641. return val.substr( idx + 1 );
  642. }
  643. // Windows-based path
  644. idx = val.lastIndexOf( "\\" );
  645. if ( idx >= 0 ) {
  646. return val.substr( idx + 1 );
  647. }
  648. // Just the file name
  649. return val;
  650. }
  651. if ( typeof val === "string" ) {
  652. return val.replace( /\r/g, "" );
  653. }
  654. return val;
  655. },
  656. check: function( element ) {
  657. element = this.validationTargetFor( this.clean( element ) );
  658. var rules = $( element ).rules(),
  659. rulesCount = $.map( rules, function( n, i ) {
  660. return i;
  661. } ).length,
  662. dependencyMismatch = false,
  663. val = this.elementValue( element ),
  664. result, method, rule, normalizer;
  665. // Prioritize the local normalizer defined for this element over the global one
  666. // if the former exists, otherwise user the global one in case it exists.
  667. if ( typeof rules.normalizer === "function" ) {
  668. normalizer = rules.normalizer;
  669. } else if ( typeof this.settings.normalizer === "function" ) {
  670. normalizer = this.settings.normalizer;
  671. }
  672. // If normalizer is defined, then call it to retreive the changed value instead
  673. // of using the real one.
  674. // Note that `this` in the normalizer is `element`.
  675. if ( normalizer ) {
  676. val = normalizer.call( element, val );
  677. // Delete the normalizer from rules to avoid treating it as a pre-defined method.
  678. delete rules.normalizer;
  679. }
  680. for ( method in rules ) {
  681. rule = { method: method, parameters: rules[ method ] };
  682. try {
  683. result = $.validator.methods[ method ].call( this, val, element, rule.parameters );
  684. // If a method indicates that the field is optional and therefore valid,
  685. // don't mark it as valid when there are no other rules
  686. if ( result === "dependency-mismatch" && rulesCount === 1 ) {
  687. dependencyMismatch = true;
  688. continue;
  689. }
  690. dependencyMismatch = false;
  691. if ( result === "pending" ) {
  692. this.toHide = this.toHide.not( this.errorsFor( element ) );
  693. return;
  694. }
  695. if ( !result ) {
  696. this.formatAndAdd( element, rule );
  697. return false;
  698. }
  699. } catch ( e ) {
  700. if ( this.settings.debug && window.console ) {
  701. console.log( "Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e );
  702. }
  703. if ( e instanceof TypeError ) {
  704. e.message += ". Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.";
  705. }
  706. throw e;
  707. }
  708. }
  709. if ( dependencyMismatch ) {
  710. return;
  711. }
  712. if ( this.objectLength( rules ) ) {
  713. this.successList.push( element );
  714. }
  715. return true;
  716. },
  717. // Return the custom message for the given element and validation method
  718. // specified in the element's HTML5 data attribute
  719. // return the generic message if present and no method specific message is present
  720. customDataMessage: function( element, method ) {
  721. return $( element ).data( "msg" + method.charAt( 0 ).toUpperCase() +
  722. method.substring( 1 ).toLowerCase() ) || $( element ).data( "msg" );
  723. },
  724. // Return the custom message for the given element name and validation method
  725. customMessage: function( name, method ) {
  726. var m = this.settings.messages[ name ];
  727. return m && ( m.constructor === String ? m : m[ method ] );
  728. },
  729. // Return the first defined argument, allowing empty strings
  730. findDefined: function() {
  731. for ( var i = 0; i < arguments.length; i++ ) {
  732. if ( arguments[ i ] !== undefined ) {
  733. return arguments[ i ];
  734. }
  735. }
  736. return undefined;
  737. },
  738. // The second parameter 'rule' used to be a string, and extended to an object literal
  739. // of the following form:
  740. // rule = {
  741. // method: "method name",
  742. // parameters: "the given method parameters"
  743. // }
  744. //
  745. // The old behavior still supported, kept to maintain backward compatibility with
  746. // old code, and will be removed in the next major release.
  747. defaultMessage: function( element, rule ) {
  748. if ( typeof rule === "string" ) {
  749. rule = { method: rule };
  750. }
  751. var message = this.findDefined(
  752. this.customMessage( element.name, rule.method ),
  753. this.customDataMessage( element, rule.method ),
  754. // 'title' is never undefined, so handle empty string as undefined
  755. !this.settings.ignoreTitle && element.title || undefined,
  756. $.validator.messages[ rule.method ],
  757. "<strong>Warning: No message defined for " + element.name + "</strong>"
  758. ),
  759. theregex = /\$?\{(\d+)\}/g;
  760. if ( typeof message === "function" ) {
  761. message = message.call( this, rule.parameters, element );
  762. } else if ( theregex.test( message ) ) {
  763. message = $.validator.format( message.replace( theregex, "{$1}" ), rule.parameters );
  764. }
  765. return message;
  766. },
  767. formatAndAdd: function( element, rule ) {
  768. var message = this.defaultMessage( element, rule );
  769. this.errorList.push( {
  770. message: message,
  771. element: element,
  772. method: rule.method
  773. } );
  774. this.errorMap[ element.name ] = message;
  775. this.submitted[ element.name ] = message;
  776. },
  777. addWrapper: function( toToggle ) {
  778. if ( this.settings.wrapper ) {
  779. toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) );
  780. }
  781. return toToggle;
  782. },
  783. defaultShowErrors: function() {
  784. var i, elements, error;
  785. for ( i = 0; this.errorList[ i ]; i++ ) {
  786. error = this.errorList[ i ];
  787. if ( this.settings.highlight ) {
  788. this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
  789. }
  790. this.showLabel( error.element, error.message );
  791. }
  792. if ( this.errorList.length ) {
  793. this.toShow = this.toShow.add( this.containers );
  794. }
  795. if ( this.settings.success ) {
  796. for ( i = 0; this.successList[ i ]; i++ ) {
  797. this.showLabel( this.successList[ i ] );
  798. }
  799. }
  800. if ( this.settings.unhighlight ) {
  801. for ( i = 0, elements = this.validElements(); elements[ i ]; i++ ) {
  802. this.settings.unhighlight.call( this, elements[ i ], this.settings.errorClass, this.settings.validClass );
  803. }
  804. }
  805. this.toHide = this.toHide.not( this.toShow );
  806. this.hideErrors();
  807. this.addWrapper( this.toShow ).show();
  808. },
  809. validElements: function() {
  810. return this.currentElements.not( this.invalidElements() );
  811. },
  812. invalidElements: function() {
  813. return $( this.errorList ).map( function() {
  814. return this.element;
  815. } );
  816. },
  817. showLabel: function( element, message ) {
  818. var place, group, errorID, v,
  819. error = this.errorsFor( element ),
  820. elementID = this.idOrName( element ),
  821. describedBy = $( element ).attr( "aria-describedby" );
  822. if ( error.length ) {
  823. // Refresh error/success class
  824. error.removeClass( this.settings.validClass ).addClass( this.settings.errorClass );
  825. // Replace message on existing label
  826. error.html( message );
  827. } else {
  828. // Create error element
  829. error = $( "<" + this.settings.errorElement + ">" )
  830. .attr( "id", elementID + "-error" )
  831. .addClass( this.settings.errorClass )
  832. .html( message || "" );
  833. // Maintain reference to the element to be placed into the DOM
  834. place = error;
  835. if ( this.settings.wrapper ) {
  836. // Make sure the element is visible, even in IE
  837. // actually showing the wrapped element is handled elsewhere
  838. place = error.hide().show().wrap( "<" + this.settings.wrapper + "/>" ).parent();
  839. }
  840. if ( this.labelContainer.length ) {
  841. this.labelContainer.append( place );
  842. } else if ( this.settings.errorPlacement ) {
  843. this.settings.errorPlacement.call( this, place, $( element ) );
  844. } else {
  845. place.insertAfter( element );
  846. }
  847. // Link error back to the element
  848. if ( error.is( "label" ) ) {
  849. // If the error is a label, then associate using 'for'
  850. error.attr( "for", elementID );
  851. // If the element is not a child of an associated label, then it's necessary
  852. // to explicitly apply aria-describedby
  853. } else if ( error.parents( "label[for='" + this.escapeCssMeta( elementID ) + "']" ).length === 0 ) {
  854. errorID = error.attr( "id" );
  855. // Respect existing non-error aria-describedby
  856. if ( !describedBy ) {
  857. describedBy = errorID;
  858. } else if ( !describedBy.match( new RegExp( "\\b" + this.escapeCssMeta( errorID ) + "\\b" ) ) ) {
  859. // Add to end of list if not already present
  860. describedBy += " " + errorID;
  861. }
  862. $( element ).attr( "aria-describedby", describedBy );
  863. // If this element is grouped, then assign to all elements in the same group
  864. group = this.groups[ element.name ];
  865. if ( group ) {
  866. v = this;
  867. $.each( v.groups, function( name, testgroup ) {
  868. if ( testgroup === group ) {
  869. $( "[name='" + v.escapeCssMeta( name ) + "']", v.currentForm )
  870. .attr( "aria-describedby", error.attr( "id" ) );
  871. }
  872. } );
  873. }
  874. }
  875. }
  876. if ( !message && this.settings.success ) {
  877. error.text( "" );
  878. if ( typeof this.settings.success === "string" ) {
  879. error.addClass( this.settings.success );
  880. } else {
  881. this.settings.success( error, element );
  882. }
  883. }
  884. this.toShow = this.toShow.add( error );
  885. },
  886. errorsFor: function( element ) {
  887. var name = this.escapeCssMeta( this.idOrName( element ) ),
  888. describer = $( element ).attr( "aria-describedby" ),
  889. selector = "label[for='" + name + "'], label[for='" + name + "'] *";
  890. // 'aria-describedby' should directly reference the error element
  891. if ( describer ) {
  892. selector = selector + ", #" + this.escapeCssMeta( describer )
  893. .replace( /\s+/g, ", #" );
  894. }
  895. return this
  896. .errors()
  897. .filter( selector );
  898. },
  899. // See https://api.jquery.com/category/selectors/, for CSS
  900. // meta-characters that should be escaped in order to be used with JQuery
  901. // as a literal part of a name/id or any selector.
  902. escapeCssMeta: function( string ) {
  903. return string.replace( /([\\!"#$%&'()*+,./:;<=>?@\[\]^`{|}~])/g, "\\$1" );
  904. },
  905. idOrName: function( element ) {
  906. return this.groups[ element.name ] || ( this.checkable( element ) ? element.name : element.id || element.name );
  907. },
  908. validationTargetFor: function( element ) {
  909. // If radio/checkbox, validate first element in group instead
  910. if ( this.checkable( element ) ) {
  911. element = this.findByName( element.name );
  912. }
  913. // Always apply ignore filter
  914. return $( element ).not( this.settings.ignore )[ 0 ];
  915. },
  916. checkable: function( element ) {
  917. return ( /radio|checkbox/i ).test( element.type );
  918. },
  919. findByName: function( name ) {
  920. return $( this.currentForm ).find( "[name='" + this.escapeCssMeta( name ) + "']" );
  921. },
  922. getLength: function( value, element ) {
  923. switch ( element.nodeName.toLowerCase() ) {
  924. case "select":
  925. return $( "option:selected", element ).length;
  926. case "input":
  927. if ( this.checkable( element ) ) {
  928. return this.findByName( element.name ).filter( ":checked" ).length;
  929. }
  930. }
  931. return value.length;
  932. },
  933. depend: function( param, element ) {
  934. return this.dependTypes[ typeof param ] ? this.dependTypes[ typeof param ]( param, element ) : true;
  935. },
  936. dependTypes: {
  937. "boolean": function( param ) {
  938. return param;
  939. },
  940. "string": function( param, element ) {
  941. return !!$( param, element.form ).length;
  942. },
  943. "function": function( param, element ) {
  944. return param( element );
  945. }
  946. },
  947. optional: function( element ) {
  948. var val = this.elementValue( element );
  949. return !$.validator.methods.required.call( this, val, element ) && "dependency-mismatch";
  950. },
  951. startRequest: function( element ) {
  952. if ( !this.pending[ element.name ] ) {
  953. this.pendingRequest++;
  954. $( element ).addClass( this.settings.pendingClass );
  955. this.pending[ element.name ] = true;
  956. }
  957. },
  958. stopRequest: function( element, valid ) {
  959. this.pendingRequest--;
  960. // Sometimes synchronization fails, make sure pendingRequest is never < 0
  961. if ( this.pendingRequest < 0 ) {
  962. this.pendingRequest = 0;
  963. }
  964. delete this.pending[ element.name ];
  965. $( element ).removeClass( this.settings.pendingClass );
  966. if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) {
  967. $( this.currentForm ).submit();
  968. // Remove the hidden input that was used as a replacement for the
  969. // missing submit button. The hidden input is added by `handle()`
  970. // to ensure that the value of the used submit button is passed on
  971. // for scripted submits triggered by this method
  972. if ( this.submitButton ) {
  973. $( "input:hidden[name='" + this.submitButton.name + "']", this.currentForm ).remove();
  974. }
  975. this.formSubmitted = false;
  976. } else if ( !valid && this.pendingRequest === 0 && this.formSubmitted ) {
  977. $( this.currentForm ).triggerHandler( "invalid-form", [ this ] );
  978. this.formSubmitted = false;
  979. }
  980. },
  981. previousValue: function( element, method ) {
  982. method = typeof method === "string" && method || "remote";
  983. return $.data( element, "previousValue" ) || $.data( element, "previousValue", {
  984. old: null,
  985. valid: true,
  986. message: this.defaultMessage( element, { method: method } )
  987. } );
  988. },
  989. // Cleans up all forms and elements, removes validator-specific events
  990. destroy: function() {
  991. this.resetForm();
  992. $( this.currentForm )
  993. .off( ".validate" )
  994. .removeData( "validator" )
  995. .find( ".validate-equalTo-blur" )
  996. .off( ".validate-equalTo" )
  997. .removeClass( "validate-equalTo-blur" )
  998. .find( ".validate-lessThan-blur" )
  999. .off( ".validate-lessThan" )
  1000. .removeClass( "validate-lessThan-blur" )
  1001. .find( ".validate-lessThanEqual-blur" )
  1002. .off( ".validate-lessThanEqual" )
  1003. .removeClass( "validate-lessThanEqual-blur" )
  1004. .find( ".validate-greaterThanEqual-blur" )
  1005. .off( ".validate-greaterThanEqual" )
  1006. .removeClass( "validate-greaterThanEqual-blur" )
  1007. .find( ".validate-greaterThan-blur" )
  1008. .off( ".validate-greaterThan" )
  1009. .removeClass( "validate-greaterThan-blur" );
  1010. }
  1011. },
  1012. classRuleSettings: {
  1013. required: { required: true },
  1014. email: { email: true },
  1015. url: { url: true },
  1016. date: { date: true },
  1017. dateISO: { dateISO: true },
  1018. number: { number: true },
  1019. digits: { digits: true },
  1020. creditcard: { creditcard: true }
  1021. },
  1022. addClassRules: function( className, rules ) {
  1023. if ( className.constructor === String ) {
  1024. this.classRuleSettings[ className ] = rules;
  1025. } else {
  1026. $.extend( this.classRuleSettings, className );
  1027. }
  1028. },
  1029. classRules: function( element ) {
  1030. var rules = {},
  1031. classes = $( element ).attr( "class" );
  1032. if ( classes ) {
  1033. $.each( classes.split( " " ), function() {
  1034. if ( this in $.validator.classRuleSettings ) {
  1035. $.extend( rules, $.validator.classRuleSettings[ this ] );
  1036. }
  1037. } );
  1038. }
  1039. return rules;
  1040. },
  1041. normalizeAttributeRule: function( rules, type, method, value ) {
  1042. // Convert the value to a number for number inputs, and for text for backwards compability
  1043. // allows type="date" and others to be compared as strings
  1044. if ( /min|max|step/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
  1045. value = Number( value );
  1046. // Support Opera Mini, which returns NaN for undefined minlength
  1047. if ( isNaN( value ) ) {
  1048. value = undefined;
  1049. }
  1050. }
  1051. if ( value || value === 0 ) {
  1052. rules[ method ] = value;
  1053. } else if ( type === method && type !== "range" ) {
  1054. // Exception: the jquery validate 'range' method
  1055. // does not test for the html5 'range' type
  1056. rules[ method ] = true;
  1057. }
  1058. },
  1059. attributeRules: function( element ) {
  1060. var rules = {},
  1061. $element = $( element ),
  1062. type = element.getAttribute( "type" ),
  1063. method, value;
  1064. for ( method in $.validator.methods ) {
  1065. // Support for <input required> in both html5 and older browsers
  1066. if ( method === "required" ) {
  1067. value = element.getAttribute( method );
  1068. // Some browsers return an empty string for the required attribute
  1069. // and non-HTML5 browsers might have required="" markup
  1070. if ( value === "" ) {
  1071. value = true;
  1072. }
  1073. // Force non-HTML5 browsers to return bool
  1074. value = !!value;
  1075. } else {
  1076. value = $element.attr( method );
  1077. }
  1078. this.normalizeAttributeRule( rules, type, method, value );
  1079. }
  1080. // 'maxlength' may be returned as -1, 2147483647 ( IE ) and 524288 ( safari ) for text inputs
  1081. if ( rules.maxlength && /-1|2147483647|524288/.test( rules.maxlength ) ) {
  1082. delete rules.maxlength;
  1083. }
  1084. return rules;
  1085. },
  1086. dataRules: function( element ) {
  1087. var rules = {},
  1088. $element = $( element ),
  1089. type = element.getAttribute( "type" ),
  1090. method, value;
  1091. for ( method in $.validator.methods ) {
  1092. value = $element.data( "rule" + method.charAt( 0 ).toUpperCase() + method.substring( 1 ).toLowerCase() );
  1093. // Cast empty attributes like `data-rule-required` to `true`
  1094. if ( value === "" ) {
  1095. value = true;
  1096. }
  1097. this.normalizeAttributeRule( rules, type, method, value );
  1098. }
  1099. return rules;
  1100. },
  1101. staticRules: function( element ) {
  1102. var rules = {},
  1103. validator = $.data( element.form, "validator" );
  1104. if ( validator.settings.rules ) {
  1105. rules = $.validator.normalizeRule( validator.settings.rules[ element.name ] ) || {};
  1106. }
  1107. return rules;
  1108. },
  1109. normalizeRules: function( rules, element ) {
  1110. // Handle dependency check
  1111. $.each( rules, function( prop, val ) {
  1112. // Ignore rule when param is explicitly false, eg. required:false
  1113. if ( val === false ) {
  1114. delete rules[ prop ];
  1115. return;
  1116. }
  1117. if ( val.param || val.depends ) {
  1118. var keepRule = true;
  1119. switch ( typeof val.depends ) {
  1120. case "string":
  1121. keepRule = !!$( val.depends, element.form ).length;
  1122. break;
  1123. case "function":
  1124. keepRule = val.depends.call( element, element );
  1125. break;
  1126. }
  1127. if ( keepRule ) {
  1128. rules[ prop ] = val.param !== undefined ? val.param : true;
  1129. } else {
  1130. $.data( element.form, "validator" ).resetElements( $( element ) );
  1131. delete rules[ prop ];
  1132. }
  1133. }
  1134. } );
  1135. // Evaluate parameters
  1136. $.each( rules, function( rule, parameter ) {
  1137. rules[ rule ] = typeof parameter === "function" && rule !== "normalizer" ? parameter( element ) : parameter;
  1138. } );
  1139. // Clean number parameters
  1140. $.each( [ "minlength", "maxlength" ], function() {
  1141. if ( rules[ this ] ) {
  1142. rules[ this ] = Number( rules[ this ] );
  1143. }
  1144. } );
  1145. $.each( [ "rangelength", "range" ], function() {
  1146. var parts;
  1147. if ( rules[ this ] ) {
  1148. if ( Array.isArray( rules[ this ] ) ) {
  1149. rules[ this ] = [ Number( rules[ this ][ 0 ] ), Number( rules[ this ][ 1 ] ) ];
  1150. } else if ( typeof rules[ this ] === "string" ) {
  1151. parts = rules[ this ].replace( /[\[\]]/g, "" ).split( /[\s,]+/ );
  1152. rules[ this ] = [ Number( parts[ 0 ] ), Number( parts[ 1 ] ) ];
  1153. }
  1154. }
  1155. } );
  1156. if ( $.validator.autoCreateRanges ) {
  1157. // Auto-create ranges
  1158. if ( rules.min != null && rules.max != null ) {
  1159. rules.range = [ rules.min, rules.max ];
  1160. delete rules.min;
  1161. delete rules.max;
  1162. }
  1163. if ( rules.minlength != null && rules.maxlength != null ) {
  1164. rules.rangelength = [ rules.minlength, rules.maxlength ];
  1165. delete rules.minlength;
  1166. delete rules.maxlength;
  1167. }
  1168. }
  1169. return rules;
  1170. },
  1171. // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
  1172. normalizeRule: function( data ) {
  1173. if ( typeof data === "string" ) {
  1174. var transformed = {};
  1175. $.each( data.split( /\s/ ), function() {
  1176. transformed[ this ] = true;
  1177. } );
  1178. data = transformed;
  1179. }
  1180. return data;
  1181. },
  1182. // https://jqueryvalidation.org/jQuery.validator.addMethod/
  1183. addMethod: function( name, method, message ) {
  1184. $.validator.methods[ name ] = method;
  1185. $.validator.messages[ name ] = message !== undefined ? message : $.validator.messages[ name ];
  1186. if ( method.length < 3 ) {
  1187. $.validator.addClassRules( name, $.validator.normalizeRule( name ) );
  1188. }
  1189. },
  1190. // https://jqueryvalidation.org/jQuery.validator.methods/
  1191. methods: {
  1192. // https://jqueryvalidation.org/required-method/
  1193. required: function( value, element, param ) {
  1194. // Check if dependency is met
  1195. if ( !this.depend( param, element ) ) {
  1196. return "dependency-mismatch";
  1197. }
  1198. if ( element.nodeName.toLowerCase() === "select" ) {
  1199. // Could be an array for select-multiple or a string, both are fine this way
  1200. var val = $( element ).val();
  1201. return val && val.length > 0;
  1202. }
  1203. if ( this.checkable( element ) ) {
  1204. return this.getLength( value, element ) > 0;
  1205. }
  1206. return value !== undefined && value !== null && value.length > 0;
  1207. },
  1208. // https://jqueryvalidation.org/email-method/
  1209. email: function( value, element ) {
  1210. // From https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
  1211. // Retrieved 2014-01-14
  1212. // If you have a problem with this implementation, report a bug against the above spec
  1213. // Or use custom methods to implement your own email validation
  1214. return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );
  1215. },
  1216. // https://jqueryvalidation.org/url-method/
  1217. url: function( value, element ) {
  1218. // Copyright (c) 2010-2013 Diego Perini, MIT licensed
  1219. // https://gist.github.com/dperini/729294
  1220. // see also https://mathiasbynens.be/demo/url-regex
  1221. // modified to allow protocol-relative URLs
  1222. return this.optional( element ) || /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( value );
  1223. },
  1224. // https://jqueryvalidation.org/date-method/
  1225. date: ( function() {
  1226. var called = false;
  1227. return function( value, element ) {
  1228. if ( !called ) {
  1229. called = true;
  1230. if ( this.settings.debug && window.console ) {
  1231. console.warn(
  1232. "The `date` method is deprecated and will be removed in version '2.0.0'.\n" +
  1233. "Please don't use it, since it relies on the Date constructor, which\n" +
  1234. "behaves very differently across browsers and locales. Use `dateISO`\n" +
  1235. "instead or one of the locale specific methods in `localizations/`\n" +
  1236. "and `additional-methods.js`."
  1237. );
  1238. }
  1239. }
  1240. return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() );
  1241. };
  1242. }() ),
  1243. // https://jqueryvalidation.org/dateISO-method/
  1244. dateISO: function( value, element ) {
  1245. return this.optional( element ) || /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test( value );
  1246. },
  1247. // https://jqueryvalidation.org/number-method/
  1248. number: function( value, element ) {
  1249. return this.optional( element ) || /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( value );
  1250. },
  1251. // https://jqueryvalidation.org/digits-method/
  1252. digits: function( value, element ) {
  1253. return this.optional( element ) || /^\d+$/.test( value );
  1254. },
  1255. // https://jqueryvalidation.org/minlength-method/
  1256. minlength: function( value, element, param ) {
  1257. var length = Array.isArray( value ) ? value.length : this.getLength( value, element );
  1258. return this.optional( element ) || length >= param;
  1259. },
  1260. // https://jqueryvalidation.org/maxlength-method/
  1261. maxlength: function( value, element, param ) {
  1262. var length = Array.isArray( value ) ? value.length : this.getLength( value, element );
  1263. return this.optional( element ) || length <= param;
  1264. },
  1265. // https://jqueryvalidation.org/rangelength-method/
  1266. rangelength: function( value, element, param ) {
  1267. var length = Array.isArray( value ) ? value.length : this.getLength( value, element );
  1268. return this.optional( element ) || ( length >= param[ 0 ] && length <= param[ 1 ] );
  1269. },
  1270. // https://jqueryvalidation.org/min-method/
  1271. min: function( value, element, param ) {
  1272. return this.optional( element ) || value >= param;
  1273. },
  1274. // https://jqueryvalidation.org/max-method/
  1275. max: function( value, element, param ) {
  1276. return this.optional( element ) || value <= param;
  1277. },
  1278. // https://jqueryvalidation.org/range-method/
  1279. range: function( value, element, param ) {
  1280. return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] );
  1281. },
  1282. // https://jqueryvalidation.org/step-method/
  1283. step: function( value, element, param ) {
  1284. var type = $( element ).attr( "type" ),
  1285. errorMessage = "Step attribute on input type " + type + " is not supported.",
  1286. supportedTypes = [ "text", "number", "range" ],
  1287. re = new RegExp( "\\b" + type + "\\b" ),
  1288. notSupported = type && !re.test( supportedTypes.join() ),
  1289. decimalPlaces = function( num ) {
  1290. var match = ( "" + num ).match( /(?:\.(\d+))?$/ );
  1291. if ( !match ) {
  1292. return 0;
  1293. }
  1294. // Number of digits right of decimal point.
  1295. return match[ 1 ] ? match[ 1 ].length : 0;
  1296. },
  1297. toInt = function( num ) {
  1298. return Math.round( num * Math.pow( 10, decimals ) );
  1299. },
  1300. valid = true,
  1301. decimals;
  1302. // Works only for text, number and range input types
  1303. // TODO find a way to support input types date, datetime, datetime-local, month, time and week
  1304. if ( notSupported ) {
  1305. throw new Error( errorMessage );
  1306. }
  1307. decimals = decimalPlaces( param );
  1308. // Value can't have too many decimals
  1309. if ( decimalPlaces( value ) > decimals || toInt( value ) % toInt( param ) !== 0 ) {
  1310. valid = false;
  1311. }
  1312. return this.optional( element ) || valid;
  1313. },
  1314. // https://jqueryvalidation.org/equalTo-method/
  1315. equalTo: function( value, element, param ) {
  1316. // Bind to the blur event of the target in order to revalidate whenever the target field is updated
  1317. var target = $( param );
  1318. if ( this.settings.onfocusout && target.not( ".validate-equalTo-blur" ).length ) {
  1319. target.addClass( "validate-equalTo-blur" ).on( "blur.validate-equalTo", function() {
  1320. $( element ).valid();
  1321. } );
  1322. }
  1323. return value === target.val();
  1324. },
  1325. // https://jqueryvalidation.org/remote-method/
  1326. remote: function( value, element, param, method ) {
  1327. if ( this.optional( element ) ) {
  1328. return "dependency-mismatch";
  1329. }
  1330. method = typeof method === "string" && method || "remote";
  1331. var previous = this.previousValue( element, method ),
  1332. validator, data, optionDataString;
  1333. if ( !this.settings.messages[ element.name ] ) {
  1334. this.settings.messages[ element.name ] = {};
  1335. }
  1336. previous.originalMessage = previous.originalMessage || this.settings.messages[ element.name ][ method ];
  1337. this.settings.messages[ element.name ][ method ] = previous.message;
  1338. param = typeof param === "string" && { url: param } || param;
  1339. optionDataString = $.param( $.extend( { data: value }, param.data ) );
  1340. if ( previous.old === optionDataString ) {
  1341. return previous.valid;
  1342. }
  1343. previous.old = optionDataString;
  1344. validator = this;
  1345. this.startRequest( element );
  1346. data = {};
  1347. data[ element.name ] = value;
  1348. $.ajax( $.extend( true, {
  1349. mode: "abort",
  1350. port: "validate" + element.name,
  1351. dataType: "json",
  1352. data: data,
  1353. context: validator.currentForm,
  1354. success: function( response ) {
  1355. var valid = response === true || response === "true",
  1356. errors, message, submitted;
  1357. validator.settings.messages[ element.name ][ method ] = previous.originalMessage;
  1358. if ( valid ) {
  1359. submitted = validator.formSubmitted;
  1360. validator.resetInternals();
  1361. validator.toHide = validator.errorsFor( element );
  1362. validator.formSubmitted = submitted;
  1363. validator.successList.push( element );
  1364. validator.invalid[ element.name ] = false;
  1365. validator.showErrors();
  1366. } else {
  1367. errors = {};
  1368. message = response || validator.defaultMessage( element, { method: method, parameters: value } );
  1369. errors[ element.name ] = previous.message = message;
  1370. validator.invalid[ element.name ] = true;
  1371. validator.showErrors( errors );
  1372. }
  1373. previous.valid = valid;
  1374. validator.stopRequest( element, valid );
  1375. }
  1376. }, param ) );
  1377. return "pending";
  1378. }
  1379. }
  1380. } );
  1381. // Ajax mode: abort
  1382. // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
  1383. // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
  1384. var pendingRequests = {},
  1385. ajax;
  1386. // Use a prefilter if available (1.5+)
  1387. if ( $.ajaxPrefilter ) {
  1388. $.ajaxPrefilter( function( settings, _, xhr ) {
  1389. var port = settings.port;
  1390. if ( settings.mode === "abort" ) {
  1391. if ( pendingRequests[ port ] ) {
  1392. pendingRequests[ port ].abort();
  1393. }
  1394. pendingRequests[ port ] = xhr;
  1395. }
  1396. } );
  1397. } else {
  1398. // Proxy ajax
  1399. ajax = $.ajax;
  1400. $.ajax = function( settings ) {
  1401. var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode,
  1402. port = ( "port" in settings ? settings : $.ajaxSettings ).port;
  1403. if ( mode === "abort" ) {
  1404. if ( pendingRequests[ port ] ) {
  1405. pendingRequests[ port ].abort();
  1406. }
  1407. pendingRequests[ port ] = ajax.apply( this, arguments );
  1408. return pendingRequests[ port ];
  1409. }
  1410. return ajax.apply( this, arguments );
  1411. };
  1412. }
  1413. return $;
  1414. }));