PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/files/jquery.validation/1.13.0/jquery.validate.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 1357 lines | 1076 code | 153 blank | 128 comment | 234 complexity | f3010ba8a4d3d1a4e1ff0bad36d27bd0 MD5 | raw file
  1. /*!
  2. * jQuery Validation Plugin v1.13.0
  3. *
  4. * http://jqueryvalidation.org/
  5. *
  6. * Copyright (c) 2014 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 {
  13. factory( jQuery );
  14. }
  15. }(function( $ ) {
  16. $.extend($.fn, {
  17. // http://jqueryvalidation.org/validate/
  18. validate: function( options ) {
  19. // if nothing is selected, return nothing; can't chain anyway
  20. if ( !this.length ) {
  21. if ( options && options.debug && window.console ) {
  22. console.warn( "Nothing selected, can't validate, returning nothing." );
  23. }
  24. return;
  25. }
  26. // check if a validator for this form was already created
  27. var validator = $.data( this[ 0 ], "validator" );
  28. if ( validator ) {
  29. return validator;
  30. }
  31. // Add novalidate tag if HTML5.
  32. this.attr( "novalidate", "novalidate" );
  33. validator = new $.validator( options, this[ 0 ] );
  34. $.data( this[ 0 ], "validator", validator );
  35. if ( validator.settings.onsubmit ) {
  36. this.validateDelegate( ":submit", "click", function( event ) {
  37. if ( validator.settings.submitHandler ) {
  38. validator.submitButton = event.target;
  39. }
  40. // allow suppressing validation by adding a cancel class to the submit button
  41. if ( $( event.target ).hasClass( "cancel" ) ) {
  42. validator.cancelSubmit = true;
  43. }
  44. // allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
  45. if ( $( event.target ).attr( "formnovalidate" ) !== undefined ) {
  46. validator.cancelSubmit = true;
  47. }
  48. });
  49. // validate the form on submit
  50. this.submit( function( event ) {
  51. if ( validator.settings.debug ) {
  52. // prevent form submit to be able to see console output
  53. event.preventDefault();
  54. }
  55. function handle() {
  56. var hidden;
  57. if ( validator.settings.submitHandler ) {
  58. if ( validator.submitButton ) {
  59. // insert a hidden input as a replacement for the missing submit button
  60. hidden = $( "<input type='hidden'/>" )
  61. .attr( "name", validator.submitButton.name )
  62. .val( $( validator.submitButton ).val() )
  63. .appendTo( validator.currentForm );
  64. }
  65. validator.settings.submitHandler.call( validator, validator.currentForm, event );
  66. if ( validator.submitButton ) {
  67. // and clean up afterwards; thanks to no-block-scope, hidden can be referenced
  68. hidden.remove();
  69. }
  70. return false;
  71. }
  72. return true;
  73. }
  74. // prevent submit for invalid forms or custom submit handlers
  75. if ( validator.cancelSubmit ) {
  76. validator.cancelSubmit = false;
  77. return handle();
  78. }
  79. if ( validator.form() ) {
  80. if ( validator.pendingRequest ) {
  81. validator.formSubmitted = true;
  82. return false;
  83. }
  84. return handle();
  85. } else {
  86. validator.focusInvalid();
  87. return false;
  88. }
  89. });
  90. }
  91. return validator;
  92. },
  93. // http://jqueryvalidation.org/valid/
  94. valid: function() {
  95. var valid, validator;
  96. if ( $( this[ 0 ] ).is( "form" ) ) {
  97. valid = this.validate().form();
  98. } else {
  99. valid = true;
  100. validator = $( this[ 0 ].form ).validate();
  101. this.each( function() {
  102. valid = validator.element( this ) && valid;
  103. });
  104. }
  105. return valid;
  106. },
  107. // attributes: space separated list of attributes to retrieve and remove
  108. removeAttrs: function( attributes ) {
  109. var result = {},
  110. $element = this;
  111. $.each( attributes.split( /\s/ ), function( index, value ) {
  112. result[ value ] = $element.attr( value );
  113. $element.removeAttr( value );
  114. });
  115. return result;
  116. },
  117. // http://jqueryvalidation.org/rules/
  118. rules: function( command, argument ) {
  119. var element = this[ 0 ],
  120. settings, staticRules, existingRules, data, param, filtered;
  121. if ( command ) {
  122. settings = $.data( element.form, "validator" ).settings;
  123. staticRules = settings.rules;
  124. existingRules = $.validator.staticRules( element );
  125. switch ( command ) {
  126. case "add":
  127. $.extend( existingRules, $.validator.normalizeRule( argument ) );
  128. // remove messages from rules, but allow them to be set separately
  129. delete existingRules.messages;
  130. staticRules[ element.name ] = existingRules;
  131. if ( argument.messages ) {
  132. settings.messages[ element.name ] = $.extend( settings.messages[ element.name ], argument.messages );
  133. }
  134. break;
  135. case "remove":
  136. if ( !argument ) {
  137. delete staticRules[ element.name ];
  138. return existingRules;
  139. }
  140. filtered = {};
  141. $.each( argument.split( /\s/ ), function( index, method ) {
  142. filtered[ method ] = existingRules[ method ];
  143. delete existingRules[ method ];
  144. if ( method === "required" ) {
  145. $( element ).removeAttr( "aria-required" );
  146. }
  147. });
  148. return filtered;
  149. }
  150. }
  151. data = $.validator.normalizeRules(
  152. $.extend(
  153. {},
  154. $.validator.classRules( element ),
  155. $.validator.attributeRules( element ),
  156. $.validator.dataRules( element ),
  157. $.validator.staticRules( element )
  158. ), element );
  159. // make sure required is at front
  160. if ( data.required ) {
  161. param = data.required;
  162. delete data.required;
  163. data = $.extend( { required: param }, data );
  164. $( element ).attr( "aria-required", "true" );
  165. }
  166. // make sure remote is at back
  167. if ( data.remote ) {
  168. param = data.remote;
  169. delete data.remote;
  170. data = $.extend( data, { remote: param });
  171. }
  172. return data;
  173. }
  174. });
  175. // Custom selectors
  176. $.extend( $.expr[ ":" ], {
  177. // http://jqueryvalidation.org/blank-selector/
  178. blank: function( a ) {
  179. return !$.trim( "" + $( a ).val() );
  180. },
  181. // http://jqueryvalidation.org/filled-selector/
  182. filled: function( a ) {
  183. return !!$.trim( "" + $( a ).val() );
  184. },
  185. // http://jqueryvalidation.org/unchecked-selector/
  186. unchecked: function( a ) {
  187. return !$( a ).prop( "checked" );
  188. }
  189. });
  190. // constructor for validator
  191. $.validator = function( options, form ) {
  192. this.settings = $.extend( true, {}, $.validator.defaults, options );
  193. this.currentForm = form;
  194. this.init();
  195. };
  196. // http://jqueryvalidation.org/jQuery.validator.format/
  197. $.validator.format = function( source, params ) {
  198. if ( arguments.length === 1 ) {
  199. return function() {
  200. var args = $.makeArray( arguments );
  201. args.unshift( source );
  202. return $.validator.format.apply( this, args );
  203. };
  204. }
  205. if ( arguments.length > 2 && params.constructor !== Array ) {
  206. params = $.makeArray( arguments ).slice( 1 );
  207. }
  208. if ( params.constructor !== Array ) {
  209. params = [ params ];
  210. }
  211. $.each( params, function( i, n ) {
  212. source = source.replace( new RegExp( "\\{" + i + "\\}", "g" ), function() {
  213. return n;
  214. });
  215. });
  216. return source;
  217. };
  218. $.extend( $.validator, {
  219. defaults: {
  220. messages: {},
  221. groups: {},
  222. rules: {},
  223. errorClass: "error",
  224. validClass: "valid",
  225. errorElement: "label",
  226. focusInvalid: true,
  227. errorContainer: $( [] ),
  228. errorLabelContainer: $( [] ),
  229. onsubmit: true,
  230. ignore: ":hidden",
  231. ignoreTitle: false,
  232. onfocusin: function( element ) {
  233. this.lastActive = element;
  234. // hide error label and remove error class on focus if enabled
  235. if ( this.settings.focusCleanup && !this.blockFocusCleanup ) {
  236. if ( this.settings.unhighlight ) {
  237. this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.validClass );
  238. }
  239. this.hideThese( this.errorsFor( element ) );
  240. }
  241. },
  242. onfocusout: function( element ) {
  243. if ( !this.checkable( element ) && ( element.name in this.submitted || !this.optional( element ) ) ) {
  244. this.element( element );
  245. }
  246. },
  247. onkeyup: function( element, event ) {
  248. if ( event.which === 9 && this.elementValue( element ) === "" ) {
  249. return;
  250. } else if ( element.name in this.submitted || element === this.lastElement ) {
  251. this.element( element );
  252. }
  253. },
  254. onclick: function( element ) {
  255. // click on selects, radiobuttons and checkboxes
  256. if ( element.name in this.submitted ) {
  257. this.element( element );
  258. // or option elements, check parent select in that case
  259. } else if ( element.parentNode.name in this.submitted ) {
  260. this.element( element.parentNode );
  261. }
  262. },
  263. highlight: function( element, errorClass, validClass ) {
  264. if ( element.type === "radio" ) {
  265. this.findByName( element.name ).addClass( errorClass ).removeClass( validClass );
  266. } else {
  267. $( element ).addClass( errorClass ).removeClass( validClass );
  268. }
  269. },
  270. unhighlight: function( element, errorClass, validClass ) {
  271. if ( element.type === "radio" ) {
  272. this.findByName( element.name ).removeClass( errorClass ).addClass( validClass );
  273. } else {
  274. $( element ).removeClass( errorClass ).addClass( validClass );
  275. }
  276. }
  277. },
  278. // http://jqueryvalidation.org/jQuery.validator.setDefaults/
  279. setDefaults: function( settings ) {
  280. $.extend( $.validator.defaults, settings );
  281. },
  282. messages: {
  283. required: "This field is required.",
  284. remote: "Please fix this field.",
  285. email: "Please enter a valid email address.",
  286. url: "Please enter a valid URL.",
  287. date: "Please enter a valid date.",
  288. dateISO: "Please enter a valid date ( ISO ).",
  289. number: "Please enter a valid number.",
  290. digits: "Please enter only digits.",
  291. creditcard: "Please enter a valid credit card number.",
  292. equalTo: "Please enter the same value again.",
  293. maxlength: $.validator.format( "Please enter no more than {0} characters." ),
  294. minlength: $.validator.format( "Please enter at least {0} characters." ),
  295. rangelength: $.validator.format( "Please enter a value between {0} and {1} characters long." ),
  296. range: $.validator.format( "Please enter a value between {0} and {1}." ),
  297. max: $.validator.format( "Please enter a value less than or equal to {0}." ),
  298. min: $.validator.format( "Please enter a value greater than or equal to {0}." )
  299. },
  300. autoCreateRanges: false,
  301. prototype: {
  302. init: function() {
  303. this.labelContainer = $( this.settings.errorLabelContainer );
  304. this.errorContext = this.labelContainer.length && this.labelContainer || $( this.currentForm );
  305. this.containers = $( this.settings.errorContainer ).add( this.settings.errorLabelContainer );
  306. this.submitted = {};
  307. this.valueCache = {};
  308. this.pendingRequest = 0;
  309. this.pending = {};
  310. this.invalid = {};
  311. this.reset();
  312. var groups = ( this.groups = {} ),
  313. rules;
  314. $.each( this.settings.groups, function( key, value ) {
  315. if ( typeof value === "string" ) {
  316. value = value.split( /\s/ );
  317. }
  318. $.each( value, function( index, name ) {
  319. groups[ name ] = key;
  320. });
  321. });
  322. rules = this.settings.rules;
  323. $.each( rules, function( key, value ) {
  324. rules[ key ] = $.validator.normalizeRule( value );
  325. });
  326. function delegate( event ) {
  327. var validator = $.data( this[ 0 ].form, "validator" ),
  328. eventType = "on" + event.type.replace( /^validate/, "" ),
  329. settings = validator.settings;
  330. if ( settings[ eventType ] && !this.is( settings.ignore ) ) {
  331. settings[ eventType ].call( validator, this[ 0 ], event );
  332. }
  333. }
  334. $( this.currentForm )
  335. .validateDelegate( ":text, [type='password'], [type='file'], select, textarea, " +
  336. "[type='number'], [type='search'] ,[type='tel'], [type='url'], " +
  337. "[type='email'], [type='datetime'], [type='date'], [type='month'], " +
  338. "[type='week'], [type='time'], [type='datetime-local'], " +
  339. "[type='range'], [type='color'], [type='radio'], [type='checkbox']",
  340. "focusin focusout keyup", delegate)
  341. // Support: Chrome, oldIE
  342. // "select" is provided as event.target when clicking a option
  343. .validateDelegate("select, option, [type='radio'], [type='checkbox']", "click", delegate);
  344. if ( this.settings.invalidHandler ) {
  345. $( this.currentForm ).bind( "invalid-form.validate", this.settings.invalidHandler );
  346. }
  347. // Add aria-required to any Static/Data/Class required fields before first validation
  348. // Screen readers require this attribute to be present before the initial submission http://www.w3.org/TR/WCAG-TECHS/ARIA2.html
  349. $( this.currentForm ).find( "[required], [data-rule-required], .required" ).attr( "aria-required", "true" );
  350. },
  351. // http://jqueryvalidation.org/Validator.form/
  352. form: function() {
  353. this.checkForm();
  354. $.extend( this.submitted, this.errorMap );
  355. this.invalid = $.extend({}, this.errorMap );
  356. if ( !this.valid() ) {
  357. $( this.currentForm ).triggerHandler( "invalid-form", [ this ]);
  358. }
  359. this.showErrors();
  360. return this.valid();
  361. },
  362. checkForm: function() {
  363. this.prepareForm();
  364. for ( var i = 0, elements = ( this.currentElements = this.elements() ); elements[ i ]; i++ ) {
  365. this.check( elements[ i ] );
  366. }
  367. return this.valid();
  368. },
  369. // http://jqueryvalidation.org/Validator.element/
  370. element: function( element ) {
  371. var cleanElement = this.clean( element ),
  372. checkElement = this.validationTargetFor( cleanElement ),
  373. result = true;
  374. this.lastElement = checkElement;
  375. if ( checkElement === undefined ) {
  376. delete this.invalid[ cleanElement.name ];
  377. } else {
  378. this.prepareElement( checkElement );
  379. this.currentElements = $( checkElement );
  380. result = this.check( checkElement ) !== false;
  381. if ( result ) {
  382. delete this.invalid[ checkElement.name ];
  383. } else {
  384. this.invalid[ checkElement.name ] = true;
  385. }
  386. }
  387. // Add aria-invalid status for screen readers
  388. $( element ).attr( "aria-invalid", !result );
  389. if ( !this.numberOfInvalids() ) {
  390. // Hide error containers on last error
  391. this.toHide = this.toHide.add( this.containers );
  392. }
  393. this.showErrors();
  394. return result;
  395. },
  396. // http://jqueryvalidation.org/Validator.showErrors/
  397. showErrors: function( errors ) {
  398. if ( errors ) {
  399. // add items to error list and map
  400. $.extend( this.errorMap, errors );
  401. this.errorList = [];
  402. for ( var name in errors ) {
  403. this.errorList.push({
  404. message: errors[ name ],
  405. element: this.findByName( name )[ 0 ]
  406. });
  407. }
  408. // remove items from success list
  409. this.successList = $.grep( this.successList, function( element ) {
  410. return !( element.name in errors );
  411. });
  412. }
  413. if ( this.settings.showErrors ) {
  414. this.settings.showErrors.call( this, this.errorMap, this.errorList );
  415. } else {
  416. this.defaultShowErrors();
  417. }
  418. },
  419. // http://jqueryvalidation.org/Validator.resetForm/
  420. resetForm: function() {
  421. if ( $.fn.resetForm ) {
  422. $( this.currentForm ).resetForm();
  423. }
  424. this.submitted = {};
  425. this.lastElement = null;
  426. this.prepareForm();
  427. this.hideErrors();
  428. this.elements()
  429. .removeClass( this.settings.errorClass )
  430. .removeData( "previousValue" )
  431. .removeAttr( "aria-invalid" );
  432. },
  433. numberOfInvalids: function() {
  434. return this.objectLength( this.invalid );
  435. },
  436. objectLength: function( obj ) {
  437. /* jshint unused: false */
  438. var count = 0,
  439. i;
  440. for ( i in obj ) {
  441. count++;
  442. }
  443. return count;
  444. },
  445. hideErrors: function() {
  446. this.hideThese( this.toHide );
  447. },
  448. hideThese: function( errors ) {
  449. errors.not( this.containers ).text( "" );
  450. this.addWrapper( errors ).hide();
  451. },
  452. valid: function() {
  453. return this.size() === 0;
  454. },
  455. size: function() {
  456. return this.errorList.length;
  457. },
  458. focusInvalid: function() {
  459. if ( this.settings.focusInvalid ) {
  460. try {
  461. $( this.findLastActive() || this.errorList.length && this.errorList[ 0 ].element || [])
  462. .filter( ":visible" )
  463. .focus()
  464. // manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
  465. .trigger( "focusin" );
  466. } catch ( e ) {
  467. // ignore IE throwing errors when focusing hidden elements
  468. }
  469. }
  470. },
  471. findLastActive: function() {
  472. var lastActive = this.lastActive;
  473. return lastActive && $.grep( this.errorList, function( n ) {
  474. return n.element.name === lastActive.name;
  475. }).length === 1 && lastActive;
  476. },
  477. elements: function() {
  478. var validator = this,
  479. rulesCache = {};
  480. // select all valid inputs inside the form (no submit or reset buttons)
  481. return $( this.currentForm )
  482. .find( "input, select, textarea" )
  483. .not( ":submit, :reset, :image, [disabled]" )
  484. .not( this.settings.ignore )
  485. .filter( function() {
  486. if ( !this.name && validator.settings.debug && window.console ) {
  487. console.error( "%o has no name assigned", this );
  488. }
  489. // select only the first element for each name, and only those with rules specified
  490. if ( this.name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
  491. return false;
  492. }
  493. rulesCache[ this.name ] = true;
  494. return true;
  495. });
  496. },
  497. clean: function( selector ) {
  498. return $( selector )[ 0 ];
  499. },
  500. errors: function() {
  501. var errorClass = this.settings.errorClass.split( " " ).join( "." );
  502. return $( this.settings.errorElement + "." + errorClass, this.errorContext );
  503. },
  504. reset: function() {
  505. this.successList = [];
  506. this.errorList = [];
  507. this.errorMap = {};
  508. this.toShow = $( [] );
  509. this.toHide = $( [] );
  510. this.currentElements = $( [] );
  511. },
  512. prepareForm: function() {
  513. this.reset();
  514. this.toHide = this.errors().add( this.containers );
  515. },
  516. prepareElement: function( element ) {
  517. this.reset();
  518. this.toHide = this.errorsFor( element );
  519. },
  520. elementValue: function( element ) {
  521. var val,
  522. $element = $( element ),
  523. type = element.type;
  524. if ( type === "radio" || type === "checkbox" ) {
  525. return $( "input[name='" + element.name + "']:checked" ).val();
  526. } else if ( type === "number" && typeof element.validity !== "undefined" ) {
  527. return element.validity.badInput ? false : $element.val();
  528. }
  529. val = $element.val();
  530. if ( typeof val === "string" ) {
  531. return val.replace(/\r/g, "" );
  532. }
  533. return val;
  534. },
  535. check: function( element ) {
  536. element = this.validationTargetFor( this.clean( element ) );
  537. var rules = $( element ).rules(),
  538. rulesCount = $.map( rules, function( n, i ) {
  539. return i;
  540. }).length,
  541. dependencyMismatch = false,
  542. val = this.elementValue( element ),
  543. result, method, rule;
  544. for ( method in rules ) {
  545. rule = { method: method, parameters: rules[ method ] };
  546. try {
  547. result = $.validator.methods[ method ].call( this, val, element, rule.parameters );
  548. // if a method indicates that the field is optional and therefore valid,
  549. // don't mark it as valid when there are no other rules
  550. if ( result === "dependency-mismatch" && rulesCount === 1 ) {
  551. dependencyMismatch = true;
  552. continue;
  553. }
  554. dependencyMismatch = false;
  555. if ( result === "pending" ) {
  556. this.toHide = this.toHide.not( this.errorsFor( element ) );
  557. return;
  558. }
  559. if ( !result ) {
  560. this.formatAndAdd( element, rule );
  561. return false;
  562. }
  563. } catch ( e ) {
  564. if ( this.settings.debug && window.console ) {
  565. console.log( "Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e );
  566. }
  567. throw e;
  568. }
  569. }
  570. if ( dependencyMismatch ) {
  571. return;
  572. }
  573. if ( this.objectLength( rules ) ) {
  574. this.successList.push( element );
  575. }
  576. return true;
  577. },
  578. // return the custom message for the given element and validation method
  579. // specified in the element's HTML5 data attribute
  580. // return the generic message if present and no method specific message is present
  581. customDataMessage: function( element, method ) {
  582. return $( element ).data( "msg" + method.charAt( 0 ).toUpperCase() +
  583. method.substring( 1 ).toLowerCase() ) || $( element ).data( "msg" );
  584. },
  585. // return the custom message for the given element name and validation method
  586. customMessage: function( name, method ) {
  587. var m = this.settings.messages[ name ];
  588. return m && ( m.constructor === String ? m : m[ method ]);
  589. },
  590. // return the first defined argument, allowing empty strings
  591. findDefined: function() {
  592. for ( var i = 0; i < arguments.length; i++) {
  593. if ( arguments[ i ] !== undefined ) {
  594. return arguments[ i ];
  595. }
  596. }
  597. return undefined;
  598. },
  599. defaultMessage: function( element, method ) {
  600. return this.findDefined(
  601. this.customMessage( element.name, method ),
  602. this.customDataMessage( element, method ),
  603. // title is never undefined, so handle empty string as undefined
  604. !this.settings.ignoreTitle && element.title || undefined,
  605. $.validator.messages[ method ],
  606. "<strong>Warning: No message defined for " + element.name + "</strong>"
  607. );
  608. },
  609. formatAndAdd: function( element, rule ) {
  610. var message = this.defaultMessage( element, rule.method ),
  611. theregex = /\$?\{(\d+)\}/g;
  612. if ( typeof message === "function" ) {
  613. message = message.call( this, rule.parameters, element );
  614. } else if ( theregex.test( message ) ) {
  615. message = $.validator.format( message.replace( theregex, "{$1}" ), rule.parameters );
  616. }
  617. this.errorList.push({
  618. message: message,
  619. element: element,
  620. method: rule.method
  621. });
  622. this.errorMap[ element.name ] = message;
  623. this.submitted[ element.name ] = message;
  624. },
  625. addWrapper: function( toToggle ) {
  626. if ( this.settings.wrapper ) {
  627. toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) );
  628. }
  629. return toToggle;
  630. },
  631. defaultShowErrors: function() {
  632. var i, elements, error;
  633. for ( i = 0; this.errorList[ i ]; i++ ) {
  634. error = this.errorList[ i ];
  635. if ( this.settings.highlight ) {
  636. this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
  637. }
  638. this.showLabel( error.element, error.message );
  639. }
  640. if ( this.errorList.length ) {
  641. this.toShow = this.toShow.add( this.containers );
  642. }
  643. if ( this.settings.success ) {
  644. for ( i = 0; this.successList[ i ]; i++ ) {
  645. this.showLabel( this.successList[ i ] );
  646. }
  647. }
  648. if ( this.settings.unhighlight ) {
  649. for ( i = 0, elements = this.validElements(); elements[ i ]; i++ ) {
  650. this.settings.unhighlight.call( this, elements[ i ], this.settings.errorClass, this.settings.validClass );
  651. }
  652. }
  653. this.toHide = this.toHide.not( this.toShow );
  654. this.hideErrors();
  655. this.addWrapper( this.toShow ).show();
  656. },
  657. validElements: function() {
  658. return this.currentElements.not( this.invalidElements() );
  659. },
  660. invalidElements: function() {
  661. return $( this.errorList ).map(function() {
  662. return this.element;
  663. });
  664. },
  665. showLabel: function( element, message ) {
  666. var place, group, errorID,
  667. error = this.errorsFor( element ),
  668. elementID = this.idOrName( element ),
  669. describedBy = $( element ).attr( "aria-describedby" );
  670. if ( error.length ) {
  671. // refresh error/success class
  672. error.removeClass( this.settings.validClass ).addClass( this.settings.errorClass );
  673. // replace message on existing label
  674. error.html( message );
  675. } else {
  676. // create error element
  677. error = $( "<" + this.settings.errorElement + ">" )
  678. .attr( "id", elementID + "-error" )
  679. .addClass( this.settings.errorClass )
  680. .html( message || "" );
  681. // Maintain reference to the element to be placed into the DOM
  682. place = error;
  683. if ( this.settings.wrapper ) {
  684. // make sure the element is visible, even in IE
  685. // actually showing the wrapped element is handled elsewhere
  686. place = error.hide().show().wrap( "<" + this.settings.wrapper + "/>" ).parent();
  687. }
  688. if ( this.labelContainer.length ) {
  689. this.labelContainer.append( place );
  690. } else if ( this.settings.errorPlacement ) {
  691. this.settings.errorPlacement( place, $( element ) );
  692. } else {
  693. place.insertAfter( element );
  694. }
  695. // Link error back to the element
  696. if ( error.is( "label" ) ) {
  697. // If the error is a label, then associate using 'for'
  698. error.attr( "for", elementID );
  699. } else if ( error.parents( "label[for='" + elementID + "']" ).length === 0 ) {
  700. // If the element is not a child of an associated label, then it's necessary
  701. // to explicitly apply aria-describedby
  702. errorID = error.attr( "id" );
  703. // Respect existing non-error aria-describedby
  704. if ( !describedBy ) {
  705. describedBy = errorID;
  706. } else if ( !describedBy.match( new RegExp( "\b" + errorID + "\b" ) ) ) {
  707. // Add to end of list if not already present
  708. describedBy += " " + errorID;
  709. }
  710. $( element ).attr( "aria-describedby", describedBy );
  711. // If this element is grouped, then assign to all elements in the same group
  712. group = this.groups[ element.name ];
  713. if ( group ) {
  714. $.each( this.groups, function( name, testgroup ) {
  715. if ( testgroup === group ) {
  716. $( "[name='" + name + "']", this.currentForm )
  717. .attr( "aria-describedby", error.attr( "id" ) );
  718. }
  719. });
  720. }
  721. }
  722. }
  723. if ( !message && this.settings.success ) {
  724. error.text( "" );
  725. if ( typeof this.settings.success === "string" ) {
  726. error.addClass( this.settings.success );
  727. } else {
  728. this.settings.success( error, element );
  729. }
  730. }
  731. this.toShow = this.toShow.add( error );
  732. },
  733. errorsFor: function( element ) {
  734. var name = this.idOrName( element ),
  735. describer = $( element ).attr( "aria-describedby" ),
  736. selector = "label[for='" + name + "'], label[for='" + name + "'] *";
  737. // aria-describedby should directly reference the error element
  738. if ( describer ) {
  739. selector = selector + ", #" + describer.replace( /\s+/g, ", #" );
  740. }
  741. return this
  742. .errors()
  743. .filter( selector );
  744. },
  745. idOrName: function( element ) {
  746. return this.groups[ element.name ] || ( this.checkable( element ) ? element.name : element.id || element.name );
  747. },
  748. validationTargetFor: function( element ) {
  749. // if radio/checkbox, validate first element in group instead
  750. if ( this.checkable( element ) ) {
  751. element = this.findByName( element.name ).not( this.settings.ignore )[ 0 ];
  752. }
  753. return element;
  754. },
  755. checkable: function( element ) {
  756. return ( /radio|checkbox/i ).test( element.type );
  757. },
  758. findByName: function( name ) {
  759. return $( this.currentForm ).find( "[name='" + name + "']" );
  760. },
  761. getLength: function( value, element ) {
  762. switch ( element.nodeName.toLowerCase() ) {
  763. case "select":
  764. return $( "option:selected", element ).length;
  765. case "input":
  766. if ( this.checkable( element ) ) {
  767. return this.findByName( element.name ).filter( ":checked" ).length;
  768. }
  769. }
  770. return value.length;
  771. },
  772. depend: function( param, element ) {
  773. return this.dependTypes[typeof param] ? this.dependTypes[typeof param]( param, element ) : true;
  774. },
  775. dependTypes: {
  776. "boolean": function( param ) {
  777. return param;
  778. },
  779. "string": function( param, element ) {
  780. return !!$( param, element.form ).length;
  781. },
  782. "function": function( param, element ) {
  783. return param( element );
  784. }
  785. },
  786. optional: function( element ) {
  787. var val = this.elementValue( element );
  788. return !$.validator.methods.required.call( this, val, element ) && "dependency-mismatch";
  789. },
  790. startRequest: function( element ) {
  791. if ( !this.pending[ element.name ] ) {
  792. this.pendingRequest++;
  793. this.pending[ element.name ] = true;
  794. }
  795. },
  796. stopRequest: function( element, valid ) {
  797. this.pendingRequest--;
  798. // sometimes synchronization fails, make sure pendingRequest is never < 0
  799. if ( this.pendingRequest < 0 ) {
  800. this.pendingRequest = 0;
  801. }
  802. delete this.pending[ element.name ];
  803. if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) {
  804. $( this.currentForm ).submit();
  805. this.formSubmitted = false;
  806. } else if (!valid && this.pendingRequest === 0 && this.formSubmitted ) {
  807. $( this.currentForm ).triggerHandler( "invalid-form", [ this ]);
  808. this.formSubmitted = false;
  809. }
  810. },
  811. previousValue: function( element ) {
  812. return $.data( element, "previousValue" ) || $.data( element, "previousValue", {
  813. old: null,
  814. valid: true,
  815. message: this.defaultMessage( element, "remote" )
  816. });
  817. }
  818. },
  819. classRuleSettings: {
  820. required: { required: true },
  821. email: { email: true },
  822. url: { url: true },
  823. date: { date: true },
  824. dateISO: { dateISO: true },
  825. number: { number: true },
  826. digits: { digits: true },
  827. creditcard: { creditcard: true }
  828. },
  829. addClassRules: function( className, rules ) {
  830. if ( className.constructor === String ) {
  831. this.classRuleSettings[ className ] = rules;
  832. } else {
  833. $.extend( this.classRuleSettings, className );
  834. }
  835. },
  836. classRules: function( element ) {
  837. var rules = {},
  838. classes = $( element ).attr( "class" );
  839. if ( classes ) {
  840. $.each( classes.split( " " ), function() {
  841. if ( this in $.validator.classRuleSettings ) {
  842. $.extend( rules, $.validator.classRuleSettings[ this ]);
  843. }
  844. });
  845. }
  846. return rules;
  847. },
  848. attributeRules: function( element ) {
  849. var rules = {},
  850. $element = $( element ),
  851. type = element.getAttribute( "type" ),
  852. method, value;
  853. for ( method in $.validator.methods ) {
  854. // support for <input required> in both html5 and older browsers
  855. if ( method === "required" ) {
  856. value = element.getAttribute( method );
  857. // Some browsers return an empty string for the required attribute
  858. // and non-HTML5 browsers might have required="" markup
  859. if ( value === "" ) {
  860. value = true;
  861. }
  862. // force non-HTML5 browsers to return bool
  863. value = !!value;
  864. } else {
  865. value = $element.attr( method );
  866. }
  867. // convert the value to a number for number inputs, and for text for backwards compability
  868. // allows type="date" and others to be compared as strings
  869. if ( /min|max/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
  870. value = Number( value );
  871. }
  872. if ( value || value === 0 ) {
  873. rules[ method ] = value;
  874. } else if ( type === method && type !== "range" ) {
  875. // exception: the jquery validate 'range' method
  876. // does not test for the html5 'range' type
  877. rules[ method ] = true;
  878. }
  879. }
  880. // maxlength may be returned as -1, 2147483647 ( IE ) and 524288 ( safari ) for text inputs
  881. if ( rules.maxlength && /-1|2147483647|524288/.test( rules.maxlength ) ) {
  882. delete rules.maxlength;
  883. }
  884. return rules;
  885. },
  886. dataRules: function( element ) {
  887. var method, value,
  888. rules = {}, $element = $( element );
  889. for ( method in $.validator.methods ) {
  890. value = $element.data( "rule" + method.charAt( 0 ).toUpperCase() + method.substring( 1 ).toLowerCase() );
  891. if ( value !== undefined ) {
  892. rules[ method ] = value;
  893. }
  894. }
  895. return rules;
  896. },
  897. staticRules: function( element ) {
  898. var rules = {},
  899. validator = $.data( element.form, "validator" );
  900. if ( validator.settings.rules ) {
  901. rules = $.validator.normalizeRule( validator.settings.rules[ element.name ] ) || {};
  902. }
  903. return rules;
  904. },
  905. normalizeRules: function( rules, element ) {
  906. // handle dependency check
  907. $.each( rules, function( prop, val ) {
  908. // ignore rule when param is explicitly false, eg. required:false
  909. if ( val === false ) {
  910. delete rules[ prop ];
  911. return;
  912. }
  913. if ( val.param || val.depends ) {
  914. var keepRule = true;
  915. switch ( typeof val.depends ) {
  916. case "string":
  917. keepRule = !!$( val.depends, element.form ).length;
  918. break;
  919. case "function":
  920. keepRule = val.depends.call( element, element );
  921. break;
  922. }
  923. if ( keepRule ) {
  924. rules[ prop ] = val.param !== undefined ? val.param : true;
  925. } else {
  926. delete rules[ prop ];
  927. }
  928. }
  929. });
  930. // evaluate parameters
  931. $.each( rules, function( rule, parameter ) {
  932. rules[ rule ] = $.isFunction( parameter ) ? parameter( element ) : parameter;
  933. });
  934. // clean number parameters
  935. $.each([ "minlength", "maxlength" ], function() {
  936. if ( rules[ this ] ) {
  937. rules[ this ] = Number( rules[ this ] );
  938. }
  939. });
  940. $.each([ "rangelength", "range" ], function() {
  941. var parts;
  942. if ( rules[ this ] ) {
  943. if ( $.isArray( rules[ this ] ) ) {
  944. rules[ this ] = [ Number( rules[ this ][ 0 ]), Number( rules[ this ][ 1 ] ) ];
  945. } else if ( typeof rules[ this ] === "string" ) {
  946. parts = rules[ this ].replace(/[\[\]]/g, "" ).split( /[\s,]+/ );
  947. rules[ this ] = [ Number( parts[ 0 ]), Number( parts[ 1 ] ) ];
  948. }
  949. }
  950. });
  951. if ( $.validator.autoCreateRanges ) {
  952. // auto-create ranges
  953. if ( rules.min && rules.max ) {
  954. rules.range = [ rules.min, rules.max ];
  955. delete rules.min;
  956. delete rules.max;
  957. }
  958. if ( rules.minlength && rules.maxlength ) {
  959. rules.rangelength = [ rules.minlength, rules.maxlength ];
  960. delete rules.minlength;
  961. delete rules.maxlength;
  962. }
  963. }
  964. return rules;
  965. },
  966. // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
  967. normalizeRule: function( data ) {
  968. if ( typeof data === "string" ) {
  969. var transformed = {};
  970. $.each( data.split( /\s/ ), function() {
  971. transformed[ this ] = true;
  972. });
  973. data = transformed;
  974. }
  975. return data;
  976. },
  977. // http://jqueryvalidation.org/jQuery.validator.addMethod/
  978. addMethod: function( name, method, message ) {
  979. $.validator.methods[ name ] = method;
  980. $.validator.messages[ name ] = message !== undefined ? message : $.validator.messages[ name ];
  981. if ( method.length < 3 ) {
  982. $.validator.addClassRules( name, $.validator.normalizeRule( name ) );
  983. }
  984. },
  985. methods: {
  986. // http://jqueryvalidation.org/required-method/
  987. required: function( value, element, param ) {
  988. // check if dependency is met
  989. if ( !this.depend( param, element ) ) {
  990. return "dependency-mismatch";
  991. }
  992. if ( element.nodeName.toLowerCase() === "select" ) {
  993. // could be an array for select-multiple or a string, both are fine this way
  994. var val = $( element ).val();
  995. return val && val.length > 0;
  996. }
  997. if ( this.checkable( element ) ) {
  998. return this.getLength( value, element ) > 0;
  999. }
  1000. return $.trim( value ).length > 0;
  1001. },
  1002. // http://jqueryvalidation.org/email-method/
  1003. email: function( value, element ) {
  1004. // From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
  1005. // Retrieved 2014-01-14
  1006. // If you have a problem with this implementation, report a bug against the above spec
  1007. // Or use custom methods to implement your own email validation
  1008. 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 );
  1009. },
  1010. // http://jqueryvalidation.org/url-method/
  1011. url: function( value, element ) {
  1012. // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
  1013. return this.optional( element ) || /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test( value );
  1014. },
  1015. // http://jqueryvalidation.org/date-method/
  1016. date: function( value, element ) {
  1017. return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() );
  1018. },
  1019. // http://jqueryvalidation.org/dateISO-method/
  1020. dateISO: function( value, element ) {
  1021. return this.optional( element ) || /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test( value );
  1022. },
  1023. // http://jqueryvalidation.org/number-method/
  1024. number: function( value, element ) {
  1025. return this.optional( element ) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( value );
  1026. },
  1027. // http://jqueryvalidation.org/digits-method/
  1028. digits: function( value, element ) {
  1029. return this.optional( element ) || /^\d+$/.test( value );
  1030. },
  1031. // http://jqueryvalidation.org/creditcard-method/
  1032. // based on http://en.wikipedia.org/wiki/Luhn/
  1033. creditcard: function( value, element ) {
  1034. if ( this.optional( element ) ) {
  1035. return "dependency-mismatch";
  1036. }
  1037. // accept only spaces, digits and dashes
  1038. if ( /[^0-9 \-]+/.test( value ) ) {
  1039. return false;
  1040. }
  1041. var nCheck = 0,
  1042. nDigit = 0,
  1043. bEven = false,
  1044. n, cDigit;
  1045. value = value.replace( /\D/g, "" );
  1046. // Basing min and max length on
  1047. // http://developer.ean.com/general_info/Valid_Credit_Card_Types
  1048. if ( value.length < 13 || value.length > 19 ) {
  1049. return false;
  1050. }
  1051. for ( n = value.length - 1; n >= 0; n--) {
  1052. cDigit = value.charAt( n );
  1053. nDigit = parseInt( cDigit, 10 );
  1054. if ( bEven ) {
  1055. if ( ( nDigit *= 2 ) > 9 ) {
  1056. nDigit -= 9;
  1057. }
  1058. }
  1059. nCheck += nDigit;
  1060. bEven = !bEven;
  1061. }
  1062. return ( nCheck % 10 ) === 0;
  1063. },
  1064. // http://jqueryvalidation.org/minlength-method/
  1065. minlength: function( value, element, param ) {
  1066. var length = $.isArray( value ) ? value.length : this.getLength( $.trim( value ), element );
  1067. return this.optional( element ) || length >= param;
  1068. },
  1069. // http://jqueryvalidation.org/maxlength-method/
  1070. maxlength: function( value, element, param ) {
  1071. var length = $.isArray( value ) ? value.length : this.getLength( $.trim( value ), element );
  1072. return this.optional( element ) || length <= param;
  1073. },
  1074. // http://jqueryvalidation.org/rangelength-method/
  1075. rangelength: function( value, element, param ) {
  1076. var length = $.isArray( value ) ? value.length : this.getLength( $.trim( value ), element );
  1077. return this.optional( element ) || ( length >= param[ 0 ] && length <= param[ 1 ] );
  1078. },
  1079. // http://jqueryvalidation.org/min-method/
  1080. min: function( value, element, param ) {
  1081. return this.optional( element ) || value >= param;
  1082. },
  1083. // http://jqueryvalidation.org/max-method/
  1084. max: function( value, element, param ) {
  1085. return this.optional( element ) || value <= param;
  1086. },
  1087. // http://jqueryvalidation.org/range-method/
  1088. range: function( value, element, param ) {
  1089. return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] );
  1090. },
  1091. // http://jqueryvalidation.org/equalTo-method/
  1092. equalTo: function( value, element, param ) {
  1093. // bind to the blur event of the target in order to revalidate whenever the target field is updated
  1094. // TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
  1095. var target = $( param );
  1096. if ( this.settings.onfocusout ) {
  1097. target.unbind( ".validate-equalTo" ).bind( "blur.validate-equalTo", function() {
  1098. $( element ).valid();
  1099. });
  1100. }
  1101. return value === target.val();
  1102. },
  1103. // http://jqueryvalidation.org/remote-method/
  1104. remote: function( value, element, param ) {
  1105. if ( this.optional( element ) ) {
  1106. return "dependency-mismatch";
  1107. }
  1108. var previous = this.previousValue( element ),
  1109. validator, data;
  1110. if (!this.settings.messages[ element.name ] ) {
  1111. this.settings.messages[ element.name ] = {};
  1112. }
  1113. previous.originalMessage = this.settings.messages[ element.name ].remote;
  1114. this.settings.messages[ element.name ].remote = previous.message;
  1115. param = typeof param === "string" && { url: param } || param;
  1116. if ( previous.old === value ) {
  1117. return previous.valid;
  1118. }
  1119. previous.old = value;
  1120. validator = this;
  1121. this.startRequest( element );
  1122. data = {};
  1123. data[ element.name ] = value;
  1124. $.ajax( $.extend( true, {
  1125. url: param,
  1126. mode: "abort",
  1127. port: "validate" + element.name,
  1128. dataType: "json",
  1129. data: data,
  1130. context: validator.currentForm,
  1131. success: function( response ) {
  1132. var valid = response === true || response === "true",
  1133. errors, message, submitted;
  1134. validator.settings.messages[ element.name ].remote = previous.originalMessage;
  1135. if ( valid ) {
  1136. submitted = validator.formSubmitted;
  1137. validator.prepareElement( element );
  1138. validator.formSubmitted = submitted;
  1139. validator.successList.push( element );
  1140. delete validator.invalid[ element.name ];
  1141. validator.showErrors();
  1142. } else {
  1143. errors = {};
  1144. message = response || validator.defaultMessage( element, "remote" );
  1145. errors[ element.name ] = previous.message = $.isFunction( message ) ? message( value ) : message;
  1146. validator.invalid[ element.name ] = true;
  1147. validator.showErrors( errors );
  1148. }
  1149. previous.valid = valid;
  1150. validator.stopRequest( element, valid );
  1151. }
  1152. }, param ) );
  1153. return "pending";
  1154. }
  1155. }
  1156. });
  1157. $.format = function deprecated() {
  1158. throw "$.format has been deprecated. Please use $.validator.format instead.";
  1159. };
  1160. // ajax mode: abort
  1161. // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
  1162. // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
  1163. var pendingRequests = {},
  1164. ajax;
  1165. // Use a prefilter if available (1.5+)
  1166. if ( $.ajaxPrefilter ) {
  1167. $.ajaxPrefilter(function( settings, _, xhr ) {
  1168. var port = settings.port;
  1169. if ( settings.mode === "abort" ) {
  1170. if ( pendingRequests[port] ) {
  1171. pendingRequests[port].abort();
  1172. }
  1173. pendingRequests[port] = xhr;
  1174. }
  1175. });
  1176. } else {
  1177. // Proxy ajax
  1178. ajax = $.ajax;
  1179. $.ajax = function( settings ) {
  1180. var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode,
  1181. port = ( "port" in settings ? settings : $.ajaxSettings ).port;
  1182. if ( mode === "abort" ) {
  1183. if ( pendingRequests[port] ) {
  1184. pendingRequests[port].abort();
  1185. }
  1186. pendingRequests[port] = ajax.apply(this, arguments);
  1187. return pendingRequests[port];
  1188. }
  1189. return ajax.apply(this, arguments);
  1190. };
  1191. }
  1192. // provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation
  1193. // handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target
  1194. $.extend($.fn, {
  1195. validateDelegate: function( delegate, type, handler ) {
  1196. return this.bind(type, function( event ) {
  1197. var target = $(event.target);
  1198. if ( target.is(delegate) ) {
  1199. return handler.apply(target, arguments);
  1200. }
  1201. });
  1202. }
  1203. });
  1204. }));