jquery.validate.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. /*!
  2. * jQuery Validation Plugin v1.15.1
  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. var element = this[ 0 ],
  120. settings, staticRules, existingRules, data, param, filtered;
  121. // If nothing is selected, return empty object; can't chain anyway
  122. if ( element == null || element.form == null ) {
  123. return;
  124. }
  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. // Set form expando on contenteditable
  356. if ( !this.form && this.hasAttribute( "contenteditable" ) ) {
  357. this.form = $( this ).closest( "form" )[ 0 ];
  358. }
  359. var validator = $.data( this.form, "validator" ),
  360. eventType = "on" + event.type.replace( /^validate/, "" ),
  361. settings = validator.settings;
  362. if ( settings[ eventType ] && !$( this ).is( settings.ignore ) ) {
  363. settings[ eventType ].call( validator, this, event );
  364. }
  365. }
  366. $( this.currentForm )
  367. .on( "focusin.validate focusout.validate keyup.validate",
  368. ":text, [type='password'], [type='file'], select, textarea, [type='number'], [type='search'], " +
  369. "[type='tel'], [type='url'], [type='email'], [type='datetime'], [type='date'], [type='month'], " +
  370. "[type='week'], [type='time'], [type='datetime-local'], [type='range'], [type='color'], " +
  371. "[type='radio'], [type='checkbox'], [contenteditable]", delegate )
  372. // Support: Chrome, oldIE
  373. // "select" is provided as event.target when clicking a option
  374. .on( "click.validate", "select, option, [type='radio'], [type='checkbox']", delegate );
  375. if ( this.settings.invalidHandler ) {
  376. $( this.currentForm ).on( "invalid-form.validate", this.settings.invalidHandler );
  377. }
  378. // Add aria-required to any Static/Data/Class required fields before first validation
  379. // Screen readers require this attribute to be present before the initial submission http://www.w3.org/TR/WCAG-TECHS/ARIA2.html
  380. $( this.currentForm ).find( "[required], [data-rule-required], .required" ).attr( "aria-required", "true" );
  381. },
  382. // http://jqueryvalidation.org/Validator.form/
  383. form: function() {
  384. this.checkForm();
  385. $.extend( this.submitted, this.errorMap );
  386. this.invalid = $.extend( {}, this.errorMap );
  387. if ( !this.valid() ) {
  388. $( this.currentForm ).triggerHandler( "invalid-form", [ this ] );
  389. }
  390. this.showErrors();
  391. return this.valid();
  392. },
  393. checkForm: function() {
  394. this.prepareForm();
  395. for ( var i = 0, elements = ( this.currentElements = this.elements() ); elements[ i ]; i++ ) {
  396. this.check( elements[ i ] );
  397. }
  398. return this.valid();
  399. },
  400. // http://jqueryvalidation.org/Validator.element/
  401. element: function( element ) {
  402. var cleanElement = this.clean( element ),
  403. checkElement = this.validationTargetFor( cleanElement ),
  404. v = this,
  405. result = true,
  406. rs, group;
  407. if ( checkElement === undefined ) {
  408. delete this.invalid[ cleanElement.name ];
  409. } else {
  410. this.prepareElement( checkElement );
  411. this.currentElements = $( checkElement );
  412. // If this element is grouped, then validate all group elements already
  413. // containing a value
  414. group = this.groups[ checkElement.name ];
  415. if ( group ) {
  416. $.each( this.groups, function( name, testgroup ) {
  417. if ( testgroup === group && name !== checkElement.name ) {
  418. cleanElement = v.validationTargetFor( v.clean( v.findByName( name ) ) );
  419. if ( cleanElement && cleanElement.name in v.invalid ) {
  420. v.currentElements.push( cleanElement );
  421. result = v.check( cleanElement ) && result;
  422. }
  423. }
  424. } );
  425. }
  426. rs = this.check( checkElement ) !== false;
  427. result = result && rs;
  428. if ( rs ) {
  429. this.invalid[ checkElement.name ] = false;
  430. } else {
  431. this.invalid[ checkElement.name ] = true;
  432. }
  433. if ( !this.numberOfInvalids() ) {
  434. // Hide error containers on last error
  435. this.toHide = this.toHide.add( this.containers );
  436. }
  437. this.showErrors();
  438. // Add aria-invalid status for screen readers
  439. $( element ).attr( "aria-invalid", !rs );
  440. }
  441. return result;
  442. },
  443. // http://jqueryvalidation.org/Validator.showErrors/
  444. showErrors: function( errors ) {
  445. if ( errors ) {
  446. var validator = this;
  447. // Add items to error list and map
  448. $.extend( this.errorMap, errors );
  449. this.errorList = $.map( this.errorMap, function( message, name ) {
  450. return {
  451. message: message,
  452. element: validator.findByName( name )[ 0 ]
  453. };
  454. } );
  455. // Remove items from success list
  456. this.successList = $.grep( this.successList, function( element ) {
  457. return !( element.name in errors );
  458. } );
  459. }
  460. if ( this.settings.showErrors ) {
  461. this.settings.showErrors.call( this, this.errorMap, this.errorList );
  462. } else {
  463. this.defaultShowErrors();
  464. }
  465. },
  466. // http://jqueryvalidation.org/Validator.resetForm/
  467. resetForm: function() {
  468. if ( $.fn.resetForm ) {
  469. $( this.currentForm ).resetForm();
  470. }
  471. this.invalid = {};
  472. this.submitted = {};
  473. this.prepareForm();
  474. this.hideErrors();
  475. var elements = this.elements()
  476. .removeData( "previousValue" )
  477. .removeAttr( "aria-invalid" );
  478. this.resetElements( elements );
  479. },
  480. resetElements: function( elements ) {
  481. var i;
  482. if ( this.settings.unhighlight ) {
  483. for ( i = 0; elements[ i ]; i++ ) {
  484. this.settings.unhighlight.call( this, elements[ i ],
  485. this.settings.errorClass, "" );
  486. this.findByName( elements[ i ].name ).removeClass( this.settings.validClass );
  487. }
  488. } else {
  489. elements
  490. .removeClass( this.settings.errorClass )
  491. .removeClass( this.settings.validClass );
  492. }
  493. },
  494. numberOfInvalids: function() {
  495. return this.objectLength( this.invalid );
  496. },
  497. objectLength: function( obj ) {
  498. /* jshint unused: false */
  499. var count = 0,
  500. i;
  501. for ( i in obj ) {
  502. if ( obj[ i ] ) {
  503. count++;
  504. }
  505. }
  506. return count;
  507. },
  508. hideErrors: function() {
  509. this.hideThese( this.toHide );
  510. },
  511. hideThese: function( errors ) {
  512. errors.not( this.containers ).text( "" );
  513. this.addWrapper( errors ).hide();
  514. },
  515. valid: function() {
  516. return this.size() === 0;
  517. },
  518. size: function() {
  519. return this.errorList.length;
  520. },
  521. focusInvalid: function() {
  522. if ( this.settings.focusInvalid ) {
  523. try {
  524. $( this.findLastActive() || this.errorList.length && this.errorList[ 0 ].element || [] )
  525. .filter( ":visible" )
  526. .focus()
  527. // Manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
  528. .trigger( "focusin" );
  529. } catch ( e ) {
  530. // Ignore IE throwing errors when focusing hidden elements
  531. }
  532. }
  533. },
  534. findLastActive: function() {
  535. var lastActive = this.lastActive;
  536. return lastActive && $.grep( this.errorList, function( n ) {
  537. return n.element.name === lastActive.name;
  538. } ).length === 1 && lastActive;
  539. },
  540. elements: function() {
  541. var validator = this,
  542. rulesCache = {};
  543. // Select all valid inputs inside the form (no submit or reset buttons)
  544. return $( this.currentForm )
  545. .find( "input, select, textarea, [contenteditable]" )
  546. .not( ":submit, :reset, :image, :disabled" )
  547. .not( this.settings.ignore )
  548. .filter( function() {
  549. var name = this.name || $( this ).attr( "name" ); // For contenteditable
  550. if ( !name && validator.settings.debug && window.console ) {
  551. console.error( "%o has no name assigned", this );
  552. }
  553. // Set form expando on contenteditable
  554. if ( this.hasAttribute( "contenteditable" ) ) {
  555. this.form = $( this ).closest( "form" )[ 0 ];
  556. }
  557. // Select only the first element for each name, and only those with rules specified
  558. if ( name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
  559. return false;
  560. }
  561. rulesCache[ name ] = true;
  562. return true;
  563. } );
  564. },
  565. clean: function( selector ) {
  566. return $( selector )[ 0 ];
  567. },
  568. errors: function() {
  569. var errorClass = this.settings.errorClass.split( " " ).join( "." );
  570. return $( this.settings.errorElement + "." + errorClass, this.errorContext );
  571. },
  572. resetInternals: function() {
  573. this.successList = [];
  574. this.errorList = [];
  575. this.errorMap = {};
  576. this.toShow = $( [] );
  577. this.toHide = $( [] );
  578. },
  579. reset: function() {
  580. this.resetInternals();
  581. this.currentElements = $( [] );
  582. },
  583. prepareForm: function() {
  584. this.reset();
  585. this.toHide = this.errors().add( this.containers );
  586. },
  587. prepareElement: function( element ) {
  588. this.reset();
  589. this.toHide = this.errorsFor( element );
  590. },
  591. elementValue: function( element ) {
  592. var $element = $( element ),
  593. type = element.type,
  594. val, idx;
  595. if ( type === "radio" || type === "checkbox" ) {
  596. return this.findByName( element.name ).filter( ":checked" ).val();
  597. } else if ( type === "number" && typeof element.validity !== "undefined" ) {
  598. return element.validity.badInput ? "NaN" : $element.val();
  599. }
  600. if ( element.hasAttribute( "contenteditable" ) ) {
  601. val = $element.text();
  602. } else {
  603. val = $element.val();
  604. }
  605. if ( type === "file" ) {
  606. // Modern browser (chrome & safari)
  607. if ( val.substr( 0, 12 ) === "C:\\fakepath\\" ) {
  608. return val.substr( 12 );
  609. }
  610. // Legacy browsers
  611. // Unix-based path
  612. idx = val.lastIndexOf( "/" );
  613. if ( idx >= 0 ) {
  614. return val.substr( idx + 1 );
  615. }
  616. // Windows-based path
  617. idx = val.lastIndexOf( "\\" );
  618. if ( idx >= 0 ) {
  619. return val.substr( idx + 1 );
  620. }
  621. // Just the file name
  622. return val;
  623. }
  624. if ( typeof val === "string" ) {
  625. return val.replace( /\r/g, "" );
  626. }
  627. return val;
  628. },
  629. check: function( element ) {
  630. element = this.validationTargetFor( this.clean( element ) );
  631. var rules = $( element ).rules(),
  632. rulesCount = $.map( rules, function( n, i ) {
  633. return i;
  634. } ).length,
  635. dependencyMismatch = false,
  636. val = this.elementValue( element ),
  637. result, method, rule;
  638. // If a normalizer is defined for this element, then
  639. // call it to retreive the changed value instead
  640. // of using the real one.
  641. // Note that `this` in the normalizer is `element`.
  642. if ( typeof rules.normalizer === "function" ) {
  643. val = rules.normalizer.call( element, val );
  644. if ( typeof val !== "string" ) {
  645. throw new TypeError( "The normalizer should return a string value." );
  646. }
  647. // Delete the normalizer from rules to avoid treating
  648. // it as a pre-defined method.
  649. delete rules.normalizer;
  650. }
  651. for ( method in rules ) {
  652. rule = { method: method, parameters: rules[ method ] };
  653. try {
  654. result = $.validator.methods[ method ].call( this, val, element, rule.parameters );
  655. // If a method indicates that the field is optional and therefore valid,
  656. // don't mark it as valid when there are no other rules
  657. if ( result === "dependency-mismatch" && rulesCount === 1 ) {
  658. dependencyMismatch = true;
  659. continue;
  660. }
  661. dependencyMismatch = false;
  662. if ( result === "pending" ) {
  663. this.toHide = this.toHide.not( this.errorsFor( element ) );
  664. return;
  665. }
  666. if ( !result ) {
  667. this.formatAndAdd( element, rule );
  668. return false;
  669. }
  670. } catch ( e ) {
  671. if ( this.settings.debug && window.console ) {
  672. console.log( "Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e );
  673. }
  674. if ( e instanceof TypeError ) {
  675. e.message += ". Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.";
  676. }
  677. throw e;
  678. }
  679. }
  680. if ( dependencyMismatch ) {
  681. return;
  682. }
  683. if ( this.objectLength( rules ) ) {
  684. this.successList.push( element );
  685. }
  686. return true;
  687. },
  688. // Return the custom message for the given element and validation method
  689. // specified in the element's HTML5 data attribute
  690. // return the generic message if present and no method specific message is present
  691. customDataMessage: function( element, method ) {
  692. return $( element ).data( "msg" + method.charAt( 0 ).toUpperCase() +
  693. method.substring( 1 ).toLowerCase() ) || $( element ).data( "msg" );
  694. },
  695. // Return the custom message for the given element name and validation method
  696. customMessage: function( name, method ) {
  697. var m = this.settings.messages[ name ];
  698. return m && ( m.constructor === String ? m : m[ method ] );
  699. },
  700. // Return the first defined argument, allowing empty strings
  701. findDefined: function() {
  702. for ( var i = 0; i < arguments.length; i++ ) {
  703. if ( arguments[ i ] !== undefined ) {
  704. return arguments[ i ];
  705. }
  706. }
  707. return undefined;
  708. },
  709. // The second parameter 'rule' used to be a string, and extended to an object literal
  710. // of the following form:
  711. // rule = {
  712. // method: "method name",
  713. // parameters: "the given method parameters"
  714. // }
  715. //
  716. // The old behavior still supported, kept to maintain backward compatibility with
  717. // old code, and will be removed in the next major release.
  718. defaultMessage: function( element, rule ) {
  719. if ( typeof rule === "string" ) {
  720. rule = { method: rule };
  721. }
  722. var message = this.findDefined(
  723. this.customMessage( element.name, rule.method ),
  724. this.customDataMessage( element, rule.method ),
  725. // 'title' is never undefined, so handle empty string as undefined
  726. !this.settings.ignoreTitle && element.title || undefined,
  727. $.validator.messages[ rule.method ],
  728. "<strong>Warning: No message defined for " + element.name + "</strong>"
  729. ),
  730. theregex = /\$?\{(\d+)\}/g;
  731. if ( typeof message === "function" ) {
  732. message = message.call( this, rule.parameters, element );
  733. } else if ( theregex.test( message ) ) {
  734. message = $.validator.format( message.replace( theregex, "{$1}" ), rule.parameters );
  735. }
  736. return message;
  737. },
  738. formatAndAdd: function( element, rule ) {
  739. var message = this.defaultMessage( element, rule );
  740. this.errorList.push( {
  741. message: message,
  742. element: element,
  743. method: rule.method
  744. } );
  745. this.errorMap[ element.name ] = message;
  746. this.submitted[ element.name ] = message;
  747. },
  748. addWrapper: function( toToggle ) {
  749. if ( this.settings.wrapper ) {
  750. toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) );
  751. }
  752. return toToggle;
  753. },
  754. defaultShowErrors: function() {
  755. var i, elements, error;
  756. for ( i = 0; this.errorList[ i ]; i++ ) {
  757. error = this.errorList[ i ];
  758. if ( this.settings.highlight ) {
  759. this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
  760. }
  761. this.showLabel( error.element, error.message );
  762. }
  763. if ( this.errorList.length ) {
  764. this.toShow = this.toShow.add( this.containers );
  765. }
  766. if ( this.settings.success ) {
  767. for ( i = 0; this.successList[ i ]; i++ ) {
  768. this.showLabel( this.successList[ i ] );
  769. }
  770. }
  771. if ( this.settings.unhighlight ) {
  772. for ( i = 0, elements = this.validElements(); elements[ i ]; i++ ) {
  773. this.settings.unhighlight.call( this, elements[ i ], this.settings.errorClass, this.settings.validClass );
  774. }
  775. }
  776. this.toHide = this.toHide.not( this.toShow );
  777. this.hideErrors();
  778. this.addWrapper( this.toShow ).show();
  779. },
  780. validElements: function() {
  781. return this.currentElements.not( this.invalidElements() );
  782. },
  783. invalidElements: function() {
  784. return $( this.errorList ).map( function() {
  785. return this.element;
  786. } );
  787. },
  788. showLabel: function( element, message ) {
  789. var place, group, errorID, v,
  790. error = this.errorsFor( element ),
  791. elementID = this.idOrName( element ),
  792. describedBy = $( element ).attr( "aria-describedby" );
  793. if ( error.length ) {
  794. // Refresh error/success class
  795. error.removeClass( this.settings.validClass ).addClass( this.settings.errorClass );
  796. // Replace message on existing label
  797. error.html( message );
  798. } else {
  799. // Create error element
  800. error = $( "<" + this.settings.errorElement + ">" )
  801. .attr( "id", elementID + "-error" )
  802. .addClass( this.settings.errorClass )
  803. .html( message || "" );
  804. // Maintain reference to the element to be placed into the DOM
  805. place = error;
  806. if ( this.settings.wrapper ) {
  807. // Make sure the element is visible, even in IE
  808. // actually showing the wrapped element is handled elsewhere
  809. place = error.hide().show().wrap( "<" + this.settings.wrapper + "/>" ).parent();
  810. }
  811. if ( this.labelContainer.length ) {
  812. this.labelContainer.append( place );
  813. } else if ( this.settings.errorPlacement ) {
  814. this.settings.errorPlacement.call( this, place, $( element ) );
  815. } else {
  816. place.insertAfter( element );
  817. }
  818. // Link error back to the element
  819. if ( error.is( "label" ) ) {
  820. // If the error is a label, then associate using 'for'
  821. error.attr( "for", elementID );
  822. // If the element is not a child of an associated label, then it's necessary
  823. // to explicitly apply aria-describedby
  824. } else if ( error.parents( "label[for='" + this.escapeCssMeta( elementID ) + "']" ).length === 0 ) {
  825. errorID = error.attr( "id" );
  826. // Respect existing non-error aria-describedby
  827. if ( !describedBy ) {
  828. describedBy = errorID;
  829. } else if ( !describedBy.match( new RegExp( "\\b" + this.escapeCssMeta( errorID ) + "\\b" ) ) ) {
  830. // Add to end of list if not already present
  831. describedBy += " " + errorID;
  832. }
  833. $( element ).attr( "aria-describedby", describedBy );
  834. // If this element is grouped, then assign to all elements in the same group
  835. group = this.groups[ element.name ];
  836. if ( group ) {
  837. v = this;
  838. $.each( v.groups, function( name, testgroup ) {
  839. if ( testgroup === group ) {
  840. $( "[name='" + v.escapeCssMeta( name ) + "']", v.currentForm )
  841. .attr( "aria-describedby", error.attr( "id" ) );
  842. }
  843. } );
  844. }
  845. }
  846. }
  847. if ( !message && this.settings.success ) {
  848. error.text( "" );
  849. if ( typeof this.settings.success === "string" ) {
  850. error.addClass( this.settings.success );
  851. } else {
  852. this.settings.success( error, element );
  853. }
  854. }
  855. this.toShow = this.toShow.add( error );
  856. },
  857. errorsFor: function( element ) {
  858. var name = this.escapeCssMeta( this.idOrName( element ) ),
  859. describer = $( element ).attr( "aria-describedby" ),
  860. selector = "label[for='" + name + "'], label[for='" + name + "'] *";
  861. // 'aria-describedby' should directly reference the error element
  862. if ( describer ) {
  863. selector = selector + ", #" + this.escapeCssMeta( describer )
  864. .replace( /\s+/g, ", #" );
  865. }
  866. return this
  867. .errors()
  868. .filter( selector );
  869. },
  870. // See https://api.jquery.com/category/selectors/, for CSS
  871. // meta-characters that should be escaped in order to be used with JQuery
  872. // as a literal part of a name/id or any selector.
  873. escapeCssMeta: function( string ) {
  874. return string.replace( /([\\!"#$%&'()*+,./:;<=>?@\[\]^`{|}~])/g, "\\$1" );
  875. },
  876. idOrName: function( element ) {
  877. return this.groups[ element.name ] || ( this.checkable( element ) ? element.name : element.id || element.name );
  878. },
  879. validationTargetFor: function( element ) {
  880. // If radio/checkbox, validate first element in group instead
  881. if ( this.checkable( element ) ) {
  882. element = this.findByName( element.name );
  883. }
  884. // Always apply ignore filter
  885. return $( element ).not( this.settings.ignore )[ 0 ];
  886. },
  887. checkable: function( element ) {
  888. return ( /radio|checkbox/i ).test( element.type );
  889. },
  890. findByName: function( name ) {
  891. return $( this.currentForm ).find( "[name='" + this.escapeCssMeta( name ) + "']" );
  892. },
  893. getLength: function( value, element ) {
  894. switch ( element.nodeName.toLowerCase() ) {
  895. case "select":
  896. return $( "option:selected", element ).length;
  897. case "input":
  898. if ( this.checkable( element ) ) {
  899. return this.findByName( element.name ).filter( ":checked" ).length;
  900. }
  901. }
  902. return value.length;
  903. },
  904. depend: function( param, element ) {
  905. return this.dependTypes[ typeof param ] ? this.dependTypes[ typeof param ]( param, element ) : true;
  906. },
  907. dependTypes: {
  908. "boolean": function( param ) {
  909. return param;
  910. },
  911. "string": function( param, element ) {
  912. return !!$( param, element.form ).length;
  913. },
  914. "function": function( param, element ) {
  915. return param( element );
  916. }
  917. },
  918. optional: function( element ) {
  919. var val = this.elementValue( element );
  920. return !$.validator.methods.required.call( this, val, element ) && "dependency-mismatch";
  921. },
  922. startRequest: function( element ) {
  923. if ( !this.pending[ element.name ] ) {
  924. this.pendingRequest++;
  925. $( element ).addClass( this.settings.pendingClass );
  926. this.pending[ element.name ] = true;
  927. }
  928. },
  929. stopRequest: function( element, valid ) {
  930. this.pendingRequest--;
  931. // Sometimes synchronization fails, make sure pendingRequest is never < 0
  932. if ( this.pendingRequest < 0 ) {
  933. this.pendingRequest = 0;
  934. }
  935. delete this.pending[ element.name ];
  936. $( element ).removeClass( this.settings.pendingClass );
  937. if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) {
  938. $( this.currentForm ).submit();
  939. this.formSubmitted = false;
  940. } else if ( !valid && this.pendingRequest === 0 && this.formSubmitted ) {
  941. $( this.currentForm ).triggerHandler( "invalid-form", [ this ] );
  942. this.formSubmitted = false;
  943. }
  944. },
  945. previousValue: function( element, method ) {
  946. method = typeof method === "string" && method || "remote";
  947. return $.data( element, "previousValue" ) || $.data( element, "previousValue", {
  948. old: null,
  949. valid: true,
  950. message: this.defaultMessage( element, { method: method } )
  951. } );
  952. },
  953. // Cleans up all forms and elements, removes validator-specific events
  954. destroy: function() {
  955. this.resetForm();
  956. $( this.currentForm )
  957. .off( ".validate" )
  958. .removeData( "validator" )
  959. .find( ".validate-equalTo-blur" )
  960. .off( ".validate-equalTo" )
  961. .removeClass( "validate-equalTo-blur" );
  962. }
  963. },
  964. classRuleSettings: {
  965. required: { required: true },
  966. email: { email: true },
  967. url: { url: true },
  968. date: { date: true },
  969. dateISO: { dateISO: true },
  970. number: { number: true },
  971. digits: { digits: true },
  972. creditcard: { creditcard: true }
  973. },
  974. addClassRules: function( className, rules ) {
  975. if ( className.constructor === String ) {
  976. this.classRuleSettings[ className ] = rules;
  977. } else {
  978. $.extend( this.classRuleSettings, className );
  979. }
  980. },
  981. classRules: function( element ) {
  982. var rules = {},
  983. classes = $( element ).attr( "class" );
  984. if ( classes ) {
  985. $.each( classes.split( " " ), function() {
  986. if ( this in $.validator.classRuleSettings ) {
  987. $.extend( rules, $.validator.classRuleSettings[ this ] );
  988. }
  989. } );
  990. }
  991. return rules;
  992. },
  993. normalizeAttributeRule: function( rules, type, method, value ) {
  994. // Convert the value to a number for number inputs, and for text for backwards compability
  995. // allows type="date" and others to be compared as strings
  996. if ( /min|max|step/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
  997. value = Number( value );
  998. // Support Opera Mini, which returns NaN for undefined minlength
  999. if ( isNaN( value ) ) {
  1000. value = undefined;
  1001. }
  1002. }
  1003. if ( value || value === 0 ) {
  1004. rules[ method ] = value;
  1005. } else if ( type === method && type !== "range" ) {
  1006. // Exception: the jquery validate 'range' method
  1007. // does not test for the html5 'range' type
  1008. rules[ method ] = true;
  1009. }
  1010. },
  1011. attributeRules: function( element ) {
  1012. var rules = {},
  1013. $element = $( element ),
  1014. type = element.getAttribute( "type" ),
  1015. method, value;
  1016. for ( method in $.validator.methods ) {
  1017. // Support for <input required> in both html5 and older browsers
  1018. if ( method === "required" ) {
  1019. value = element.getAttribute( method );
  1020. // Some browsers return an empty string for the required attribute
  1021. // and non-HTML5 browsers might have required="" markup
  1022. if ( value === "" ) {
  1023. value = true;
  1024. }
  1025. // Force non-HTML5 browsers to return bool
  1026. value = !!value;
  1027. } else {
  1028. value = $element.attr( method );
  1029. }
  1030. this.normalizeAttributeRule( rules, type, method, value );
  1031. }
  1032. // 'maxlength' may be returned as -1, 2147483647 ( IE ) and 524288 ( safari ) for text inputs
  1033. if ( rules.maxlength && /-1|2147483647|524288/.test( rules.maxlength ) ) {
  1034. delete rules.maxlength;
  1035. }
  1036. return rules;
  1037. },
  1038. dataRules: function( element ) {
  1039. var rules = {},
  1040. $element = $( element ),
  1041. type = element.getAttribute( "type" ),
  1042. method, value;
  1043. for ( method in $.validator.methods ) {
  1044. value = $element.data( "rule" + method.charAt( 0 ).toUpperCase() + method.substring( 1 ).toLowerCase() );
  1045. this.normalizeAttributeRule( rules, type, method, value );
  1046. }
  1047. return rules;
  1048. },
  1049. staticRules: function( element ) {
  1050. var rules = {},
  1051. validator = $.data( element.form, "validator" );
  1052. if ( validator.settings.rules ) {
  1053. rules = $.validator.normalizeRule( validator.settings.rules[ element.name ] ) || {};
  1054. }
  1055. return rules;
  1056. },
  1057. normalizeRules: function( rules, element ) {
  1058. // Handle dependency check
  1059. $.each( rules, function( prop, val ) {
  1060. // Ignore rule when param is explicitly false, eg. required:false
  1061. if ( val === false ) {
  1062. delete rules[ prop ];
  1063. return;
  1064. }
  1065. if ( val.param || val.depends ) {
  1066. var keepRule = true;
  1067. switch ( typeof val.depends ) {
  1068. case "string":
  1069. keepRule = !!$( val.depends, element.form ).length;
  1070. break;
  1071. case "function":
  1072. keepRule = val.depends.call( element, element );
  1073. break;
  1074. }
  1075. if ( keepRule ) {
  1076. rules[ prop ] = val.param !== undefined ? val.param : true;
  1077. } else {
  1078. $.data( element.form, "validator" ).resetElements( $( element ) );
  1079. delete rules[ prop ];
  1080. }
  1081. }
  1082. } );
  1083. // Evaluate parameters
  1084. $.each( rules, function( rule, parameter ) {
  1085. rules[ rule ] = $.isFunction( parameter ) && rule !== "normalizer" ? parameter( element ) : parameter;
  1086. } );
  1087. // Clean number parameters
  1088. $.each( [ "minlength", "maxlength" ], function() {
  1089. if ( rules[ this ] ) {
  1090. rules[ this ] = Number( rules[ this ] );
  1091. }
  1092. } );
  1093. $.each( [ "rangelength", "range" ], function() {
  1094. var parts;
  1095. if ( rules[ this ] ) {
  1096. if ( $.isArray( rules[ this ] ) ) {
  1097. rules[ this ] = [ Number( rules[ this ][ 0 ] ), Number( rules[ this ][ 1 ] ) ];
  1098. } else if ( typeof rules[ this ] === "string" ) {
  1099. parts = rules[ this ].replace( /[\[\]]/g, "" ).split( /[\s,]+/ );
  1100. rules[ this ] = [ Number( parts[ 0 ] ), Number( parts[ 1 ] ) ];
  1101. }
  1102. }
  1103. } );
  1104. if ( $.validator.autoCreateRanges ) {
  1105. // Auto-create ranges
  1106. if ( rules.min != null && rules.max != null ) {
  1107. rules.range = [ rules.min, rules.max ];
  1108. delete rules.min;
  1109. delete rules.max;
  1110. }
  1111. if ( rules.minlength != null && rules.maxlength != null ) {
  1112. rules.rangelength = [ rules.minlength, rules.maxlength ];
  1113. delete rules.minlength;
  1114. delete rules.maxlength;
  1115. }
  1116. }
  1117. return rules;
  1118. },
  1119. // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
  1120. normalizeRule: function( data ) {
  1121. if ( typeof data === "string" ) {
  1122. var transformed = {};
  1123. $.each( data.split( /\s/ ), function() {
  1124. transformed[ this ] = true;
  1125. } );
  1126. data = transformed;
  1127. }
  1128. return data;
  1129. },
  1130. // http://jqueryvalidation.org/jQuery.validator.addMethod/
  1131. addMethod: function( name, method, message ) {
  1132. $.validator.methods[ name ] = method;
  1133. $.validator.messages[ name ] = message !== undefined ? message : $.validator.messages[ name ];
  1134. if ( method.length < 3 ) {
  1135. $.validator.addClassRules( name, $.validator.normalizeRule( name ) );
  1136. }
  1137. },
  1138. // http://jqueryvalidation.org/jQuery.validator.methods/
  1139. methods: {
  1140. // http://jqueryvalidation.org/required-method/
  1141. required: function( value, element, param ) {
  1142. // Check if dependency is met
  1143. if ( !this.depend( param, element ) ) {
  1144. return "dependency-mismatch";
  1145. }
  1146. if ( element.nodeName.toLowerCase() === "select" ) {
  1147. // Could be an array for select-multiple or a string, both are fine this way
  1148. var val = $( element ).val();
  1149. return val && val.length > 0;
  1150. }
  1151. if ( this.checkable( element ) ) {
  1152. return this.getLength( value, element ) > 0;
  1153. }
  1154. return value.length > 0;
  1155. },
  1156. // http://jqueryvalidation.org/email-method/
  1157. email: function( value, element ) {
  1158. // From https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
  1159. // Retrieved 2014-01-14
  1160. // If you have a problem with this implementation, report a bug against the above spec
  1161. // Or use custom methods to implement your own email validation
  1162. 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 );
  1163. },
  1164. // http://jqueryvalidation.org/url-method/
  1165. url: function( value, element ) {
  1166. // Copyright (c) 2010-2013 Diego Perini, MIT licensed
  1167. // https://gist.github.com/dperini/729294
  1168. // see also https://mathiasbynens.be/demo/url-regex
  1169. // modified to allow protocol-relative URLs
  1170. 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 );
  1171. },
  1172. // http://jqueryvalidation.org/date-method/
  1173. date: function( value, element ) {
  1174. return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() );
  1175. },
  1176. // http://jqueryvalidation.org/dateISO-method/
  1177. dateISO: function( value, element ) {
  1178. return this.optional( element ) || /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test( value );
  1179. },
  1180. // http://jqueryvalidation.org/number-method/
  1181. number: function( value, element ) {
  1182. return this.optional( element ) || /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( value );
  1183. },
  1184. // http://jqueryvalidation.org/digits-method/
  1185. digits: function( value, element ) {
  1186. return this.optional( element ) || /^\d+$/.test( value );
  1187. },
  1188. // http://jqueryvalidation.org/minlength-method/
  1189. minlength: function( value, element, param ) {
  1190. var length = $.isArray( value ) ? value.length : this.getLength( value, element );
  1191. return this.optional( element ) || length >= param;
  1192. },
  1193. // http://jqueryvalidation.org/maxlength-method/
  1194. maxlength: function( value, element, param ) {
  1195. var length = $.isArray( value ) ? value.length : this.getLength( value, element );
  1196. return this.optional( element ) || length <= param;
  1197. },
  1198. // http://jqueryvalidation.org/rangelength-method/
  1199. rangelength: function( value, element, param ) {
  1200. var length = $.isArray( value ) ? value.length : this.getLength( value, element );
  1201. return this.optional( element ) || ( length >= param[ 0 ] && length <= param[ 1 ] );
  1202. },
  1203. // http://jqueryvalidation.org/min-method/
  1204. min: function( value, element, param ) {
  1205. return this.optional( element ) || value >= param;
  1206. },
  1207. // http://jqueryvalidation.org/max-method/
  1208. max: function( value, element, param ) {
  1209. return this.optional( element ) || value <= param;
  1210. },
  1211. // http://jqueryvalidation.org/range-method/
  1212. range: function( value, element, param ) {
  1213. return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] );
  1214. },
  1215. // http://jqueryvalidation.org/step-method/
  1216. step: function( value, element, param ) {
  1217. var type = $( element ).attr( "type" ),
  1218. errorMessage = "Step attribute on input type " + type + " is not supported.",
  1219. supportedTypes = [ "text", "number", "range" ],
  1220. re = new RegExp( "\\b" + type + "\\b" ),
  1221. notSupported = type && !re.test( supportedTypes.join() ),
  1222. decimalPlaces = function( num ) {
  1223. var match = ( "" + num ).match( /(?:\.(\d+))?$/ );
  1224. if ( !match ) {
  1225. return 0;
  1226. }
  1227. // Number of digits right of decimal point.
  1228. return match[ 1 ] ? match[ 1 ].length : 0;
  1229. },
  1230. toInt = function( num ) {
  1231. return Math.round( num * Math.pow( 10, decimals ) );
  1232. },
  1233. valid = true,
  1234. decimals;
  1235. // Works only for text, number and range input types
  1236. // TODO find a way to support input types date, datetime, datetime-local, month, time and week
  1237. if ( notSupported ) {
  1238. throw new Error( errorMessage );
  1239. }
  1240. decimals = decimalPlaces( param );
  1241. // Value can't have too many decimals
  1242. if ( decimalPlaces( value ) > decimals || toInt( value ) % toInt( param ) !== 0 ) {
  1243. valid = false;
  1244. }
  1245. return this.optional( element ) || valid;
  1246. },
  1247. // http://jqueryvalidation.org/equalTo-method/
  1248. equalTo: function( value, element, param ) {
  1249. // Bind to the blur event of the target in order to revalidate whenever the target field is updated
  1250. var target = $( param );
  1251. if ( this.settings.onfocusout && target.not( ".validate-equalTo-blur" ).length ) {
  1252. target.addClass( "validate-equalTo-blur" ).on( "blur.validate-equalTo", function() {
  1253. $( element ).valid();
  1254. } );
  1255. }
  1256. return value === target.val();
  1257. },
  1258. // http://jqueryvalidation.org/remote-method/
  1259. remote: function( value, element, param, method ) {
  1260. if ( this.optional( element ) ) {
  1261. return "dependency-mismatch";
  1262. }
  1263. method = typeof method === "string" && method || "remote";
  1264. var previous = this.previousValue( element, method ),
  1265. validator, data, optionDataString;
  1266. if ( !this.settings.messages[ element.name ] ) {
  1267. this.settings.messages[ element.name ] = {};
  1268. }
  1269. previous.originalMessage = previous.originalMessage || this.settings.messages[ element.name ][ method ];
  1270. this.settings.messages[ element.name ][ method ] = previous.message;
  1271. param = typeof param === "string" && { url: param } || param;
  1272. optionDataString = $.param( $.extend( { data: value }, param.data ) );
  1273. if ( previous.old === optionDataString ) {
  1274. return previous.valid;
  1275. }
  1276. previous.old = optionDataString;
  1277. validator = this;
  1278. this.startRequest( element );
  1279. data = {};
  1280. data[ element.name ] = value;
  1281. $.ajax( $.extend( true, {
  1282. mode: "abort",
  1283. port: "validate" + element.name,
  1284. dataType: "json",
  1285. data: data,
  1286. context: validator.currentForm,
  1287. success: function( response ) {
  1288. var valid = response === true || response === "true",
  1289. errors, message, submitted;
  1290. validator.settings.messages[ element.name ][ method ] = previous.originalMessage;
  1291. if ( valid ) {
  1292. submitted = validator.formSubmitted;
  1293. validator.resetInternals();
  1294. validator.toHide = validator.errorsFor( element );
  1295. validator.formSubmitted = submitted;
  1296. validator.successList.push( element );
  1297. validator.invalid[ element.name ] = false;
  1298. validator.showErrors();
  1299. } else {
  1300. errors = {};
  1301. message = response || validator.defaultMessage( element, { method: method, parameters: value } );
  1302. errors[ element.name ] = previous.message = message;
  1303. validator.invalid[ element.name ] = true;
  1304. validator.showErrors( errors );
  1305. }
  1306. previous.valid = valid;
  1307. validator.stopRequest( element, valid );
  1308. }
  1309. }, param ) );
  1310. return "pending";
  1311. }
  1312. }
  1313. } );
  1314. // Ajax mode: abort
  1315. // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
  1316. // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
  1317. var pendingRequests = {},
  1318. ajax;
  1319. // Use a prefilter if available (1.5+)
  1320. if ( $.ajaxPrefilter ) {
  1321. $.ajaxPrefilter( function( settings, _, xhr ) {
  1322. var port = settings.port;
  1323. if ( settings.mode === "abort" ) {
  1324. if ( pendingRequests[ port ] ) {
  1325. pendingRequests[ port ].abort();
  1326. }
  1327. pendingRequests[ port ] = xhr;
  1328. }
  1329. } );
  1330. } else {
  1331. // Proxy ajax
  1332. ajax = $.ajax;
  1333. $.ajax = function( settings ) {
  1334. var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode,
  1335. port = ( "port" in settings ? settings : $.ajaxSettings ).port;
  1336. if ( mode === "abort" ) {
  1337. if ( pendingRequests[ port ] ) {
  1338. pendingRequests[ port ].abort();
  1339. }
  1340. pendingRequests[ port ] = ajax.apply( this, arguments );
  1341. return pendingRequests[ port ];
  1342. }
  1343. return ajax.apply( this, arguments );
  1344. };
  1345. }
  1346. }));