jquery.validatebox.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * jQuery EasyUI 1.4.4
  3. *
  4. * Copyright (c) 2009-2015 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.timer){
  18. clearTimeout(_5.timer);
  19. }
  20. $(_4).tooltip("destroy");
  21. $(_4).unbind();
  22. $(_4).remove();
  23. };
  24. function _6(_7){
  25. var _8=$.data(_7,"validatebox").options;
  26. var _9=$(_7);
  27. _9.unbind(".validatebox");
  28. if(_8.novalidate||_9.is(":disabled")){
  29. return;
  30. }
  31. for(var _a in _8.events){
  32. $(_7).bind(_a+".validatebox",{target:_7},_8.events[_a]);
  33. }
  34. };
  35. function _b(e){
  36. var _c=e.data.target;
  37. var _d=$.data(_c,"validatebox");
  38. var _e=$(_c);
  39. if($(_c).attr("readonly")){
  40. return;
  41. }
  42. _d.validating=true;
  43. _d.value=undefined;
  44. (function(){
  45. if(_d.validating){
  46. if(_d.value!=_e.val()){
  47. _d.value=_e.val();
  48. if(_d.timer){
  49. clearTimeout(_d.timer);
  50. }
  51. _d.timer=setTimeout(function(){
  52. $(_c).validatebox("validate");
  53. },_d.options.delay);
  54. }else{
  55. _f(_c);
  56. }
  57. setTimeout(arguments.callee,200);
  58. }
  59. })();
  60. };
  61. function _10(e){
  62. var _11=e.data.target;
  63. var _12=$.data(_11,"validatebox");
  64. if(_12.timer){
  65. clearTimeout(_12.timer);
  66. _12.timer=undefined;
  67. }
  68. _12.validating=false;
  69. _13(_11);
  70. };
  71. function _14(e){
  72. var _15=e.data.target;
  73. if($(_15).hasClass("validatebox-invalid")){
  74. _16(_15);
  75. }
  76. };
  77. function _17(e){
  78. var _18=e.data.target;
  79. var _19=$.data(_18,"validatebox");
  80. if(!_19.validating){
  81. _13(_18);
  82. }
  83. };
  84. function _16(_1a){
  85. var _1b=$.data(_1a,"validatebox");
  86. var _1c=_1b.options;
  87. $(_1a).tooltip($.extend({},_1c.tipOptions,{content:_1b.message,position:_1c.tipPosition,deltaX:_1c.deltaX})).tooltip("show");
  88. _1b.tip=true;
  89. };
  90. function _f(_1d){
  91. var _1e=$.data(_1d,"validatebox");
  92. if(_1e&&_1e.tip){
  93. $(_1d).tooltip("reposition");
  94. }
  95. };
  96. function _13(_1f){
  97. var _20=$.data(_1f,"validatebox");
  98. _20.tip=false;
  99. $(_1f).tooltip("hide");
  100. };
  101. function _21(_22){
  102. var _23=$.data(_22,"validatebox");
  103. var _24=_23.options;
  104. var box=$(_22);
  105. _24.onBeforeValidate.call(_22);
  106. var _25=_26();
  107. _24.onValidate.call(_22,_25);
  108. return _25;
  109. function _27(msg){
  110. _23.message=msg;
  111. };
  112. function _28(_29,_2a){
  113. var _2b=box.val();
  114. var _2c=/([a-zA-Z_]+)(.*)/.exec(_29);
  115. var _2d=_24.rules[_2c[1]];
  116. if(_2d&&_2b){
  117. var _2e=_2a||_24.validParams||eval(_2c[2]);
  118. if(!_2d["validator"].call(_22,_2b,_2e)){
  119. box.addClass("validatebox-invalid");
  120. var _2f=_2d["message"];
  121. if(_2e){
  122. for(var i=0;i<_2e.length;i++){
  123. _2f=_2f.replace(new RegExp("\\{"+i+"\\}","g"),_2e[i]);
  124. }
  125. }
  126. _27(_24.invalidMessage||_2f);
  127. if(_23.validating){
  128. _16(_22);
  129. }
  130. return false;
  131. }
  132. }
  133. return true;
  134. };
  135. function _26(){
  136. box.removeClass("validatebox-invalid");
  137. _13(_22);
  138. if(_24.novalidate||box.is(":disabled")){
  139. return true;
  140. }
  141. if(_24.required){
  142. if(box.val()==""){
  143. box.addClass("validatebox-invalid");
  144. _27(_24.missingMessage);
  145. if(_23.validating){
  146. _16(_22);
  147. }
  148. return false;
  149. }
  150. }
  151. if(_24.validType){
  152. if($.isArray(_24.validType)){
  153. for(var i=0;i<_24.validType.length;i++){
  154. if(!_28(_24.validType[i])){
  155. return false;
  156. }
  157. }
  158. }else{
  159. if(typeof _24.validType=="string"){
  160. if(!_28(_24.validType)){
  161. return false;
  162. }
  163. }else{
  164. for(var _30 in _24.validType){
  165. var _31=_24.validType[_30];
  166. if(!_28(_30,_31)){
  167. return false;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. return true;
  174. };
  175. };
  176. function _32(_33,_34){
  177. var _35=$.data(_33,"validatebox").options;
  178. if(_34!=undefined){
  179. _35.novalidate=_34;
  180. }
  181. if(_35.novalidate){
  182. $(_33).removeClass("validatebox-invalid");
  183. _13(_33);
  184. }
  185. _21(_33);
  186. _6(_33);
  187. };
  188. $.fn.validatebox=function(_36,_37){
  189. if(typeof _36=="string"){
  190. return $.fn.validatebox.methods[_36](this,_37);
  191. }
  192. _36=_36||{};
  193. return this.each(function(){
  194. var _38=$.data(this,"validatebox");
  195. if(_38){
  196. $.extend(_38.options,_36);
  197. }else{
  198. _1(this);
  199. $.data(this,"validatebox",{options:$.extend({},$.fn.validatebox.defaults,$.fn.validatebox.parseOptions(this),_36)});
  200. }
  201. _32(this);
  202. _21(this);
  203. });
  204. };
  205. $.fn.validatebox.methods={options:function(jq){
  206. return $.data(jq[0],"validatebox").options;
  207. },destroy:function(jq){
  208. return jq.each(function(){
  209. _3(this);
  210. });
  211. },validate:function(jq){
  212. return jq.each(function(){
  213. _21(this);
  214. });
  215. },isValid:function(jq){
  216. return _21(jq[0]);
  217. },enableValidation:function(jq){
  218. return jq.each(function(){
  219. _32(this,false);
  220. });
  221. },disableValidation:function(jq){
  222. return jq.each(function(){
  223. _32(this,true);
  224. });
  225. }};
  226. $.fn.validatebox.parseOptions=function(_39){
  227. var t=$(_39);
  228. return $.extend({},$.parser.parseOptions(_39,["validType","missingMessage","invalidMessage","tipPosition",{delay:"number",deltaX:"number"}]),{required:(t.attr("required")?true:undefined),novalidate:(t.attr("novalidate")!=undefined?true:undefined)});
  229. };
  230. $.fn.validatebox.defaults={required:false,validType:null,validParams:null,delay:200,missingMessage:"This field is required.",invalidMessage:null,tipPosition:"right",deltaX:0,novalidate:false,events:{focus:_b,blur:_10,mouseenter:_14,mouseleave:_17,click:function(e){
  231. var t=$(e.data.target);
  232. if(!t.is(":focus")){
  233. t.trigger("focus");
  234. }
  235. }},tipOptions:{showEvent:"none",hideEvent:"none",showDelay:0,hideDelay:0,zIndex:"",onShow:function(){
  236. $(this).tooltip("tip").css({color:"#000",borderColor:"#CC9933",backgroundColor:"#FFFFCC"});
  237. },onHide:function(){
  238. $(this).tooltip("destroy");
  239. }},rules:{email:{validator:function(_3a){
  240. 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(_3a);
  241. },message:"Please enter a valid email address."},url:{validator:function(_3b){
  242. 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(_3b);
  243. },message:"Please enter a valid URL."},length:{validator:function(_3c,_3d){
  244. var len=$.trim(_3c).length;
  245. return len>=_3d[0]&&len<=_3d[1];
  246. },message:"Please enter a value between {0} and {1}."},remote:{validator:function(_3e,_3f){
  247. var _40={};
  248. _40[_3f[1]]=_3e;
  249. var _41=$.ajax({url:_3f[0],dataType:"json",data:_40,async:false,cache:false,type:"post"}).responseText;
  250. return _41=="true";
  251. },message:"Please fix this field."}},onBeforeValidate:function(){
  252. },onValidate:function(_42){
  253. }};
  254. })(jQuery);