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

1302 lines
42 KiB

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