Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

1674 wiersze
69 KiB

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