You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1245 lines
40 KiB

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