jquery.validate.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /**
  2. * jQuery Validation Plugin 1.8.1
  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. (function($) {
  14. $.extend($.fn, {
  15. // http://docs.jquery.com/Plugins/Validation/validate
  16. validate: function( options ) {
  17. // if nothing is selected, return nothing; can't chain anyway
  18. if (!this.length) {
  19. options && options.debug && window.console && console.warn( "nothing selected, can't validate, returning nothing" );
  20. return;
  21. }
  22. // check if a validator for this form was already created
  23. var validator = $.data(this[0], 'validator');
  24. if ( validator ) {
  25. return validator;
  26. }
  27. validator = new $.validator( options, this[0] );
  28. $.data(this[0], 'validator', validator);
  29. if ( validator.settings.onsubmit ) {
  30. // allow suppresing validation by adding a cancel class to the submit button
  31. this.find("input, button").filter(".cancel").click(function() {
  32. validator.cancelSubmit = true;
  33. });
  34. // when a submitHandler is used, capture the submitting button
  35. if (validator.settings.submitHandler) {
  36. this.find("input, button").filter(":submit").click(function() {
  37. validator.submitButton = this;
  38. });
  39. }
  40. // validate the form on submit
  41. this.submit( function( event ) {
  42. if ( validator.settings.debug )
  43. // prevent form submit to be able to see console output
  44. event.preventDefault();
  45. function handle() {
  46. if ( validator.settings.submitHandler ) {
  47. if (validator.submitButton) {
  48. // insert a hidden input as a replacement for the missing submit button
  49. var hidden = $("<input type='hidden'/>").attr("name", validator.submitButton.name).val(validator.submitButton.value).appendTo(validator.currentForm);
  50. }
  51. validator.settings.submitHandler.call( validator, validator.currentForm );
  52. if (validator.submitButton) {
  53. // and clean up afterwards; thanks to no-block-scope, hidden can be referenced
  54. hidden.remove();
  55. }
  56. return false;
  57. }
  58. return true;
  59. }
  60. // prevent submit for invalid forms or custom submit handlers
  61. if ( validator.cancelSubmit ) {
  62. validator.cancelSubmit = false;
  63. return handle();
  64. }
  65. if ( validator.form() ) {
  66. if ( validator.pendingRequest ) {
  67. validator.formSubmitted = true;
  68. return false;
  69. }
  70. return handle();
  71. } else {
  72. validator.focusInvalid();
  73. return false;
  74. }
  75. });
  76. }
  77. return validator;
  78. },
  79. // http://docs.jquery.com/Plugins/Validation/valid
  80. valid: function() {
  81. if ( $(this[0]).is('form')) {
  82. return this.validate().form();
  83. } else {
  84. var valid = true;
  85. var validator = $(this[0].form).validate();
  86. this.each(function() {
  87. valid &= validator.element(this);
  88. });
  89. return valid;
  90. }
  91. },
  92. // attributes: space seperated list of attributes to retrieve and remove
  93. removeAttrs: function(attributes) {
  94. var result = {},
  95. $element = this;
  96. $.each(attributes.split(/\s/), function(index, value) {
  97. result[value] = $element.attr(value);
  98. $element.removeAttr(value);
  99. });
  100. return result;
  101. },
  102. // http://docs.jquery.com/Plugins/Validation/rules
  103. rules: function(command, argument) {
  104. var element = this[0];
  105. if (command) {
  106. var settings = $.data(element.form, 'validator').settings;
  107. var staticRules = settings.rules;
  108. var existingRules = $.validator.staticRules(element);
  109. switch(command) {
  110. case "add":
  111. $.extend(existingRules, $.validator.normalizeRule(argument));
  112. staticRules[element.name] = existingRules;
  113. if (argument.messages)
  114. settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages );
  115. break;
  116. case "remove":
  117. if (!argument) {
  118. delete staticRules[element.name];
  119. return existingRules;
  120. }
  121. var filtered = {};
  122. $.each(argument.split(/\s/), function(index, method) {
  123. filtered[method] = existingRules[method];
  124. delete existingRules[method];
  125. });
  126. return filtered;
  127. }
  128. }
  129. var data = $.validator.normalizeRules(
  130. $.extend(
  131. {},
  132. $.validator.metadataRules(element),
  133. $.validator.classRules(element),
  134. $.validator.attributeRules(element),
  135. $.validator.staticRules(element)
  136. ), element);
  137. // make sure required is at front
  138. if (data.required) {
  139. var param = data.required;
  140. delete data.required;
  141. data = $.extend({required: param}, data);
  142. }
  143. return data;
  144. }
  145. });
  146. // Custom selectors
  147. $.extend($.expr[":"], {
  148. // http://docs.jquery.com/Plugins/Validation/blank
  149. blank: function(a) {return !$.trim("" + a.value);},
  150. // http://docs.jquery.com/Plugins/Validation/filled
  151. filled: function(a) {return !!$.trim("" + a.value);},
  152. // http://docs.jquery.com/Plugins/Validation/unchecked
  153. unchecked: function(a) {return !a.checked;}
  154. });
  155. // constructor for validator
  156. $.validator = function( options, form ) {
  157. this.settings = $.extend( true, {}, $.validator.defaults, options );
  158. this.currentForm = form;
  159. this.init();
  160. };
  161. $.validator.format = function(source, params) {
  162. if ( arguments.length == 1 )
  163. return function() {
  164. var args = $.makeArray(arguments);
  165. args.unshift(source);
  166. return $.validator.format.apply( this, args );
  167. };
  168. if ( arguments.length > 2 && params.constructor != Array ) {
  169. params = $.makeArray(arguments).slice(1);
  170. }
  171. if ( params.constructor != Array ) {
  172. params = [ params ];
  173. }
  174. $.each(params, function(i, n) {
  175. source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
  176. });
  177. return source;
  178. };
  179. $.extend($.validator, {
  180. defaults: {
  181. messages: {},
  182. groups: {},
  183. rules: {},
  184. errorClass: "error",
  185. validClass: "valid",
  186. errorElement: "label",
  187. focusInvalid: true,
  188. errorContainer: $( [] ),
  189. errorLabelContainer: $( [] ),
  190. onsubmit: true,
  191. ignore: [],
  192. ignoreTitle: false,
  193. onfocusin: function(element) {
  194. this.lastActive = element;
  195. // hide error label and remove error class on focus if enabled
  196. if ( this.settings.focusCleanup && !this.blockFocusCleanup ) {
  197. this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass, this.settings.validClass );
  198. this.addWrapper(this.errorsFor(element)).hide();
  199. }
  200. },
  201. onfocusout: function(element) {
  202. if ( !this.checkable(element) && (element.name in this.submitted || !this.optional(element)) ) {
  203. this.element(element);
  204. }
  205. },
  206. onkeyup: function(element) {
  207. if ( element.name in this.submitted || element == this.lastElement ) {
  208. this.element(element);
  209. }
  210. },
  211. onclick: function(element) {
  212. // click on selects, radiobuttons and checkboxes
  213. if ( element.name in this.submitted )
  214. this.element(element);
  215. // or option elements, check parent select in that case
  216. else if (element.parentNode.name in this.submitted)
  217. this.element(element.parentNode);
  218. },
  219. highlight: function(element, errorClass, validClass) {
  220. if (element.type === 'radio') {
  221. this.findByName(element.name).addClass(errorClass).removeClass(validClass);
  222. } else {
  223. $(element).addClass(errorClass).removeClass(validClass);
  224. }
  225. },
  226. unhighlight: function(element, errorClass, validClass) {
  227. if (element.type === 'radio') {
  228. this.findByName(element.name).removeClass(errorClass).addClass(validClass);
  229. } else {
  230. $(element).removeClass(errorClass).addClass(validClass);
  231. }
  232. }
  233. },
  234. // http://docs.jquery.com/Plugins/Validation/Validator/setDefaults
  235. setDefaults: function(settings) {
  236. $.extend( $.validator.defaults, settings );
  237. },
  238. messages: {
  239. required: "必填项",
  240. remote: "数据已存在",
  241. email: "请输入有效的邮箱",
  242. url: "请输入有效的网址",
  243. date: "请输入有效的日期",
  244. dateISO: "请输入有效的(ISO)日期.",
  245. number: "请输入有效的数字",
  246. digits: "请只输入数字",
  247. creditcard: "请输入有效的信用卡号码",
  248. equalTo: "请再次输入相同的值.",
  249. accept: "请输入合法的扩展名.",
  250. maxlength: $.validator.format("请输入不超过 {0} 个字符."),
  251. minlength: $.validator.format("请输入至少 {0} 个字符."),
  252. rangelength: $.validator.format("请输入介于值 {0} 和 {1} 个字符长."),
  253. range: $.validator.format("请输入介于 {0} 和 {1}的值"),
  254. max: $.validator.format("请输入大于或等于 {0}的值"),
  255. min: $.validator.format("请输入大于或等于 {0}的值"),
  256. ip4: "请输入正确的IP地址",
  257. mobile: "请输入正确的手机号码",
  258. zipcode: "请输入正确的邮编",
  259. qq: "请输入正确的QQ号码",
  260. idcard: "请输入正确的身份证号",
  261. chinese: "请输入中文字符",
  262. cn_username:"请输入中文英文和数字",
  263. tel: "请输入正确的电话号码",
  264. english: "只能输入英文字母",
  265. en_num: "只能输入英文和数字和下划线"
  266. },
  267. autoCreateRanges: false,
  268. prototype: {
  269. init: function() {
  270. this.labelContainer = $(this.settings.errorLabelContainer);
  271. this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm);
  272. this.containers = $(this.settings.errorContainer).add( this.settings.errorLabelContainer );
  273. this.submitted = {};
  274. this.valueCache = {};
  275. this.pendingRequest = 0;
  276. this.pending = {};
  277. this.invalid = {};
  278. this.reset();
  279. var groups = (this.groups = {});
  280. $.each(this.settings.groups, function(key, value) {
  281. $.each(value.split(/\s/), function(index, name) {
  282. groups[name] = key;
  283. });
  284. });
  285. var rules = this.settings.rules;
  286. $.each(rules, function(key, value) {
  287. rules[key] = $.validator.normalizeRule(value);
  288. });
  289. function delegate(event) {
  290. var validator = $.data(this[0].form, "validator"),
  291. eventType = "on" + event.type.replace(/^validate/, "");
  292. validator.settings[eventType] && validator.settings[eventType].call(validator, this[0] );
  293. }
  294. $(this.currentForm)
  295. .validateDelegate(":text, :password, :file, select, textarea", "focusin focusout keyup", delegate)
  296. .validateDelegate(":radio, :checkbox, select, option", "click", delegate);
  297. if (this.settings.invalidHandler)
  298. $(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler);
  299. },
  300. // http://docs.jquery.com/Plugins/Validation/Validator/form
  301. form: function() {
  302. this.checkForm();
  303. $.extend(this.submitted, this.errorMap);
  304. this.invalid = $.extend({}, this.errorMap);
  305. if (!this.valid())
  306. $(this.currentForm).triggerHandler("invalid-form", [this]);
  307. this.showErrors();
  308. return this.valid();
  309. },
  310. checkForm: function() {
  311. this.prepareForm();
  312. for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
  313. this.check( elements[i] );
  314. }
  315. return this.valid();
  316. },
  317. // http://docs.jquery.com/Plugins/Validation/Validator/element
  318. element: function( element ) {
  319. element = this.clean( element );
  320. this.lastElement = element;
  321. this.prepareElement( element );
  322. this.currentElements = $(element);
  323. var result = this.check( element );
  324. if ( result ) {
  325. delete this.invalid[element.name];
  326. } else {
  327. this.invalid[element.name] = true;
  328. }
  329. if ( !this.numberOfInvalids() ) {
  330. // Hide error containers on last error
  331. this.toHide = this.toHide.add( this.containers );
  332. }
  333. this.showErrors();
  334. return result;
  335. },
  336. // http://docs.jquery.com/Plugins/Validation/Validator/showErrors
  337. showErrors: function(errors) {
  338. if(errors) {
  339. // add items to error list and map
  340. $.extend( this.errorMap, errors );
  341. this.errorList = [];
  342. for ( var name in errors ) {
  343. this.errorList.push({
  344. message: errors[name],
  345. element: this.findByName(name)[0]
  346. });
  347. }
  348. // remove items from success list
  349. this.successList = $.grep( this.successList, function(element) {
  350. return !(element.name in errors);
  351. });
  352. }
  353. this.settings.showErrors
  354. ? this.settings.showErrors.call( this, this.errorMap, this.errorList )
  355. : this.defaultShowErrors();
  356. },
  357. // http://docs.jquery.com/Plugins/Validation/Validator/resetForm
  358. resetForm: function() {
  359. if ( $.fn.resetForm )
  360. $( this.currentForm ).resetForm();
  361. this.submitted = {};
  362. this.prepareForm();
  363. this.hideErrors();
  364. this.elements().removeClass( this.settings.errorClass );
  365. },
  366. numberOfInvalids: function() {
  367. return this.objectLength(this.invalid);
  368. },
  369. objectLength: function( obj ) {
  370. var count = 0;
  371. for ( var i in obj )
  372. count++;
  373. return count;
  374. },
  375. hideErrors: function() {
  376. this.addWrapper( this.toHide ).hide();
  377. },
  378. valid: function() {
  379. return this.size() == 0;
  380. },
  381. size: function() {
  382. return this.errorList.length;
  383. },
  384. focusInvalid: function() {
  385. if( this.settings.focusInvalid ) {
  386. try {
  387. $(this.findLastActive() || this.errorList.length && this.errorList[0].element || [])
  388. .filter(":visible")
  389. .focus()
  390. // manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
  391. .trigger("focusin");
  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. ip4: {ip4: true},
  686. zipcode: {zipcode: true},
  687. qq: {qq: true},
  688. idcard: {idcard: true},
  689. chinese: {chinese: true},
  690. cn_username: {cn_username: true},
  691. tel: {tel: true},
  692. mobile: {mobile: true},
  693. english: {english: true},
  694. en_num: {en_num: true}
  695. },
  696. addClassRules: function(className, rules) {
  697. className.constructor == String ?
  698. this.classRuleSettings[className] = rules :
  699. $.extend(this.classRuleSettings, className);
  700. },
  701. classRules: function(element) {
  702. var rules = {};
  703. var classes = $(element).attr('class');
  704. classes && $.each(classes.split(' '), function() {
  705. if (this in $.validator.classRuleSettings) {
  706. $.extend(rules, $.validator.classRuleSettings[this]);
  707. }
  708. });
  709. return rules;
  710. },
  711. attributeRules: function(element) {
  712. var rules = {};
  713. var $element = $(element);
  714. for (var method in $.validator.methods) {
  715. var value = $element.attr(method);
  716. if (value) {
  717. rules[method] = value;
  718. }
  719. }
  720. // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
  721. if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) {
  722. delete rules.maxlength;
  723. }
  724. return rules;
  725. },
  726. metadataRules: function(element) {
  727. if (!$.metadata) return {};
  728. var meta = $.data(element.form, 'validator').settings.meta;
  729. return meta ?
  730. $(element).metadata()[meta] :
  731. $(element).metadata();
  732. },
  733. staticRules: function(element) {
  734. var rules = {};
  735. var validator = $.data(element.form, 'validator');
  736. if (validator.settings.rules) {
  737. rules = $.validator.normalizeRule(validator.settings.rules[element.name]) || {};
  738. }
  739. return rules;
  740. },
  741. normalizeRules: function(rules, element) {
  742. // handle dependency check
  743. $.each(rules, function(prop, val) {
  744. // ignore rule when param is explicitly false, eg. required:false
  745. if (val === false) {
  746. delete rules[prop];
  747. return;
  748. }
  749. if (val.param || val.depends) {
  750. var keepRule = true;
  751. switch (typeof val.depends) {
  752. case "string":
  753. keepRule = !!$(val.depends, element.form).length;
  754. break;
  755. case "function":
  756. keepRule = val.depends.call(element, element);
  757. break;
  758. }
  759. if (keepRule) {
  760. rules[prop] = val.param !== undefined ? val.param : true;
  761. } else {
  762. delete rules[prop];
  763. }
  764. }
  765. });
  766. // evaluate parameters
  767. $.each(rules, function(rule, parameter) {
  768. rules[rule] = $.isFunction(parameter) ? parameter(element) : parameter;
  769. });
  770. // clean number parameters
  771. $.each(['minlength', 'maxlength', 'min', 'max'], function() {
  772. if (rules[this]) {
  773. rules[this] = Number(rules[this]);
  774. }
  775. });
  776. $.each(['rangelength', 'range'], function() {
  777. if (rules[this]) {
  778. rules[this] = [Number(rules[this][0]), Number(rules[this][1])];
  779. }
  780. });
  781. if ($.validator.autoCreateRanges) {
  782. // auto-create ranges
  783. if (rules.min && rules.max) {
  784. rules.range = [rules.min, rules.max];
  785. delete rules.min;
  786. delete rules.max;
  787. }
  788. if (rules.minlength && rules.maxlength) {
  789. rules.rangelength = [rules.minlength, rules.maxlength];
  790. delete rules.minlength;
  791. delete rules.maxlength;
  792. }
  793. }
  794. // To support custom messages in metadata ignore rule methods titled "messages"
  795. if (rules.messages) {
  796. delete rules.messages;
  797. }
  798. return rules;
  799. },
  800. // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
  801. normalizeRule: function(data) {
  802. if( typeof data == "string" ) {
  803. var transformed = {};
  804. $.each(data.split(/\s/), function() {
  805. transformed[this] = true;
  806. });
  807. data = transformed;
  808. }
  809. return data;
  810. },
  811. // http://docs.jquery.com/Plugins/Validation/Validator/addMethod
  812. addMethod: function(name, method, message) {
  813. $.validator.methods[name] = method;
  814. $.validator.messages[name] = message != undefined ? message : $.validator.messages[name];
  815. if (method.length < 3) {
  816. $.validator.addClassRules(name, $.validator.normalizeRule(name));
  817. }
  818. },
  819. methods: {
  820. // http://docs.jquery.com/Plugins/Validation/Methods/required
  821. required: function(value, element, param) {
  822. // check if dependency is met
  823. if ( !this.depend(param, element) )
  824. return "dependency-mismatch";
  825. switch( element.nodeName.toLowerCase() ) {
  826. case 'select':
  827. // could be an array for select-multiple or a string, both are fine this way
  828. var val = $(element).val();
  829. return val && val.length > 0;
  830. case 'input':
  831. if ( this.checkable(element) )
  832. return this.getLength(value, element) > 0;
  833. default:
  834. return $.trim(value).length > 0;
  835. }
  836. },
  837. // http://docs.jquery.com/Plugins/Validation/Methods/remote
  838. remote: function(value, element, param) {
  839. if ( this.optional(element) )
  840. return "dependency-mismatch";
  841. var previous = this.previousValue(element);
  842. if (!this.settings.messages[element.name] )
  843. this.settings.messages[element.name] = {};
  844. previous.originalMessage = this.settings.messages[element.name].remote;
  845. this.settings.messages[element.name].remote = previous.message;
  846. param = typeof param == "string" && {url:param} || param;
  847. if ( this.pending[element.name] ) {
  848. return "pending";
  849. }
  850. if ( previous.old === value ) {
  851. return previous.valid;
  852. }
  853. previous.old = value;
  854. var validator = this;
  855. this.startRequest(element);
  856. var data = {};
  857. data[element.name] = value;
  858. $.ajax($.extend(true, {
  859. url: param,
  860. mode: "abort",
  861. port: "validate" + element.name,
  862. dataType: "json",
  863. data: data,
  864. success: function(response) {
  865. validator.settings.messages[element.name].remote = previous.originalMessage;
  866. var valid = response === true;
  867. if ( valid ) {
  868. var submitted = validator.formSubmitted;
  869. validator.prepareElement(element);
  870. validator.formSubmitted = submitted;
  871. validator.successList.push(element);
  872. validator.showErrors();
  873. } else {
  874. var errors = {};
  875. var message = response || validator.defaultMessage( element, "remote" );
  876. errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
  877. validator.showErrors(errors);
  878. }
  879. previous.valid = valid;
  880. validator.stopRequest(element, valid);
  881. }
  882. }, param));
  883. return "pending";
  884. },
  885. // http://docs.jquery.com/Plugins/Validation/Methods/minlength
  886. minlength: function(value, element, param) {
  887. return this.optional(element) || this.getLength($.trim(value), element) >= param;
  888. },
  889. // http://docs.jquery.com/Plugins/Validation/Methods/maxlength
  890. maxlength: function(value, element, param) {
  891. return this.optional(element) || this.getLength($.trim(value), element) <= param;
  892. },
  893. // http://docs.jquery.com/Plugins/Validation/Methods/rangelength
  894. rangelength: function(value, element, param) {
  895. var length = this.getLength($.trim(value), element);
  896. return this.optional(element) || ( length >= param[0] && length <= param[1] );
  897. },
  898. // http://docs.jquery.com/Plugins/Validation/Methods/min
  899. min: function( value, element, param ) {
  900. return this.optional(element) || value >= param;
  901. },
  902. // http://docs.jquery.com/Plugins/Validation/Methods/max
  903. max: function( value, element, param ) {
  904. return this.optional(element) || value <= param;
  905. },
  906. // http://docs.jquery.com/Plugins/Validation/Methods/range
  907. range: function( value, element, param ) {
  908. return this.optional(element) || ( value >= param[0] && value <= param[1] );
  909. },
  910. // http://docs.jquery.com/Plugins/Validation/Methods/email
  911. email: function(value, element) {
  912. // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
  913. 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);
  914. },
  915. // http://docs.jquery.com/Plugins/Validation/Methods/url
  916. url: function(value, element) {
  917. // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
  918. 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);
  919. },
  920. // http://docs.jquery.com/Plugins/Validation/Methods/date
  921. date: function(value, element) {
  922. return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
  923. },
  924. // http://docs.jquery.com/Plugins/Validation/Methods/dateISO
  925. dateISO: function(value, element) {
  926. return this.optional(element) || /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value);
  927. },
  928. // http://docs.jquery.com/Plugins/Validation/Methods/number
  929. number: function(value, element) {
  930. return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
  931. },
  932. english: function(value, element) {
  933. return this.optional(element) || /^[a-z_A-Z]+$/.test(value);
  934. },
  935. en_num: function(value, element) {
  936. return this.optional(element) || /^\w+$/.test(value);
  937. },
  938. ip4: function(value, element) {
  939. return this.optional(element) || /^\d+\.\d+\.\d+\.\d+$/.test(value);
  940. },
  941. mobile: function(value, element) {
  942. return this.optional(element) || /^[0-9]{11}$/.test(value);
  943. },
  944. zipcode: function(value, element) {
  945. return this.optional(element) || /^\d{6}$/.test(value);
  946. },
  947. qq: function(value, element) {
  948. return this.optional(element) || /^\d{5,}$/.test(value);
  949. },
  950. idcard: function(value, element) {
  951. return this.optional(element) || /^[1-9]([0-9]{14}|[0-9]{17})$/.test(value);
  952. },
  953. chinese: function(value, element) {
  954. return this.optional(element) || /^[\u4e00-\u9fa5]+$/.test(value);
  955. },
  956. cn_username: function(value, element) {
  957. return this.optional(element) || /^([\u4e00-\u9fa5]|[\w])+$/.test(value);
  958. },
  959. tel: function(value, element) {
  960. return this.optional(element) || /^[+]{0,1}(\d){1,4}[ ]{0,1}([-]{0,1}((\d)|[ ]){1,12})+$/.test(value);
  961. },
  962. // http://docs.jquery.com/Plugins/Validation/Methods/digits
  963. digits: function(value, element) {
  964. return this.optional(element) || /^\d+$/.test(value);
  965. },
  966. // http://docs.jquery.com/Plugins/Validation/Methods/creditcard
  967. // based on http://en.wikipedia.org/wiki/Luhn
  968. creditcard: function(value, element) {
  969. if ( this.optional(element) )
  970. return "dependency-mismatch";
  971. // accept only digits and dashes
  972. if (/[^0-9-]+/.test(value))
  973. return false;
  974. var nCheck = 0,
  975. nDigit = 0,
  976. bEven = false;
  977. value = value.replace(/\D/g, "");
  978. for (var n = value.length - 1; n >= 0; n--) {
  979. var cDigit = value.charAt(n);
  980. var nDigit = parseInt(cDigit, 10);
  981. if (bEven) {
  982. if ((nDigit *= 2) > 9)
  983. nDigit -= 9;
  984. }
  985. nCheck += nDigit;
  986. bEven = !bEven;
  987. }
  988. return (nCheck % 10) == 0;
  989. },
  990. // http://docs.jquery.com/Plugins/Validation/Methods/accept
  991. accept: function(value, element, param) {
  992. param = typeof param == "string" ? param.replace(/,/g, '|') : "png|jpe?g|gif";
  993. return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i"));
  994. },
  995. // http://docs.jquery.com/Plugins/Validation/Methods/equalTo
  996. equalTo: function(value, element, param) {
  997. // bind to the blur event of the target in order to revalidate whenever the target field is updated
  998. // TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
  999. var target = $(param).unbind(".validate-equalTo").bind("blur.validate-equalTo", function() {
  1000. $(element).valid();
  1001. });
  1002. return value == target.val();
  1003. }
  1004. }
  1005. });
  1006. // deprecated, use $.validator.format instead
  1007. $.format = $.validator.format;
  1008. })(jQuery);
  1009. // ajax mode: abort
  1010. // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
  1011. // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
  1012. (function ($) {
  1013. var pendingRequests = {};
  1014. // Use a prefilter if available (1.5+)
  1015. if ( $.ajaxPrefilter ) {
  1016. $.ajaxPrefilter(function(settings, _, xhr) {
  1017. var port = settings.port;
  1018. if (settings.mode == "abort") {
  1019. if ( pendingRequests[port] ) {
  1020. pendingRequests[port].abort();
  1021. }
  1022. pendingRequests[port] = xhr;
  1023. }
  1024. });
  1025. } else {
  1026. // Proxy ajax
  1027. var ajax = $.ajax;
  1028. $.ajax = function(settings) {
  1029. var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode,
  1030. port = ( "port" in settings ? settings : $.ajaxSettings ).port;
  1031. if (mode == "abort") {
  1032. if ( pendingRequests[port] ) {
  1033. pendingRequests[port].abort();
  1034. }
  1035. return (pendingRequests[port] = ajax.apply(this, arguments));
  1036. }
  1037. return ajax.apply(this, arguments);
  1038. };
  1039. }
  1040. })(jQuery);
  1041. // provides cross-browser focusin and focusout events
  1042. // IE has native support, in other browsers, use event caputuring (neither bubbles)
  1043. // provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation
  1044. // handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target
  1045. (function ($) {
  1046. // only implement if not provided by jQuery core (since 1.4)
  1047. // TODO verify if jQuery 1.4's implementation is compatible with older jQuery special-event APIs
  1048. if (!jQuery.event.special.focusin && !jQuery.event.special.focusout && document.addEventListener) {
  1049. $.each({
  1050. focus: 'focusin',
  1051. blur: 'focusout'
  1052. }, function( original, fix ){
  1053. $.event.special[fix] = {
  1054. setup:function() {
  1055. this.addEventListener( original, handler, true );
  1056. },
  1057. teardown:function() {
  1058. this.removeEventListener( original, handler, true );
  1059. },
  1060. handler: function(e) {
  1061. arguments[0] = $.event.fix(e);
  1062. arguments[0].type = fix;
  1063. return $.event.handle.apply(this, arguments);
  1064. }
  1065. };
  1066. function handler(e) {
  1067. e = $.event.fix(e);
  1068. e.type = fix;
  1069. return $.event.handle.call(this, e);
  1070. }
  1071. });
  1072. }
  1073. $.extend($.fn, {
  1074. validateDelegate: function(delegate, type, handler) {
  1075. return this.bind(type, function(event) {
  1076. var target = $(event.target);
  1077. if (target.is(delegate)) {
  1078. return handler.apply(target, arguments);
  1079. }
  1080. });
  1081. }
  1082. });
  1083. })(jQuery);
  1084. /*设置显示样式*/
  1085. (function($) {
  1086. $.extend({
  1087. metadata : {
  1088. defaults : {
  1089. type: 'class',
  1090. name: 'metadata',
  1091. cre: /({.*})/,
  1092. single: 'metadata'
  1093. },
  1094. setType: function( type, name ){
  1095. this.defaults.type = type;
  1096. this.defaults.name = name;
  1097. },
  1098. get: function( elem, opts ){
  1099. var settings = $.extend({},this.defaults,opts);
  1100. // check for empty string in single property
  1101. if ( !settings.single.length ) settings.single = 'metadata';
  1102. var data = $.data(elem, settings.single);
  1103. // returned cached data if it already exists
  1104. if ( data ) return data;
  1105. data = "{}";
  1106. if ( settings.type == "class" ) {
  1107. var m = settings.cre.exec( elem.className );
  1108. if ( m )
  1109. data = m[1];
  1110. } else if ( settings.type == "elem" ) {
  1111. if( !elem.getElementsByTagName )
  1112. return undefined;
  1113. var e = elem.getElementsByTagName(settings.name);
  1114. if ( e.length )
  1115. data = $.trim(e[0].innerHTML);
  1116. } else if ( elem.getAttribute != undefined ) {
  1117. var attr = elem.getAttribute( settings.name );
  1118. if ( attr )
  1119. data = attr;
  1120. }
  1121. if ( data.indexOf( '{' ) <0 )
  1122. data = "{" + data + "}";
  1123. data = eval("(" + data + ")");
  1124. $.data( elem, settings.single, data );
  1125. return data;
  1126. }
  1127. }
  1128. });
  1129. $.fn.metadata = function( opts ){
  1130. return $.metadata.get( this[0], opts );
  1131. };
  1132. })(jQuery);
  1133. $.metadata.setType("attr", "validate");