jquery.validate.js 42 KB

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