/Visual Studio 2008/CSASPNETMVCDataView/Scripts/jquery.validate.min-vsdoc.js

# · JavaScript · 1283 lines · 892 code · 148 blank · 243 comment · 203 complexity · 7a5f69e8e9578c6b5074157e63e72b3b MD5 · raw file

  1. /*
  2. * This file has been commented to support Visual Studio Intellisense.
  3. * You should not use this file at runtime inside the browser--it is only
  4. * intended to be used only for design-time IntelliSense. Please use the
  5. * standard jQuery library for all production use.
  6. *
  7. * Comment version: 1.5.5a
  8. */
  9. /*
  10. * jQuery validation plug-in 1.5.5
  11. *
  12. * http://bassistance.de/jquery-plugins/jquery-plugin-validation/
  13. * http://docs.jquery.com/Plugins/Validation
  14. *
  15. * Copyright (c) 2006 - 2008 JĂśrn Zaefferer
  16. *
  17. * $Id: jquery.validate.js 6403 2009-06-17 14:27:16Z joern.zaefferer $
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining
  20. * a copy of this software and associated documentation files (the
  21. * "Software"), to deal in the Software without restriction, including
  22. * without limitation the rights to use, copy, modify, merge, publish,
  23. * distribute, sublicense, and/or sell copies of the Software, and to
  24. * permit persons to whom the Software is furnished to do so, subject to
  25. * the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be
  28. * included in all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. */
  38. (function($) {
  39. $.extend($.fn, {
  40. // http://docs.jquery.com/Plugins/Validation/validate
  41. validate: function( options ) {
  42. /// <summary>
  43. /// Validates the selected form. This method sets up event handlers for submit, focus,
  44. /// keyup, blur and click to trigger validation of the entire form or individual
  45. /// elements. Each one can be disabled, see the onxxx options (onsubmit, onfocusout,
  46. /// onkeyup, onclick). focusInvalid focuses elements when submitting a invalid form.
  47. /// </summary>
  48. /// <param name="options" type="Options">
  49. /// A set of key/value pairs that configure the validate. All options are optional.
  50. /// </param>
  51. /// <returns type="Validator" />
  52. // if nothing is selected, return nothing; can't chain anyway
  53. if (!this.length) {
  54. options && options.debug && window.console && console.warn( "nothing selected, can't validate, returning nothing" );
  55. return;
  56. }
  57. // check if a validator for this form was already created
  58. var validator = $.data(this[0], 'validator');
  59. if ( validator ) {
  60. return validator;
  61. }
  62. validator = new $.validator( options, this[0] );
  63. $.data(this[0], 'validator', validator);
  64. if ( validator.settings.onsubmit ) {
  65. // allow suppresing validation by adding a cancel class to the submit button
  66. this.find("input, button").filter(".cancel").click(function() {
  67. validator.cancelSubmit = true;
  68. });
  69. // when a submitHandler is used, capture the submitting button
  70. if (validator.settings.submitHandler) {
  71. this.find("input, button").filter(":submit").click(function() {
  72. validator.submitButton = this;
  73. });
  74. }
  75. // validate the form on submit
  76. this.submit( function( event ) {
  77. if ( validator.settings.debug )
  78. // prevent form submit to be able to see console output
  79. event.preventDefault();
  80. function handle() {
  81. if ( validator.settings.submitHandler ) {
  82. if (validator.submitButton) {
  83. // insert a hidden input as a replacement for the missing submit button
  84. var hidden = $("<input type='hidden'/>").attr("name", validator.submitButton.name).val(validator.submitButton.value).appendTo(validator.currentForm);
  85. }
  86. validator.settings.submitHandler.call( validator, validator.currentForm );
  87. if (validator.submitButton) {
  88. // and clean up afterwards; thanks to no-block-scope, hidden can be referenced
  89. hidden.remove();
  90. }
  91. return false;
  92. }
  93. return true;
  94. }
  95. // prevent submit for invalid forms or custom submit handlers
  96. if ( validator.cancelSubmit ) {
  97. validator.cancelSubmit = false;
  98. return handle();
  99. }
  100. if ( validator.form() ) {
  101. if ( validator.pendingRequest ) {
  102. validator.formSubmitted = true;
  103. return false;
  104. }
  105. return handle();
  106. } else {
  107. validator.focusInvalid();
  108. return false;
  109. }
  110. });
  111. }
  112. return validator;
  113. },
  114. // http://docs.jquery.com/Plugins/Validation/valid
  115. valid: function() {
  116. /// <summary>
  117. /// Checks if the selected form is valid or if all selected elements are valid.
  118. /// validate() needs to be called on the form before checking it using this method.
  119. /// </summary>
  120. /// <returns type="Boolean" />
  121. if ( $(this[0]).is('form')) {
  122. return this.validate().form();
  123. } else {
  124. var valid = true;
  125. var validator = $(this[0].form).validate();
  126. this.each(function() {
  127. valid &= validator.element(this);
  128. });
  129. return valid;
  130. }
  131. },
  132. // attributes: space seperated list of attributes to retrieve and remove
  133. removeAttrs: function(attributes) {
  134. /// <summary>
  135. /// Remove the specified attributes from the first matched element and return them.
  136. /// </summary>
  137. /// <param name="attributes" type="String">
  138. /// A space-seperated list of attribute names to remove.
  139. /// </param>
  140. /// <returns type="" />
  141. var result = {},
  142. $element = this;
  143. $.each(attributes.split(/\s/), function(index, value) {
  144. result[value] = $element.attr(value);
  145. $element.removeAttr(value);
  146. });
  147. return result;
  148. },
  149. // http://docs.jquery.com/Plugins/Validation/rules
  150. rules: function(command, argument) {
  151. /// <summary>
  152. /// Return the validations rules for the first selected element.
  153. /// </summary>
  154. /// <param name="command" type="String">
  155. /// Can be either "add" or "remove".
  156. /// </param>
  157. /// <param name="argument" type="">
  158. /// A list of rules to add or remove.
  159. /// </param>
  160. /// <returns type="" />
  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( {}, $.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.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. if ( element.name in this.submitted )
  282. this.element(element);
  283. },
  284. highlight: function( element, errorClass, validClass ) {
  285. $(element).addClass(errorClass).removeClass(validClass);
  286. },
  287. unhighlight: function( element, errorClass, validClass ) {
  288. $(element).removeClass(errorClass).addClass(validClass);
  289. }
  290. },
  291. // http://docs.jquery.com/Plugins/Validation/Validator/setDefaults
  292. setDefaults: function(settings) {
  293. /// <summary>
  294. /// Modify default settings for validation.
  295. /// Accepts everything that Plugins/Validation/validate accepts.
  296. /// </summary>
  297. /// <param name="settings" type="Options">
  298. /// Options to set as default.
  299. /// </param>
  300. /// <returns type="undefined" />
  301. $.extend( $.validator.defaults, settings );
  302. },
  303. messages: {
  304. required: "This field is required.",
  305. remote: "Please fix this field.",
  306. email: "Please enter a valid email address.",
  307. url: "Please enter a valid URL.",
  308. date: "Please enter a valid date.",
  309. dateISO: "Please enter a valid date (ISO).",
  310. dateDE: "Bitte geben Sie ein gĂźltiges Datum ein.",
  311. number: "Please enter a valid number.",
  312. numberDE: "Bitte geben Sie eine Nummer ein.",
  313. digits: "Please enter only digits",
  314. creditcard: "Please enter a valid credit card number.",
  315. equalTo: "Please enter the same value again.",
  316. accept: "Please enter a value with a valid extension.",
  317. maxlength: $.validator.format("Please enter no more than {0} characters."),
  318. minlength: $.validator.format("Please enter at least {0} characters."),
  319. rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
  320. range: $.validator.format("Please enter a value between {0} and {1}."),
  321. max: $.validator.format("Please enter a value less than or equal to {0}."),
  322. min: $.validator.format("Please enter a value greater than or equal to {0}.")
  323. },
  324. autoCreateRanges: false,
  325. prototype: {
  326. init: function() {
  327. this.labelContainer = $(this.settings.errorLabelContainer);
  328. this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm);
  329. this.containers = $(this.settings.errorContainer).add( this.settings.errorLabelContainer );
  330. this.submitted = {};
  331. this.valueCache = {};
  332. this.pendingRequest = 0;
  333. this.pending = {};
  334. this.invalid = {};
  335. this.reset();
  336. var groups = (this.groups = {});
  337. $.each(this.settings.groups, function(key, value) {
  338. $.each(value.split(/\s/), function(index, name) {
  339. groups[name] = key;
  340. });
  341. });
  342. var rules = this.settings.rules;
  343. $.each(rules, function(key, value) {
  344. rules[key] = $.validator.normalizeRule(value);
  345. });
  346. function delegate(event) {
  347. var validator = $.data(this[0].form, "validator");
  348. validator.settings["on" + event.type] && validator.settings["on" + event.type].call(validator, this[0] );
  349. }
  350. $(this.currentForm)
  351. .delegate("focusin focusout keyup", ":text, :password, :file, select, textarea", delegate)
  352. .delegate("click", ":radio, :checkbox", delegate);
  353. if (this.settings.invalidHandler)
  354. $(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler);
  355. },
  356. // http://docs.jquery.com/Plugins/Validation/Validator/form
  357. form: function() {
  358. /// <summary>
  359. /// Validates the form, returns true if it is valid, false otherwise.
  360. /// This behaves as a normal submit event, but returns the result.
  361. /// </summary>
  362. /// <returns type="Boolean" />
  363. this.checkForm();
  364. $.extend(this.submitted, this.errorMap);
  365. this.invalid = $.extend({}, this.errorMap);
  366. if (!this.valid())
  367. $(this.currentForm).triggerHandler("invalid-form", [this]);
  368. this.showErrors();
  369. return this.valid();
  370. },
  371. checkForm: function() {
  372. this.prepareForm();
  373. for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
  374. this.check( elements[i] );
  375. }
  376. return this.valid();
  377. },
  378. // http://docs.jquery.com/Plugins/Validation/Validator/element
  379. element: function( element ) {
  380. /// <summary>
  381. /// Validates a single element, returns true if it is valid, false otherwise.
  382. /// This behaves as validation on blur or keyup, but returns the result.
  383. /// </summary>
  384. /// <param name="element" type="Selector">
  385. /// An element to validate, must be inside the validated form.
  386. /// </param>
  387. /// <returns type="Boolean" />
  388. element = this.clean( element );
  389. this.lastElement = element;
  390. this.prepareElement( element );
  391. this.currentElements = $(element);
  392. var result = this.check( element );
  393. if ( result ) {
  394. delete this.invalid[element.name];
  395. } else {
  396. this.invalid[element.name] = true;
  397. }
  398. if ( !this.numberOfInvalids() ) {
  399. // Hide error containers on last error
  400. this.toHide = this.toHide.add( this.containers );
  401. }
  402. this.showErrors();
  403. return result;
  404. },
  405. // http://docs.jquery.com/Plugins/Validation/Validator/showErrors
  406. showErrors: function(errors) {
  407. /// <summary>
  408. /// Show the specified messages.
  409. /// Keys have to refer to the names of elements, values are displayed for those elements, using the configured error placement.
  410. /// </summary>
  411. /// <param name="errors" type="Object">
  412. /// One or more key/value pairs of input names and messages.
  413. /// </param>
  414. /// <returns type="undefined" />
  415. if(errors) {
  416. // add items to error list and map
  417. $.extend( this.errorMap, errors );
  418. this.errorList = [];
  419. for ( var name in errors ) {
  420. this.errorList.push({
  421. message: errors[name],
  422. element: this.findByName(name)[0]
  423. });
  424. }
  425. // remove items from success list
  426. this.successList = $.grep( this.successList, function(element) {
  427. return !(element.name in errors);
  428. });
  429. }
  430. this.settings.showErrors
  431. ? this.settings.showErrors.call( this, this.errorMap, this.errorList )
  432. : this.defaultShowErrors();
  433. },
  434. // http://docs.jquery.com/Plugins/Validation/Validator/resetForm
  435. resetForm: function() {
  436. /// <summary>
  437. /// Resets the controlled form.
  438. /// Resets input fields to their original value (requires form plugin), removes classes
  439. /// indicating invalid elements and hides error messages.
  440. /// </summary>
  441. /// <returns type="undefined" />
  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 || []).filter(":visible").focus();
  479. } catch(e) {
  480. // ignore IE throwing errors when focusing hidden elements
  481. }
  482. }
  483. },
  484. findLastActive: function() {
  485. var lastActive = this.lastActive;
  486. return lastActive && $.grep(this.errorList, function(n) {
  487. return n.element.name == lastActive.name;
  488. }).length == 1 && lastActive;
  489. },
  490. elements: function() {
  491. var validator = this,
  492. rulesCache = {};
  493. // select all valid inputs inside the form (no submit or reset buttons)
  494. // workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
  495. return $([]).add(this.currentForm.elements)
  496. .filter(":input")
  497. .not(":submit, :reset, :image, [disabled]")
  498. .not( this.settings.ignore )
  499. .filter(function() {
  500. !this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);
  501. // select only the first element for each name, and only those with rules specified
  502. if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
  503. return false;
  504. rulesCache[this.name] = true;
  505. return true;
  506. });
  507. },
  508. clean: function( selector ) {
  509. return $( selector )[0];
  510. },
  511. errors: function() {
  512. return $( this.settings.errorElement + "." + this.settings.errorClass, this.errorContext );
  513. },
  514. reset: function() {
  515. this.successList = [];
  516. this.errorList = [];
  517. this.errorMap = {};
  518. this.toShow = $([]);
  519. this.toHide = $([]);
  520. this.formSubmitted = false;
  521. this.currentElements = $([]);
  522. },
  523. prepareForm: function() {
  524. this.reset();
  525. this.toHide = this.errors().add( this.containers );
  526. },
  527. prepareElement: function( element ) {
  528. this.reset();
  529. this.toHide = this.errorsFor(element);
  530. },
  531. check: function( element ) {
  532. element = this.clean( element );
  533. // if radio/checkbox, validate first element in group instead
  534. if (this.checkable(element)) {
  535. element = this.findByName( element.name )[0];
  536. }
  537. var rules = $(element).rules();
  538. var dependencyMismatch = false;
  539. for( method in rules ) {
  540. var rule = { method: method, parameters: rules[method] };
  541. try {
  542. var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters );
  543. // if a method indicates that the field is optional and therefore valid,
  544. // don't mark it as valid when there are no other rules
  545. if ( result == "dependency-mismatch" ) {
  546. dependencyMismatch = true;
  547. continue;
  548. }
  549. dependencyMismatch = false;
  550. if ( result == "pending" ) {
  551. this.toHide = this.toHide.not( this.errorsFor(element) );
  552. return;
  553. }
  554. if( !result ) {
  555. this.formatAndAdd( element, rule );
  556. return false;
  557. }
  558. } catch(e) {
  559. this.settings.debug && window.console && console.log("exception occured when checking element " + element.id
  560. + ", check the '" + rule.method + "' method");
  561. throw e;
  562. }
  563. }
  564. if (dependencyMismatch)
  565. return;
  566. if ( this.objectLength(rules) )
  567. this.successList.push(element);
  568. return true;
  569. },
  570. // return the custom message for the given element and validation method
  571. // specified in the element's "messages" metadata
  572. customMetaMessage: function(element, method) {
  573. if (!$.metadata)
  574. return;
  575. var meta = this.settings.meta
  576. ? $(element).metadata()[this.settings.meta]
  577. : $(element).metadata();
  578. return meta && meta.messages && meta.messages[method];
  579. },
  580. // return the custom message for the given element name and validation method
  581. customMessage: function( name, method ) {
  582. var m = this.settings.messages[name];
  583. return m && (m.constructor == String
  584. ? m
  585. : m[method]);
  586. },
  587. // return the first defined argument, allowing empty strings
  588. findDefined: function() {
  589. for(var i = 0; i < arguments.length; i++) {
  590. if (arguments[i] !== undefined)
  591. return arguments[i];
  592. }
  593. return undefined;
  594. },
  595. defaultMessage: function( element, method) {
  596. return this.findDefined(
  597. this.customMessage( element.name, method ),
  598. this.customMetaMessage( element, method ),
  599. // title is never undefined, so handle empty string as undefined
  600. !this.settings.ignoreTitle && element.title || undefined,
  601. $.validator.messages[method],
  602. "<strong>Warning: No message defined for " + element.name + "</strong>"
  603. );
  604. },
  605. formatAndAdd: function( element, rule ) {
  606. var message = this.defaultMessage( element, rule.method );
  607. if ( typeof message == "function" )
  608. message = message.call(this, rule.parameters, element);
  609. this.errorList.push({
  610. message: message,
  611. element: element
  612. });
  613. this.errorMap[element.name] = message;
  614. this.submitted[element.name] = message;
  615. },
  616. addWrapper: function(toToggle) {
  617. if ( this.settings.wrapper )
  618. toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) );
  619. return toToggle;
  620. },
  621. defaultShowErrors: function() {
  622. for ( var i = 0; this.errorList[i]; i++ ) {
  623. var error = this.errorList[i];
  624. this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
  625. this.showLabel( error.element, error.message );
  626. }
  627. if( this.errorList.length ) {
  628. this.toShow = this.toShow.add( this.containers );
  629. }
  630. if (this.settings.success) {
  631. for ( var i = 0; this.successList[i]; i++ ) {
  632. this.showLabel( this.successList[i] );
  633. }
  634. }
  635. if (this.settings.unhighlight) {
  636. for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
  637. this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass );
  638. }
  639. }
  640. this.toHide = this.toHide.not( this.toShow );
  641. this.hideErrors();
  642. this.addWrapper( this.toShow ).show();
  643. },
  644. validElements: function() {
  645. return this.currentElements.not(this.invalidElements());
  646. },
  647. invalidElements: function() {
  648. return $(this.errorList).map(function() {
  649. return this.element;
  650. });
  651. },
  652. showLabel: function(element, message) {
  653. var label = this.errorsFor( element );
  654. if ( label.length ) {
  655. // refresh error/success class
  656. label.removeClass().addClass( this.settings.errorClass );
  657. // check if we have a generated label, replace the message then
  658. label.attr("generated") && label.html(message);
  659. } else {
  660. // create label
  661. label = $("<" + this.settings.errorElement + "/>")
  662. .attr({"for": this.idOrName(element), generated: true})
  663. .addClass(this.settings.errorClass)
  664. .html(message || "");
  665. if ( this.settings.wrapper ) {
  666. // make sure the element is visible, even in IE
  667. // actually showing the wrapped element is handled elsewhere
  668. label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
  669. }
  670. if ( !this.labelContainer.append(label).length )
  671. this.settings.errorPlacement
  672. ? this.settings.errorPlacement(label, $(element) )
  673. : label.insertAfter(element);
  674. }
  675. if ( !message && this.settings.success ) {
  676. label.text("");
  677. typeof this.settings.success == "string"
  678. ? label.addClass( this.settings.success )
  679. : this.settings.success( label );
  680. }
  681. this.toShow = this.toShow.add(label);
  682. },
  683. errorsFor: function(element) {
  684. return this.errors().filter("[for='" + this.idOrName(element) + "']");
  685. },
  686. idOrName: function(element) {
  687. return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name);
  688. },
  689. checkable: function( element ) {
  690. return /radio|checkbox/i.test(element.type);
  691. },
  692. findByName: function( name ) {
  693. // select by name and filter by form for performance over form.find("[name=...]")
  694. var form = this.currentForm;
  695. return $(document.getElementsByName(name)).map(function(index, element) {
  696. return element.form == form && element.name == name && element || null;
  697. });
  698. },
  699. getLength: function(value, element) {
  700. switch( element.nodeName.toLowerCase() ) {
  701. case 'select':
  702. return $("option:selected", element).length;
  703. case 'input':
  704. if( this.checkable( element) )
  705. return this.findByName(element.name).filter(':checked').length;
  706. }
  707. return value.length;
  708. },
  709. depend: function(param, element) {
  710. return this.dependTypes[typeof param]
  711. ? this.dependTypes[typeof param](param, element)
  712. : true;
  713. },
  714. dependTypes: {
  715. "boolean": function(param, element) {
  716. return param;
  717. },
  718. "string": function(param, element) {
  719. return !!$(param, element.form).length;
  720. },
  721. "function": function(param, element) {
  722. return param(element);
  723. }
  724. },
  725. optional: function(element) {
  726. return !$.validator.methods.required.call(this, $.trim(element.value), element) && "dependency-mismatch";
  727. },
  728. startRequest: function(element) {
  729. if (!this.pending[element.name]) {
  730. this.pendingRequest++;
  731. this.pending[element.name] = true;
  732. }
  733. },
  734. stopRequest: function(element, valid) {
  735. this.pendingRequest--;
  736. // sometimes synchronization fails, make sure pendingRequest is never < 0
  737. if (this.pendingRequest < 0)
  738. this.pendingRequest = 0;
  739. delete this.pending[element.name];
  740. if ( valid && this.pendingRequest == 0 && this.formSubmitted && this.form() ) {
  741. $(this.currentForm).submit();
  742. } else if (!valid && this.pendingRequest == 0 && this.formSubmitted) {
  743. $(this.currentForm).triggerHandler("invalid-form", [this]);
  744. }
  745. },
  746. previousValue: function(element) {
  747. return $.data(element, "previousValue") || $.data(element, "previousValue", previous = {
  748. old: null,
  749. valid: true,
  750. message: this.defaultMessage( element, "remote" )
  751. });
  752. }
  753. },
  754. classRuleSettings: {
  755. required: {required: true},
  756. email: {email: true},
  757. url: {url: true},
  758. date: {date: true},
  759. dateISO: {dateISO: true},
  760. dateDE: {dateDE: true},
  761. number: {number: true},
  762. numberDE: {numberDE: true},
  763. digits: {digits: true},
  764. creditcard: {creditcard: true}
  765. },
  766. // http://docs.jquery.com/Plugins/Validation/Validator/addClassRules#namerules
  767. addClassRules: function(className, rules) {
  768. /// <summary>
  769. /// Add a compound class method - useful to refactor common combinations of rules into a single
  770. /// class.
  771. /// </summary>
  772. /// <param name="name" type="String">
  773. /// The name of the class rule to add
  774. /// </param>
  775. /// <param name="rules" type="Options">
  776. /// The compound rules
  777. /// </param>
  778. /// <returns type="undefined" />
  779. className.constructor == String ?
  780. this.classRuleSettings[className] = rules :
  781. $.extend(this.classRuleSettings, className);
  782. },
  783. classRules: function(element) {
  784. var rules = {};
  785. var classes = $(element).attr('class');
  786. classes && $.each(classes.split(' '), function() {
  787. if (this in $.validator.classRuleSettings) {
  788. $.extend(rules, $.validator.classRuleSettings[this]);
  789. }
  790. });
  791. return rules;
  792. },
  793. attributeRules: function(element) {
  794. var rules = {};
  795. var $element = $(element);
  796. for (method in $.validator.methods) {
  797. var value = $element.attr(method);
  798. if (value) {
  799. rules[method] = value;
  800. }
  801. }
  802. // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
  803. if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) {
  804. delete rules.maxlength;
  805. }
  806. return rules;
  807. },
  808. metadataRules: function(element) {
  809. if (!$.metadata) return {};
  810. var meta = $.data(element.form, 'validator').settings.meta;
  811. return meta ?
  812. $(element).metadata()[meta] :
  813. $(element).metadata();
  814. },
  815. staticRules: function(element) {
  816. var rules = {};
  817. var validator = $.data(element.form, 'validator');
  818. if (validator.settings.rules) {
  819. rules = $.validator.normalizeRule(validator.settings.rules[element.name]) || {};
  820. }
  821. return rules;
  822. },
  823. normalizeRules: function(rules, element) {
  824. // handle dependency check
  825. $.each(rules, function(prop, val) {
  826. // ignore rule when param is explicitly false, eg. required:false
  827. if (val === false) {
  828. delete rules[prop];
  829. return;
  830. }
  831. if (val.param || val.depends) {
  832. var keepRule = true;
  833. switch (typeof val.depends) {
  834. case "string":
  835. keepRule = !!$(val.depends, element.form).length;
  836. break;
  837. case "function":
  838. keepRule = val.depends.call(element, element);
  839. break;
  840. }
  841. if (keepRule) {
  842. rules[prop] = val.param !== undefined ? val.param : true;
  843. } else {
  844. delete rules[prop];
  845. }
  846. }
  847. });
  848. // evaluate parameters
  849. $.each(rules, function(rule, parameter) {
  850. rules[rule] = $.isFunction(parameter) ? parameter(element) : parameter;
  851. });
  852. // clean number parameters
  853. $.each(['minlength', 'maxlength', 'min', 'max'], function() {
  854. if (rules[this]) {
  855. rules[this] = Number(rules[this]);
  856. }
  857. });
  858. $.each(['rangelength', 'range'], function() {
  859. if (rules[this]) {
  860. rules[this] = [Number(rules[this][0]), Number(rules[this][1])];
  861. }
  862. });
  863. if ($.validator.autoCreateRanges) {
  864. // auto-create ranges
  865. if (rules.min && rules.max) {
  866. rules.range = [rules.min, rules.max];
  867. delete rules.min;
  868. delete rules.max;
  869. }
  870. if (rules.minlength && rules.maxlength) {
  871. rules.rangelength = [rules.minlength, rules.maxlength];
  872. delete rules.minlength;
  873. delete rules.maxlength;
  874. }
  875. }
  876. // To support custom messages in metadata ignore rule methods titled "messages"
  877. if (rules.messages) {
  878. delete rules.messages
  879. }
  880. return rules;
  881. },
  882. // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
  883. normalizeRule: function(data) {
  884. if( typeof data == "string" ) {
  885. var transformed = {};
  886. $.each(data.split(/\s/), function() {
  887. transformed[this] = true;
  888. });
  889. data = transformed;
  890. }
  891. return data;
  892. },
  893. // http://docs.jquery.com/Plugins/Validation/Validator/addMethod
  894. addMethod: function(name, method, message) {
  895. /// <summary>
  896. /// Add a custom validation method. It must consist of a name (must be a legal javascript
  897. /// identifier), a javascript based function and a default string message.
  898. /// </summary>
  899. /// <param name="name" type="String">
  900. /// The name of the method, used to identify and referencing it, must be a valid javascript
  901. /// identifier
  902. /// </param>
  903. /// <param name="method" type="Function">
  904. /// The actual method implementation, returning true if an element is valid
  905. /// </param>
  906. /// <param name="message" type="String" optional="true">
  907. /// (Optional) The default message to display for this method. Can be a function created by
  908. /// jQuery.validator.format(value). When undefined, an already existing message is used
  909. /// (handy for localization), otherwise the field-specific messages have to be defined.
  910. /// </param>
  911. /// <returns type="undefined" />
  912. $.validator.methods[name] = method;
  913. $.validator.messages[name] = message || $.validator.messages[name];
  914. if (method.length < 3) {
  915. $.validator.addClassRules(name, $.validator.normalizeRule(name));
  916. }
  917. },
  918. methods: {
  919. // http://docs.jquery.com/Plugins/Validation/Methods/required
  920. required: function(value, element, param) {
  921. // check if dependency is met
  922. if ( !this.depend(param, element) )
  923. return "dependency-mismatch";
  924. switch( element.nodeName.toLowerCase() ) {
  925. case 'select':
  926. var options = $("option:selected", element);
  927. return options.length > 0 && ( element.type == "select-multiple" || ($.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value).length > 0);
  928. case 'input':
  929. if ( this.checkable(element) )
  930. return this.getLength(value, element) > 0;
  931. default:
  932. return $.trim(value).length > 0;
  933. }
  934. },
  935. // http://docs.jquery.com/Plugins/Validation/Methods/remote
  936. remote: function(value, element, param) {
  937. if ( this.optional(element) )
  938. return "dependency-mismatch";
  939. var previous = this.previousValue(element);
  940. if (!this.settings.messages[element.name] )
  941. this.settings.messages[element.name] = {};
  942. this.settings.messages[element.name].remote = typeof previous.message == "function" ? previous.message(value) : previous.message;
  943. param = typeof param == "string" && {url:param} || param;
  944. if ( previous.old !== value ) {
  945. previous.old = value;
  946. var validator = this;
  947. this.startRequest(element);
  948. var data = {};
  949. data[element.name] = value;
  950. $.ajax($.extend(true, {
  951. url: param,
  952. mode: "abort",
  953. port: "validate" + element.name,
  954. dataType: "json",
  955. data: data,
  956. success: function(response) {
  957. var valid = response === true;
  958. if ( valid ) {
  959. var submitted = validator.formSubmitted;
  960. validator.prepareElement(element);
  961. validator.formSubmitted = submitted;
  962. validator.successList.push(element);
  963. validator.showErrors();
  964. } else {
  965. var errors = {};
  966. errors[element.name] = previous.message = response || validator.defaultMessage( element, "remote" );
  967. validator.showErrors(errors);
  968. }
  969. previous.valid = valid;
  970. validator.stopRequest(element, valid);
  971. }
  972. }, param));
  973. return "pending";
  974. } else if( this.pending[element.name] ) {
  975. return "pending";
  976. }
  977. return previous.valid;
  978. },
  979. // http://docs.jquery.com/Plugins/Validation/Methods/minlength
  980. minlength: function(value, element, param) {
  981. return this.optional(element) || this.getLength($.trim(value), element) >= param;
  982. },
  983. // http://docs.jquery.com/Plugins/Validation/Methods/maxlength
  984. maxlength: function(value, element, param) {
  985. return this.optional(element) || this.getLength($.trim(value), element) <= param;
  986. },
  987. // http://docs.jquery.com/Plugins/Validation/Methods/rangelength
  988. rangelength: function(value, element, param) {
  989. var length = this.getLength($.trim(value), element);
  990. return this.optional(element) || ( length >= param[0] && length <= param[1] );
  991. },
  992. // http://docs.jquery.com/Plugins/Validation/Methods/min
  993. min: function( value, element, param ) {
  994. return this.optional(element) || value >= param;
  995. },
  996. // http://docs.jquery.com/Plugins/Validation/Methods/max
  997. max: function( value, element, param ) {
  998. return this.optional(element) || value <= param;
  999. },
  1000. // http://docs.jquery.com/Plugins/Validation/Methods/range
  1001. range: function( value, element, param ) {
  1002. return this.optional(element) || ( value >= param[0] && value <= param[1] );
  1003. },
  1004. // http://docs.jquery.com/Plugins/Validation/Methods/email
  1005. email: function(value, element) {
  1006. // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
  1007. 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);
  1008. },
  1009. // http://docs.jquery.com/Plugins/Validation/Methods/url
  1010. url: function(value, element) {
  1011. // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
  1012. 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);
  1013. },
  1014. // http://docs.jquery.com/Plugins/Validation/Methods/date
  1015. date: function(value, element) {
  1016. return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
  1017. },
  1018. // http://docs.jquery.com/Plugins/Validation/Methods/dateISO
  1019. dateISO: function(value, element) {
  1020. return this.optional(element) || /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value);
  1021. },
  1022. // http://docs.jquery.com/Plugins/Validation/Methods/dateDE
  1023. dateDE: function(value, element) {
  1024. return this.optional(element) || /^\d\d?\.\d\d?\.\d\d\d?\d?$/.test(value);
  1025. },
  1026. // http://docs.jquery.com/Plugins/Validation/Methods/number
  1027. number: function(value, element) {
  1028. return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
  1029. },
  1030. // http://docs.jquery.com/Plugins/Validation/Methods/numberDE
  1031. numberDE: function(value, element) {
  1032. return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
  1033. },
  1034. // http://docs.jquery.com/Plugins/Validation/Methods/digits
  1035. digits: function(value, element) {
  1036. return this.optional(element) || /^\d+$/.test(value);
  1037. },
  1038. // http://docs.jquery.com/Plugins/Validation/Methods/creditcard
  1039. // based on http://en.wikipedia.org/wiki/Luhn
  1040. creditcard: function(value, element) {
  1041. if ( this.optional(element) )
  1042. return "dependency-mismatch";
  1043. // accept only digits and dashes
  1044. if (/[^0-9-]+/.test(value))
  1045. return false;
  1046. var nCheck = 0,
  1047. nDigit = 0,
  1048. bEven = false;
  1049. value = value.replace(/\D/g, "");
  1050. for (n = value.length - 1; n >= 0; n--) {
  1051. var cDigit = value.charAt(n);
  1052. var nDigit = parseInt(cDigit, 10);
  1053. if (bEven) {
  1054. if ((nDigit *= 2) > 9)
  1055. nDigit -= 9;
  1056. }
  1057. nCheck += nDigit;
  1058. bEven = !bEven;
  1059. }
  1060. return (nCheck % 10) == 0;
  1061. },
  1062. // http://docs.jquery.com/Plugins/Validation/Methods/accept
  1063. accept: function(value, element, param) {
  1064. param = typeof param == "string" ? param.replace(/,/g, '|') : "png|jpe?g|gif";
  1065. return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i"));
  1066. },
  1067. // http://docs.jquery.com/Plugins/Validation/Methods/equalTo
  1068. equalTo: function(value, element, param) {
  1069. return value == $(param).val();
  1070. }
  1071. }
  1072. });
  1073. // deprecated, use $.validator.format instead
  1074. $.format = $.validator.format;
  1075. })(jQuery);
  1076. // ajax mode: abort
  1077. // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
  1078. // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
  1079. ;(function($) {
  1080. var ajax = $.ajax;
  1081. var pendingRequests = {};
  1082. $.ajax = function(settings) {
  1083. // create settings for compatibility with ajaxSetup
  1084. settings = $.extend(settings, $.extend({}, $.ajaxSettings, settings));
  1085. var port = settings.port;
  1086. if (settings.mode == "abort") {
  1087. if ( pendingRequests[port] ) {
  1088. pendingRequests[port].abort();
  1089. }
  1090. return (pendingRequests[port] = ajax.apply(this, arguments));
  1091. }
  1092. return ajax.apply(this, arguments);
  1093. };
  1094. })(jQuery);
  1095. // provides cross-browser focusin and focusout events
  1096. // IE has native support, in other browsers, use event caputuring (neither bubbles)
  1097. // provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation
  1098. // handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target
  1099. // provides triggerEvent(type: String, target: Element) to trigger delegated events
  1100. ;(function($) {
  1101. $.each({
  1102. focus: 'focusin',
  1103. blur: 'focusout'
  1104. }, function( original, fix ){
  1105. $.event.special[fix] = {
  1106. setup:function() {
  1107. if ( $.browser.msie ) return false;
  1108. this.addEventListener( original, $.event.special[fix].handler, true );
  1109. },
  1110. teardown:function() {
  1111. if ( $.browser.msie ) return false;
  1112. this.removeEventListener( original,
  1113. $.event.special[fix].handler, true );
  1114. },
  1115. handler: function(e) {
  1116. arguments[0] = $.event.fix(e);
  1117. arguments[0].type = fix;
  1118. return $.event.handle.apply(this, arguments);
  1119. }
  1120. };
  1121. });
  1122. $.extend($.fn, {
  1123. delegate: function(type, delegate, handler) {
  1124. return this.bind(type, function(event) {
  1125. var target = $(event.target);
  1126. if (target.is(delegate)) {
  1127. return handler.apply(target, arguments);
  1128. }
  1129. });
  1130. },
  1131. triggerEvent: function(type, target) {
  1132. return this.triggerHandler(type, [$.event.fix({ type: type, target: target })]);
  1133. }
  1134. })
  1135. })(jQuery);