validate.js 40 KB

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