PageRenderTime 71ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MM.Web/Scripts/jquery.validate-vsdoc.js

#
JavaScript | 1292 lines | 900 code | 146 blank | 246 comment | 202 complexity | 83355ac765c2fb008cd39212a6b2637e 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.6
  8. */
  9. /*
  10. * jQuery validation plug-in 1.6
  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. // 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. /// <returns type="undefined" />
  305. $.extend( $.validator.defaults, settings );
  306. },
  307. messages: {
  308. required: "This field is required.",
  309. remote: "Please fix this field.",
  310. email: "Please enter a valid email address.",
  311. url: "Please enter a valid URL.",
  312. date: "Please enter a valid date.",
  313. dateISO: "Please enter a valid date (ISO).",
  314. number: "Please enter a valid number.",
  315. digits: "Please enter only digits.",
  316. creditcard: "Please enter a valid credit card number.",
  317. equalTo: "Please enter the same value again.",
  318. accept: "Please enter a value with a valid extension.",
  319. maxlength: $.validator.format("Please enter no more than {0} characters."),
  320. minlength: $.validator.format("Please enter at least {0} characters."),
  321. rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
  322. range: $.validator.format("Please enter a value between {0} and {1}."),
  323. max: $.validator.format("Please enter a value less than or equal to {0}."),
  324. min: $.validator.format("Please enter a value greater than or equal to {0}.")
  325. },
  326. autoCreateRanges: false,
  327. prototype: {
  328. init: function() {
  329. this.labelContainer = $(this.settings.errorLabelContainer);
  330. this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm);
  331. this.containers = $(this.settings.errorContainer).add( this.settings.errorLabelContainer );
  332. this.submitted = {};
  333. this.valueCache = {};
  334. this.pendingRequest = 0;
  335. this.pending = {};
  336. this.invalid = {};
  337. this.reset();
  338. var groups = (this.groups = {});
  339. $.each(this.settings.groups, function(key, value) {
  340. $.each(value.split(/\s/), function(index, name) {
  341. groups[name] = key;
  342. });
  343. });
  344. var rules = this.settings.rules;
  345. $.each(rules, function(key, value) {
  346. rules[key] = $.validator.normalizeRule(value);
  347. });
  348. function delegate(event) {
  349. var validator = $.data(this[0].form, "validator");
  350. validator.settings["on" + event.type] && validator.settings["on" + event.type].call(validator, this[0] );
  351. }
  352. $(this.currentForm)
  353. .delegate("focusin focusout keyup", ":text, :password, :file, select, textarea", delegate)
  354. .delegate("click", ":radio, :checkbox, select, option", 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. /// <returns type="undefined" />
  417. if(errors) {
  418. // add items to error list and map
  419. $.extend( this.errorMap, errors );
  420. this.errorList = [];
  421. for ( var name in errors ) {
  422. this.errorList.push({
  423. message: errors[name],
  424. element: this.findByName(name)[0]
  425. });
  426. }
  427. // remove items from success list
  428. this.successList = $.grep( this.successList, function(element) {
  429. return !(element.name in errors);
  430. });
  431. }
  432. this.settings.showErrors
  433. ? this.settings.showErrors.call( this, this.errorMap, this.errorList )
  434. : this.defaultShowErrors();
  435. },
  436. // http://docs.jquery.com/Plugins/Validation/Validator/resetForm
  437. resetForm: function() {
  438. /// <summary>
  439. /// Resets the controlled form.
  440. /// Resets input fields to their original value (requires form plugin), removes classes
  441. /// indicating invalid elements and hides error messages.
  442. /// </summary>
  443. /// <returns type="undefined" />
  444. if ( $.fn.resetForm )
  445. $( this.currentForm ).resetForm();
  446. this.submitted = {};
  447. this.prepareForm();
  448. this.hideErrors();
  449. this.elements().removeClass( this.settings.errorClass );
  450. },
  451. numberOfInvalids: function() {
  452. /// <summary>
  453. /// Returns the number of invalid fields.
  454. /// This depends on the internal validator state. It covers all fields only after
  455. /// validating the complete form (on submit or via $("form").valid()). After validating
  456. /// a single element, only that element is counted. Most useful in combination with the
  457. /// invalidHandler-option.
  458. /// </summary>
  459. /// <returns type="Number" />
  460. return this.objectLength(this.invalid);
  461. },
  462. objectLength: function( obj ) {
  463. var count = 0;
  464. for ( var i in obj )
  465. count++;
  466. return count;
  467. },
  468. hideErrors: function() {
  469. this.addWrapper( this.toHide ).hide();
  470. },
  471. valid: function() {
  472. return this.size() == 0;
  473. },
  474. size: function() {
  475. return this.errorList.length;
  476. },
  477. focusInvalid: function() {
  478. if( this.settings.focusInvalid ) {
  479. try {
  480. $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []).filter(":visible").focus();
  481. } catch(e) {
  482. // ignore IE throwing errors when focusing hidden elements
  483. }
  484. }
  485. },
  486. findLastActive: function() {
  487. var lastActive = this.lastActive;
  488. return lastActive && $.grep(this.errorList, function(n) {
  489. return n.element.name == lastActive.name;
  490. }).length == 1 && lastActive;
  491. },
  492. elements: function() {
  493. var validator = this,
  494. rulesCache = {};
  495. // select all valid inputs inside the form (no submit or reset buttons)
  496. // workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
  497. return $([]).add(this.currentForm.elements)
  498. .filter(":input")
  499. .not(":submit, :reset, :image, [disabled]")
  500. .not( this.settings.ignore )
  501. .filter(function() {
  502. !this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);
  503. // select only the first element for each name, and only those with rules specified
  504. if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
  505. return false;
  506. rulesCache[this.name] = true;
  507. return true;
  508. });
  509. },
  510. clean: function( selector ) {
  511. return $( selector )[0];
  512. },
  513. errors: function() {
  514. return $( this.settings.errorElement + "." + this.settings.errorClass, this.errorContext );
  515. },
  516. reset: function() {
  517. this.successList = [];
  518. this.errorList = [];
  519. this.errorMap = {};
  520. this.toShow = $([]);
  521. this.toHide = $([]);
  522. this.currentElements = $([]);
  523. },
  524. prepareForm: function() {
  525. this.reset();
  526. this.toHide = this.errors().add( this.containers );
  527. },
  528. prepareElement: function( element ) {
  529. this.reset();
  530. this.toHide = this.errorsFor(element);
  531. },
  532. check: function( element ) {
  533. element = this.clean( element );
  534. // if radio/checkbox, validate first element in group instead
  535. if (this.checkable(element)) {
  536. element = this.findByName( element.name )[0];
  537. }
  538. var rules = $(element).rules();
  539. var dependencyMismatch = false;
  540. for( method in rules ) {
  541. var rule = { method: method, parameters: rules[method] };
  542. try {
  543. var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters );
  544. // if a method indicates that the field is optional and therefore valid,
  545. // don't mark it as valid when there are no other rules
  546. if ( result == "dependency-mismatch" ) {
  547. dependencyMismatch = true;
  548. continue;
  549. }
  550. dependencyMismatch = false;
  551. if ( result == "pending" ) {
  552. this.toHide = this.toHide.not( this.errorsFor(element) );
  553. return;
  554. }
  555. if( !result ) {
  556. this.formatAndAdd( element, rule );
  557. return false;
  558. }
  559. } catch(e) {
  560. this.settings.debug && window.console && console.log("exception occured when checking element " + element.id
  561. + ", check the '" + rule.method + "' method", e);
  562. throw e;
  563. }
  564. }
  565. if (dependencyMismatch)
  566. return;
  567. if ( this.objectLength(rules) )
  568. this.successList.push(element);
  569. return true;
  570. },
  571. // return the custom message for the given element and validation method
  572. // specified in the element's "messages" metadata
  573. customMetaMessage: function(element, method) {
  574. if (!$.metadata)
  575. return;
  576. var meta = this.settings.meta
  577. ? $(element).metadata()[this.settings.meta]
  578. : $(element).metadata();
  579. return meta && meta.messages && meta.messages[method];
  580. },
  581. // return the custom message for the given element name and validation method
  582. customMessage: function( name, method ) {
  583. var m = this.settings.messages[name];
  584. return m && (m.constructor == String
  585. ? m
  586. : m[method]);
  587. },
  588. // return the first defined argument, allowing empty strings
  589. findDefined: function() {
  590. for(var i = 0; i < arguments.length; i++) {
  591. if (arguments[i] !== undefined)
  592. return arguments[i];
  593. }
  594. return undefined;
  595. },
  596. defaultMessage: function( element, method) {
  597. return this.findDefined(
  598. this.customMessage( element.name, method ),
  599. this.customMetaMessage( element, method ),
  600. // title is never undefined, so handle empty string as undefined
  601. !this.settings.ignoreTitle && element.title || undefined,
  602. $.validator.messages[method],
  603. "<strong>Warning: No message defined for " + element.name + "</strong>"
  604. );
  605. },
  606. formatAndAdd: function( element, rule ) {
  607. var message = this.defaultMessage( element, rule.method ),
  608. theregex = /\$?\{(\d+)\}/g;
  609. if ( typeof message == "function" ) {
  610. message = message.call(this, rule.parameters, element);
  611. } else if (theregex.test(message)) {
  612. message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters);
  613. }
  614. this.errorList.push({
  615. message: message,
  616. element: element
  617. });
  618. this.errorMap[element.name] = message;
  619. this.submitted[element.name] = message;
  620. },
  621. addWrapper: function(toToggle) {
  622. if ( this.settings.wrapper )
  623. toToggle = toToggle.add( toToggle.parent( this.settings.wrapper ) );
  624. return toToggle;
  625. },
  626. defaultShowErrors: function() {
  627. for ( var i = 0; this.errorList[i]; i++ ) {
  628. var error = this.errorList[i];
  629. this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass );
  630. this.showLabel( error.element, error.message );
  631. }
  632. if( this.errorList.length ) {
  633. this.toShow = this.toShow.add( this.containers );
  634. }
  635. if (this.settings.success) {
  636. for ( var i = 0; this.successList[i]; i++ ) {
  637. this.showLabel( this.successList[i] );
  638. }
  639. }
  640. if (this.settings.unhighlight) {
  641. for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
  642. this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass );
  643. }
  644. }
  645. this.toHide = this.toHide.not( this.toShow );
  646. this.hideErrors();
  647. this.addWrapper( this.toShow ).show();
  648. },
  649. validElements: function() {
  650. return this.currentElements.not(this.invalidElements());
  651. },
  652. invalidElements: function() {
  653. return $(this.errorList).map(function() {
  654. return this.element;
  655. });
  656. },
  657. showLabel: function(element, message) {
  658. var label = this.errorsFor( element );
  659. if ( label.length ) {
  660. // refresh error/success class
  661. label.removeClass().addClass( this.settings.errorClass );
  662. // check if we have a generated label, replace the message then
  663. label.attr("generated") && label.html(message);
  664. } else {
  665. // create label
  666. label = $("<" + this.settings.errorElement + "/>")
  667. .attr({"for": this.idOrName(element), generated: true})
  668. .addClass(this.settings.errorClass)
  669. .html(message || "");
  670. if ( this.settings.wrapper ) {
  671. // make sure the element is visible, even in IE
  672. // actually showing the wrapped element is handled elsewhere
  673. label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
  674. }
  675. if ( !this.labelContainer.append(label).length )
  676. this.settings.errorPlacement
  677. ? this.settings.errorPlacement(label, $(element) )
  678. : label.insertAfter(element);
  679. }
  680. if ( !message && this.settings.success ) {
  681. label.text("");
  682. typeof this.settings.success == "string"
  683. ? label.addClass( this.settings.success )
  684. : this.settings.success( label );
  685. }
  686. this.toShow = this.toShow.add(label);
  687. },
  688. errorsFor: function(element) {
  689. var name = this.idOrName(element);
  690. return this.errors().filter(function() {
  691. return $(this).attr('for') == name
  692. });
  693. },
  694. idOrName: function(element) {
  695. return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name);
  696. },
  697. checkable: function( element ) {
  698. return /radio|checkbox/i.test(element.type);
  699. },
  700. findByName: function( name ) {
  701. // select by name and filter by form for performance over form.find("[name=...]")
  702. var form = this.currentForm;
  703. return $(document.getElementsByName(name)).map(function(index, element) {
  704. return element.form == form && element.name == name && element || null;
  705. });
  706. },
  707. getLength: function(value, element) {
  708. switch( element.nodeName.toLowerCase() ) {
  709. case 'select':
  710. return $("option:selected", element).length;
  711. case 'input':
  712. if( this.checkable( element) )
  713. return this.findByName(element.name).filter(':checked').length;
  714. }
  715. return value.length;
  716. },
  717. depend: function(param, element) {
  718. return this.dependTypes[typeof param]
  719. ? this.dependTypes[typeof param](param, element)
  720. : true;
  721. },
  722. dependTypes: {
  723. "boolean": function(param, element) {
  724. return param;
  725. },
  726. "string": function(param, element) {
  727. return !!$(param, element.form).length;
  728. },
  729. "function": function(param, element) {
  730. return param(element);
  731. }
  732. },
  733. optional: function(element) {
  734. return !$.validator.methods.required.call(this, $.trim(element.value), element) && "dependency-mismatch";
  735. },
  736. startRequest: function(element) {
  737. if (!this.pending[element.name]) {
  738. this.pendingRequest++;
  739. this.pending[element.name] = true;
  740. }
  741. },
  742. stopRequest: function(element, valid) {
  743. this.pendingRequest--;
  744. // sometimes synchronization fails, make sure pendingRequest is never < 0
  745. if (this.pendingRequest < 0)
  746. this.pendingRequest = 0;
  747. delete this.pending[element.name];
  748. if ( valid && this.pendingRequest == 0 && this.formSubmitted && this.form() ) {
  749. $(this.currentForm).submit();
  750. this.formSubmitted = false;
  751. } else if (!valid && this.pendingRequest == 0 && this.formSubmitted) {
  752. $(this.currentForm).triggerHandler("invalid-form", [this]);
  753. this.formSubmitted = false;
  754. }
  755. },
  756. previousValue: function(element) {
  757. return $.data(element, "previousValue") || $.data(element, "previousValue", {
  758. old: null,
  759. valid: true,
  760. message: this.defaultMessage( element, "remote" )
  761. });
  762. }
  763. },
  764. classRuleSettings: {
  765. required: {required: true},
  766. email: {email: true},
  767. url: {url: true},
  768. date: {date: true},
  769. dateISO: {dateISO: true},
  770. dateDE: {dateDE: true},
  771. number: {number: true},
  772. numberDE: {numberDE: true},
  773. digits: {digits: true},
  774. creditcard: {creditcard: true}
  775. },
  776. // http://docs.jquery.com/Plugins/Validation/Validator/addClassRules#namerules
  777. addClassRules: function(className, rules) {
  778. /// <summary>
  779. /// Add a compound class method - useful to refactor common combinations of rules into a single
  780. /// class.
  781. /// </summary>
  782. /// <param name="name" type="String">
  783. /// The name of the class rule to add
  784. /// </param>
  785. /// <param name="rules" type="Options">
  786. /// The compound rules
  787. /// </param>
  788. /// <returns type="undefined" />
  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 (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. /// <returns type="undefined" />
  922. $.validator.methods[name] = method;
  923. $.validator.messages[name] = message != undefined ? message : $.validator.messages[name];
  924. if (method.length < 3) {
  925. $.validator.addClassRules(name, $.validator.normalizeRule(name));
  926. }
  927. },
  928. methods: {
  929. // http://docs.jquery.com/Plugins/Validation/Methods/required
  930. required: function(value, element, param) {
  931. // check if dependency is met
  932. if ( !this.depend(param, element) )
  933. return "dependency-mismatch";
  934. switch( element.nodeName.toLowerCase() ) {
  935. case 'select':
  936. // could be an array for select-multiple or a string, both are fine this way
  937. var val = $(element).val();
  938. return val && val.length > 0;
  939. case 'input':
  940. if ( this.checkable(element) )
  941. return this.getLength(value, element) > 0;
  942. default:
  943. return $.trim(value).length > 0;
  944. }
  945. },
  946. // http://docs.jquery.com/Plugins/Validation/Methods/remote
  947. remote: function(value, element, param) {
  948. if ( this.optional(element) )
  949. return "dependency-mismatch";
  950. var previous = this.previousValue(element);
  951. if (!this.settings.messages[element.name] )
  952. this.settings.messages[element.name] = {};
  953. previous.originalMessage = this.settings.messages[element.name].remote;
  954. this.settings.messages[element.name].remote = previous.message;
  955. param = typeof param == "string" && {url:param} || param;
  956. if ( previous.old !== value ) {
  957. previous.old = value;
  958. var validator = this;
  959. this.startRequest(element);
  960. var data = {};
  961. data[element.name] = value;
  962. $.ajax($.extend(true, {
  963. url: param,
  964. mode: "abort",
  965. port: "validate" + element.name,
  966. dataType: "json",
  967. data: data,
  968. success: function(response) {
  969. validator.settings.messages[element.name].remote = previous.originalMessage;
  970. var valid = response === true;
  971. if ( valid ) {
  972. var submitted = validator.formSubmitted;
  973. validator.prepareElement(element);
  974. validator.formSubmitted = submitted;
  975. validator.successList.push(element);
  976. validator.showErrors();
  977. } else {
  978. var errors = {};
  979. var message = (previous.message = response || validator.defaultMessage( element, "remote" ));
  980. errors[element.name] = $.isFunction(message) ? message(value) : message;
  981. validator.showErrors(errors);
  982. }
  983. previous.valid = valid;
  984. validator.stopRequest(element, valid);
  985. }
  986. }, param));
  987. return "pending";
  988. } else if( this.pending[element.name] ) {
  989. return "pending";
  990. }
  991. return previous.valid;
  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 ajax = $.ajax;
  1092. var pendingRequests = {};
  1093. $.ajax = function(settings) {
  1094. // create settings for compatibility with ajaxSetup
  1095. settings = $.extend(settings, $.extend({}, $.ajaxSettings, settings));
  1096. var port = settings.port;
  1097. if (settings.mode == "abort") {
  1098. if ( pendingRequests[port] ) {
  1099. pendingRequests[port].abort();
  1100. }
  1101. return (pendingRequests[port] = ajax.apply(this, arguments));
  1102. }
  1103. return ajax.apply(this, arguments);
  1104. };
  1105. })(jQuery);
  1106. // provides cross-browser focusin and focusout events
  1107. // IE has native support, in other browsers, use event caputuring (neither bubbles)
  1108. // provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation
  1109. // handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target
  1110. // provides triggerEvent(type: String, target: Element) to trigger delegated events
  1111. ;(function($) {
  1112. $.each({
  1113. focus: 'focusin',
  1114. blur: 'focusout'
  1115. }, function( original, fix ){
  1116. $.event.special[fix] = {
  1117. setup:function() {
  1118. if ( $.browser.msie ) return false;
  1119. this.addEventListener( original, $.event.special[fix].handler, true );
  1120. },
  1121. teardown:function() {
  1122. if ( $.browser.msie ) return false;
  1123. this.removeEventListener( original,
  1124. $.event.special[fix].handler, true );
  1125. },
  1126. handler: function(e) {
  1127. arguments[0] = $.event.fix(e);
  1128. arguments[0].type = fix;
  1129. return $.event.handle.apply(this, arguments);
  1130. }
  1131. };
  1132. });
  1133. $.extend($.fn, {
  1134. delegate: function(type, delegate, handler) {
  1135. return this.bind(type, function(event) {
  1136. var target = $(event.target);
  1137. if (target.is(delegate)) {
  1138. return handler.apply(target, arguments);
  1139. }
  1140. });
  1141. },
  1142. triggerEvent: function(type, target) {
  1143. return this.triggerHandler(type, [$.event.fix({ type: type, target: target })]);
  1144. }
  1145. })
  1146. })(jQuery);