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.

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