jquery.validate-vsdoc.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. /// 验证所选表单。此方法将为 submit、focus、
  44. /// keyup、blur 和 click 设置事件处理程序,用于触发整个表单或各个
  45. /// 元素的验证操作。可以禁用每个事件,请参见 onxxx 选项(onsubmit、onfocusout、
  46. /// onkeyup 和 onclick)。当提交的表单无效时,focusInvalid 将焦点设置在元素上。
  47. /// </summary>
  48. /// <param name="options" type="Options">
  49. /// 配置验证的一组键/值对。所有选项都是可选的。
  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. /// 检查选定表单是否有效,或检查所选全部元素是否都有效。
  118. /// 使用此方法检查表单之前,需要对表单调用 validate()。
  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. /// 从首个匹配的元素中移除指定的特性并返回这些特性。
  136. /// </summary>
  137. /// <param name="attributes" type="String">
  138. /// 要移除的特性名称的空格分隔列表。
  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. /// 为首个选定的元素返回验证规则。
  153. /// </summary>
  154. /// <param name="command" type="String">
  155. /// 可以是“add”或“remove”。
  156. /// </param>
  157. /// <param name="argument" type="">
  158. /// 要添加或移除的规则的列表。
  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. /// 用参数替换 {n} 占位符。
  221. /// 除了字符串模板自身以外,还可以传递一个或多个参数,以插入到
  222. /// 字符串中。
  223. /// </summary>
  224. /// <param name="source" type="String">
  225. /// 要设置格式的字符串。
  226. /// </param>
  227. /// <param name="params" type="String">
  228. /// 要插入的首个参数,或要插入的字符串数组
  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. /// 修改验证操作的默认设置。
  299. /// 接受 Plugins/Validation/validate 接受的所有内容。
  300. /// </summary>
  301. /// <param name="settings" type="Options">
  302. /// 要设置为默认值的选项。
  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: "请输入有效的 URL。",
  312. date: "Please enter a valid date.",
  313. dateISO: "请输入有效的日期(ISO)。",
  314. number: "Please enter a valid number.",
  315. 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. /// 验证表单。如果表单有效,则返回 true;否则返回 false。
  362. /// 此行为与普通 submit 事件类似,但它返回的是结果。
  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. /// 验证单个元素。如果元素有效,则返回 true;否则返回 false。
  384. /// 此行为类似于对 blur 或 keyup 进行验证操作,但它返回的是结果。
  385. /// </summary>
  386. /// <param name="element" type="Selector">
  387. /// 要验证的元素,该元素必须位于已验证的表单内。
  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. /// 显示指定的消息。
  411. /// 键必须指代元素的名称,将显示那些元素所对应的值(使用已配置的错误位置)。
  412. /// </summary>
  413. /// <param name="errors" type="Object">
  414. /// 输入名称和消息的一个或多个键/值对。
  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. /// 重置受控表单。
  440. /// 将输入字段重置为其原始值(需要表单插件),移除指示无效元素的
  441. /// 类并隐藏错误消息。
  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. /// 返回无效字段的个数。
  454. /// 这依赖于内部验证程序的状态。只有在验证整个表单
  455. /// (在提交时或通过调用 $("form").valid())之后才会覆盖所有字段。在验证
  456. /// 单个元素之后,只计入相应的元素。与 invalidHandler-option 组合时
  457. /// 最有用。
  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 分配名称", 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>警告: 没有为它们定义任何消息" + 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. /// 添加一个复合类方法,这对于将常见的规则组合重构为单个
  780. /// 类很有用。
  781. /// </summary>
  782. /// <param name="name" type="String">
  783. /// 要添加的类规则的名称
  784. /// </param>
  785. /// <param name="rules" type="Options">
  786. /// 复合规则
  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. /// 添加一个自定义验证方法。此方法必须包含一个名称(必须是合法的 javascript
  907. /// 标识符)、一个基于 javascript 的函数和一个默认字符串消息。
  908. /// </summary>
  909. /// <param name="name" type="String">
  910. /// 用于标识和引用方法的名称必须是有效的 javascript
  911. /// 标识符
  912. /// </param>
  913. /// <param name="method" type="Function">
  914. /// 实际的方法实现,在元素有效时返回 true
  915. /// </param>
  916. /// <param name="message" type="String" optional="true">
  917. /// (可选)要为此方法显示的默认消息。可以是由
  918. /// jQuery.validator.format(value) 创建的函数。若未定义,则使用已经存在的消息
  919. /// (方便本地化),否则必须定义特定于字段的消息。
  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);