jquery.validate.js 53 KB

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