jquery.validate.js 45 KB

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