jquery.validatebox.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * EasyUI for jQuery 1.6.10
  3. *
  4. * Copyright (c) 2009-2018 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the freeware license: http://www.jeasyui.com/license_freeware.php
  7. * To use it on other terms please contact us: info@jeasyui.com
  8. *
  9. */
  10. (function($){
  11. function _1(_2){
  12. $(_2).addClass("validatebox-text");
  13. };
  14. function _3(_4){
  15. var _5=$.data(_4,"validatebox");
  16. _5.validating=false;
  17. if(_5.vtimer){
  18. clearTimeout(_5.vtimer);
  19. }
  20. if(_5.ftimer){
  21. clearTimeout(_5.ftimer);
  22. }
  23. $(_4).tooltip("destroy");
  24. $(_4).unbind();
  25. $(_4).remove();
  26. };
  27. function _6(_7){
  28. var _8=$.data(_7,"validatebox").options;
  29. $(_7).unbind(".validatebox");
  30. if(_8.novalidate||_8.disabled){
  31. return;
  32. }
  33. for(var _9 in _8.events){
  34. $(_7).bind(_9+".validatebox",{target:_7},_8.events[_9]);
  35. }
  36. };
  37. function _a(e){
  38. var _b=e.data.target;
  39. var _c=$.data(_b,"validatebox");
  40. var _d=_c.options;
  41. if($(_b).attr("readonly")){
  42. return;
  43. }
  44. _c.validating=true;
  45. _c.value=_d.val(_b);
  46. (function(){
  47. if(!$(_b).is(":visible")){
  48. _c.validating=false;
  49. }
  50. if(_c.validating){
  51. var _e=_d.val(_b);
  52. if(_c.value!=_e){
  53. _c.value=_e;
  54. if(_c.vtimer){
  55. clearTimeout(_c.vtimer);
  56. }
  57. _c.vtimer=setTimeout(function(){
  58. $(_b).validatebox("validate");
  59. },_d.delay);
  60. }else{
  61. if(_c.message){
  62. _d.err(_b,_c.message);
  63. }
  64. }
  65. _c.ftimer=setTimeout(arguments.callee,_d.interval);
  66. }
  67. })();
  68. };
  69. function _f(e){
  70. var _10=e.data.target;
  71. var _11=$.data(_10,"validatebox");
  72. var _12=_11.options;
  73. _11.validating=false;
  74. if(_11.vtimer){
  75. clearTimeout(_11.vtimer);
  76. _11.vtimer=undefined;
  77. }
  78. if(_11.ftimer){
  79. clearTimeout(_11.ftimer);
  80. _11.ftimer=undefined;
  81. }
  82. if(_12.validateOnBlur){
  83. setTimeout(function(){
  84. $(_10).validatebox("validate");
  85. },0);
  86. }
  87. _12.err(_10,_11.message,"hide");
  88. };
  89. function _13(e){
  90. var _14=e.data.target;
  91. var _15=$.data(_14,"validatebox");
  92. _15.options.err(_14,_15.message,"show");
  93. };
  94. function _16(e){
  95. var _17=e.data.target;
  96. var _18=$.data(_17,"validatebox");
  97. if(!_18.validating){
  98. _18.options.err(_17,_18.message,"hide");
  99. }
  100. };
  101. function _19(_1a,_1b,_1c){
  102. var _1d=$.data(_1a,"validatebox");
  103. var _1e=_1d.options;
  104. var t=$(_1a);
  105. if(_1c=="hide"||!_1b){
  106. t.tooltip("hide");
  107. }else{
  108. if((t.is(":focus")&&_1d.validating)||_1c=="show"){
  109. t.tooltip($.extend({},_1e.tipOptions,{content:_1b,position:_1e.tipPosition,deltaX:_1e.deltaX,deltaY:_1e.deltaY})).tooltip("show");
  110. }
  111. }
  112. };
  113. function _1f(_20){
  114. var _21=$.data(_20,"validatebox");
  115. var _22=_21.options;
  116. var box=$(_20);
  117. _22.onBeforeValidate.call(_20);
  118. var _23=_24();
  119. _23?box.removeClass("validatebox-invalid"):box.addClass("validatebox-invalid");
  120. _22.err(_20,_21.message);
  121. _22.onValidate.call(_20,_23);
  122. return _23;
  123. function _25(msg){
  124. _21.message=msg;
  125. };
  126. function _26(_27,_28){
  127. var _29=_22.val(_20);
  128. var _2a=/([a-zA-Z_]+)(.*)/.exec(_27);
  129. var _2b=_22.rules[_2a[1]];
  130. if(_2b&&_29){
  131. var _2c=_28||_22.validParams||eval(_2a[2]);
  132. if(!_2b["validator"].call(_20,_29,_2c)){
  133. var _2d=_2b["message"];
  134. if(_2c){
  135. for(var i=0;i<_2c.length;i++){
  136. _2d=_2d.replace(new RegExp("\\{"+i+"\\}","g"),_2c[i]);
  137. }
  138. }
  139. _25(_22.invalidMessage||_2d);
  140. return false;
  141. }
  142. }
  143. return true;
  144. };
  145. function _24(){
  146. _25("");
  147. if(!_22._validateOnCreate){
  148. setTimeout(function(){
  149. _22._validateOnCreate=true;
  150. },0);
  151. return true;
  152. }
  153. if(_22.novalidate||_22.disabled){
  154. return true;
  155. }
  156. if(_22.required){
  157. if(_22.val(_20)==""){
  158. _25(_22.missingMessage);
  159. return false;
  160. }
  161. }
  162. if(_22.validType){
  163. if($.isArray(_22.validType)){
  164. for(var i=0;i<_22.validType.length;i++){
  165. if(!_26(_22.validType[i])){
  166. return false;
  167. }
  168. }
  169. }else{
  170. if(typeof _22.validType=="string"){
  171. if(!_26(_22.validType)){
  172. return false;
  173. }
  174. }else{
  175. for(var _2e in _22.validType){
  176. var _2f=_22.validType[_2e];
  177. if(!_26(_2e,_2f)){
  178. return false;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. return true;
  185. };
  186. };
  187. function _30(_31,_32){
  188. var _33=$.data(_31,"validatebox").options;
  189. if(_32!=undefined){
  190. _33.disabled=_32;
  191. }
  192. if(_33.disabled){
  193. $(_31).addClass("validatebox-disabled")._propAttr("disabled",true);
  194. }else{
  195. $(_31).removeClass("validatebox-disabled")._propAttr("disabled",false);
  196. }
  197. };
  198. function _34(_35,_36){
  199. var _37=$.data(_35,"validatebox").options;
  200. _37.readonly=_36==undefined?true:_36;
  201. if(_37.readonly||!_37.editable){
  202. $(_35).triggerHandler("blur.validatebox");
  203. $(_35).addClass("validatebox-readonly")._propAttr("readonly",true);
  204. }else{
  205. $(_35).removeClass("validatebox-readonly")._propAttr("readonly",false);
  206. }
  207. };
  208. $.fn.validatebox=function(_38,_39){
  209. if(typeof _38=="string"){
  210. return $.fn.validatebox.methods[_38](this,_39);
  211. }
  212. _38=_38||{};
  213. return this.each(function(){
  214. var _3a=$.data(this,"validatebox");
  215. if(_3a){
  216. $.extend(_3a.options,_38);
  217. }else{
  218. _1(this);
  219. _3a=$.data(this,"validatebox",{options:$.extend({},$.fn.validatebox.defaults,$.fn.validatebox.parseOptions(this),_38)});
  220. }
  221. _3a.options._validateOnCreate=_3a.options.validateOnCreate;
  222. _30(this,_3a.options.disabled);
  223. _34(this,_3a.options.readonly);
  224. _6(this);
  225. _1f(this);
  226. });
  227. };
  228. $.fn.validatebox.methods={options:function(jq){
  229. return $.data(jq[0],"validatebox").options;
  230. },destroy:function(jq){
  231. return jq.each(function(){
  232. _3(this);
  233. });
  234. },validate:function(jq){
  235. return jq.each(function(){
  236. _1f(this);
  237. });
  238. },isValid:function(jq){
  239. return _1f(jq[0]);
  240. },enableValidation:function(jq){
  241. return jq.each(function(){
  242. $(this).validatebox("options").novalidate=false;
  243. _6(this);
  244. _1f(this);
  245. });
  246. },disableValidation:function(jq){
  247. return jq.each(function(){
  248. $(this).validatebox("options").novalidate=true;
  249. _6(this);
  250. _1f(this);
  251. });
  252. },resetValidation:function(jq){
  253. return jq.each(function(){
  254. var _3b=$(this).validatebox("options");
  255. _3b._validateOnCreate=_3b.validateOnCreate;
  256. _1f(this);
  257. });
  258. },enable:function(jq){
  259. return jq.each(function(){
  260. _30(this,false);
  261. _6(this);
  262. _1f(this);
  263. });
  264. },disable:function(jq){
  265. return jq.each(function(){
  266. _30(this,true);
  267. _6(this);
  268. _1f(this);
  269. });
  270. },readonly:function(jq,_3c){
  271. return jq.each(function(){
  272. _34(this,_3c);
  273. _6(this);
  274. _1f(this);
  275. });
  276. }};
  277. $.fn.validatebox.parseOptions=function(_3d){
  278. var t=$(_3d);
  279. return $.extend({},$.parser.parseOptions(_3d,["validType","missingMessage","invalidMessage","tipPosition",{delay:"number",interval:"number",deltaX:"number"},{editable:"boolean",validateOnCreate:"boolean",validateOnBlur:"boolean"}]),{required:(t.attr("required")?true:undefined),disabled:(t.attr("disabled")?true:undefined),readonly:(t.attr("readonly")?true:undefined),novalidate:(t.attr("novalidate")!=undefined?true:undefined)});
  280. };
  281. $.fn.validatebox.defaults={required:false,validType:null,validParams:null,delay:200,interval:200,missingMessage:"This field is required.",invalidMessage:null,tipPosition:"right",deltaX:0,deltaY:0,novalidate:false,editable:true,disabled:false,readonly:false,validateOnCreate:true,validateOnBlur:false,events:{focus:_a,blur:_f,mouseenter:_13,mouseleave:_16,click:function(e){
  282. var t=$(e.data.target);
  283. if(t.attr("type")=="checkbox"||t.attr("type")=="radio"){
  284. t.focus().validatebox("validate");
  285. }
  286. }},val:function(_3e){
  287. return $(_3e).val();
  288. },err:function(_3f,_40,_41){
  289. _19(_3f,_40,_41);
  290. },tipOptions:{showEvent:"none",hideEvent:"none",showDelay:0,hideDelay:0,zIndex:"",onShow:function(){
  291. $(this).tooltip("tip").css({color:"#000",borderColor:"#CC9933",backgroundColor:"#FFFFCC"});
  292. },onHide:function(){
  293. $(this).tooltip("destroy");
  294. }},rules:{email:{validator:function(_42){
  295. return /^((([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(_42);
  296. },message:"Please enter a valid email address."},url:{validator:function(_43){
  297. return /^(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(_43);
  298. },message:"Please enter a valid URL."},length:{validator:function(_44,_45){
  299. var len=$.trim(_44).length;
  300. return len>=_45[0]&&len<=_45[1];
  301. },message:"Please enter a value between {0} and {1}."},remote:{validator:function(_46,_47){
  302. var _48={};
  303. _48[_47[1]]=_46;
  304. var _49=$.ajax({url:_47[0],dataType:"json",data:_48,async:false,cache:false,type:"post"}).responseText;
  305. return _49=="true";
  306. },message:"Please fix this field."}},onBeforeValidate:function(){
  307. },onValidate:function(_4a){
  308. }};
  309. })(jQuery);