Validform_v5.3.1.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. 通用表单验证方法
  3. Validform version 5.3.1
  4. By sean during April 7, 2010 - February 21, 2013
  5. For more information, please visit http://validform.rjboy.cn
  6. Validform is available under the terms of the MIT license.
  7. Demo:
  8. $(".demoform").Validform({//$(".demoform")指明是哪一表单需要验证,名称需加在form表单上;
  9. btnSubmit:"#btn_sub", //#btn_sub是该表单下要绑定点击提交表单事件的按钮;如果form内含有submit按钮该参数可省略;
  10. btnReset:".btn_reset",//可选项 .btn_reset是该表单下要绑定点击重置表单事件的按钮;
  11. tiptype:1, //可选项 1=>pop box,2=>side tip(parent.next.find; with default pop),3=>side tip(siblings; with default pop),4=>side tip(siblings; none pop),默认为1,也可以传入一个function函数,自定义提示信息的显示方式(可以实现你想要的任何效果,具体参见demo页);
  12. ignoreHidden:false,//可选项 true | false 默认为false,当为true时对:hidden的表单元素将不做验证;
  13. dragonfly:false,//可选项 true | false 默认false,当为true时,值为空时不做验证;
  14. tipSweep:true,//可选项 true | false 默认为false,只在表单提交时触发检测,blur事件将不会触发检测(实时验证会在后台进行,不会显示检测结果);
  15. label:".label",//可选项 选择符,在没有绑定nullmsg时查找要显示的提示文字,默认查找".Validform_label"下的文字;
  16. showAllError:false,//可选项 true | false,true:提交表单时所有错误提示信息都会显示,false:一碰到验证不通过的就停止检测后面的元素,只显示该元素的错误信息;
  17. postonce:true, //可选项 表单是否只能提交一次,true开启,不填则默认关闭;
  18. ajaxPost:true, //使用ajax方式提交表单数据,默认false,提交地址就是action指定地址;
  19. datatype:{//传入自定义datatype类型,可以是正则,也可以是函数(函数内会传入一个参数);
  20. "*6-20": /^[^\s]{6,20}$/,
  21. "z2-4" : /^[\u4E00-\u9FA5\uf900-\ufa2d]{2,4}$/,
  22. "username":function(gets,obj,curform,regxp){
  23. //参数gets是获取到的表单元素值,obj为当前表单元素,curform为当前验证的表单,regxp为内置的一些正则表达式的引用;
  24. var reg1=/^[\w\.]{4,16}$/,
  25. reg2=/^[\u4E00-\u9FA5\uf900-\ufa2d]{2,8}$/;
  26. if(reg1.test(gets)){return true;}
  27. if(reg2.test(gets)){return true;}
  28. return false;
  29. //注意return可以返回true 或 false 或 字符串文字,true表示验证通过,返回字符串表示验证失败,字符串作为错误提示显示,返回false则用errmsg或默认的错误提示;
  30. },
  31. "phone":function(){
  32. // 5.0 版本之后,要实现二选一的验证效果,datatype 的名称 不 需要以 "option_" 开头;
  33. }
  34. },
  35. usePlugin:{
  36. swfupload:{},
  37. datepicker:{},
  38. passwordstrength:{},
  39. jqtransform:{
  40. selector:"select,input"
  41. }
  42. },
  43. beforeCheck:function(curform){
  44. //在表单提交执行验证之前执行的函数,curform参数是当前表单对象。
  45. //这里明确return false的话将不会继续执行验证操作;
  46. },
  47. beforeSubmit:function(curform){
  48. //在验证成功后,表单提交前执行的函数,curform参数是当前表单对象。
  49. //这里明确return false的话表单将不会提交;
  50. },
  51. callback:function(data){
  52. //返回数据data是json格式,{"info":"demo info","status":"y"}
  53. //info: 输出提示信息;
  54. //status: 返回提交数据的状态,是否提交成功。如可以用"y"表示提交成功,"n"表示提交失败,在ajax_post.php文件返回数据里自定字符,主要用在callback函数里根据该值执行相应的回调操作;
  55. //你也可以在ajax_post.php文件返回更多信息在这里获取,进行相应操作;
  56. //ajax遇到服务端错误时也会执行回调,这时的data是{ status:**, statusText:**, readyState:**, responseText:** };
  57. //这里执行回调操作;
  58. //注意:如果不是ajax方式提交表单,传入callback,这时data参数是当前表单对象,回调函数会在表单验证全部通过后执行,然后判断是否提交表单,如果callback里明确return false,则表单不会提交,如果return true或没有return,则会提交表单。
  59. }
  60. });
  61. Validform对象的方法和属性:
  62. tipmsg:自定义提示信息,通过修改Validform对象的这个属性值来让同一个页面的不同表单使用不同的提示文字;
  63. dataType:获取内置的一些正则;
  64. eq(n):获取Validform对象的第n个元素;
  65. ajaxPost(flag,sync,url):以ajax方式提交表单。flag为true时,跳过验证直接提交,sync为true时将以同步的方式进行ajax提交,传入了url地址时,表单会提交到这个地址;
  66. abort():终止ajax的提交;
  67. submitForm(flag,url):以参数里设置的方式提交表单,flag为true时,跳过验证直接提交,传入了url地址时,表单会提交到这个地址;
  68. resetForm():重置表单;
  69. resetStatus():重置表单的提交状态。传入了postonce参数的话,表单成功提交后状态会设置为"posted",重置提交状态可以让表单继续可以提交;
  70. getStatus():获取表单的提交状态,normal:未提交,posting:正在提交,posted:已成功提交过;
  71. setStatus(status):设置表单的提交状态,可以设置normal,posting,posted三种状态,不传参则设置状态为posting,这个状态表单可以验证,但不能提交;
  72. ignore(selector):忽略对所选择对象的验证;
  73. unignore(selector):将ignore方法所忽略验证的对象重新获取验证效果;
  74. addRule(rule):可以通过Validform对象的这个方法来给表单元素绑定验证规则;
  75. check(bool,selector):对指定对象进行验证(默认验证当前整个表单),通过返回true,否则返回false(绑定实时验证的对象,格式符合要求时返回true,而不会等ajax的返回结果),bool为true时则只验证不显示提示信息;
  76. config(setup):可以通过这个方法来指定表单的提交地址,给表单ajax和实时验证的ajax里设置参数;
  77. */
  78. (function($,win,undef){
  79. var errorobj=null,//指示当前验证失败的表单元素;
  80. msgobj=null,//pop box object
  81. msghidden=true;//msgbox hidden?
  82. var tipmsg={//默认提示文字;
  83. tit:"提示信息",
  84. w:{
  85. "*":"不能为空!",
  86. "*6-16":"请填写6到16位任意字符!",
  87. "n":"请填写数字!",
  88. "n6-16":"请填写6到16位数字!",
  89. "s":"不能输入特殊字符!",
  90. "s6-18":"请填写6到18位字符!",
  91. "p":"请填写邮政编码!",
  92. "m":"请填写手机号码!",
  93. "e":"邮箱地址格式不对!",
  94. "url":"请填写网址!"
  95. },
  96. def:"请填写正确信息!",
  97. undef:"datatype未定义!",
  98. reck:"两次输入的内容不一致!",
  99. r:"通过信息验证!",
  100. c:"正在检测信息…",
  101. s:"请{填写|选择}{0|信息}!",
  102. v:"所填信息没有经过验证,请稍后…",
  103. p:"正在提交数据…"
  104. }
  105. $.Tipmsg=tipmsg;
  106. var Validform=function(forms,settings,inited){
  107. var settings=$.extend({},Validform.defaults,settings);
  108. settings.datatype && $.extend(Validform.util.dataType,settings.datatype);
  109. var brothers=this;
  110. brothers.tipmsg={w:{}};
  111. brothers.settings=settings;
  112. brothers.forms=forms;
  113. brothers.objects=[];
  114. //创建子对象时不再绑定事件;
  115. if(inited===true){
  116. return false;
  117. }
  118. forms.each(function(){
  119. //已经绑定事件时跳过,避免事件重复绑定;
  120. if(this.validform_inited=="inited"){return true;}
  121. this.validform_inited="inited";
  122. var $this=$(this);
  123. //防止表单按钮双击提交两次;
  124. this.validform_status="normal"; //normal | posting | posted;
  125. //label,在没有输入时查找要显示的提示文字;
  126. this.validform_label=settings.label;
  127. //让每个Validform对象都能自定义tipmsg;
  128. $this.data("tipmsg",brothers.tipmsg);
  129. //bind the blur event;
  130. $this.delegate("[datatype]","blur",function(){
  131. //判断是否是在提交表单操作时触发的验证请求;
  132. var subpost=arguments[1];
  133. Validform.util.check.call(this,$this,brothers,subpost);
  134. });
  135. //点击表单元素,默认文字消失效果;
  136. //表单元素值比较时的信息提示增强;
  137. //radio、checkbox提示信息增强;
  138. //外调插件初始化;
  139. Validform.util.enhance.call($this,settings.tiptype,settings.usePlugin,settings.tipSweep);
  140. settings.btnSubmit && $this.find(settings.btnSubmit).bind("click",function(){
  141. $this.trigger("submit");
  142. return false;
  143. });
  144. $this.submit(function(){
  145. var subflag=Validform.util.submitForm.call($this,settings);
  146. subflag === undef && (subflag=true);
  147. return subflag;
  148. });
  149. $this.find("[type='reset']").add($this.find(settings.btnReset)).bind("click",function(){
  150. Validform.util.resetForm.call($this);
  151. });
  152. });
  153. //预创建pop box;
  154. if( settings.tiptype==1 || (settings.tiptype==2 || settings.tiptype==3) && settings.ajaxPost ){
  155. creatMsgbox();
  156. }
  157. }
  158. Validform.defaults={
  159. tiptype:1,
  160. tipSweep:false,
  161. showAllError:false,
  162. postonce:false,
  163. ajaxPost:false
  164. }
  165. Validform.util={
  166. dataType:{
  167. "*":/[\w\W]+/,
  168. "*6-16":/^[\w\W]{6,16}$/,
  169. "n":/^\d+$/,
  170. "n6-16":/^\d{6,16}$/,
  171. "s":/^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]+$/,
  172. "s6-18":/^[\u4E00-\u9FA5\uf900-\ufa2d\w\.\s]{6,18}$/,
  173. "p":/^[0-9]{6}$/,
  174. /*update-begin--Author:dangzhenghui Date:20170627 for:手机支持170号段*/
  175. "m":/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}$|19[0-9]{9}$|16[0-9]{9}$|18[0-9]{9}$/,
  176. /*update-end--Author:dangzhenghui Date:20170627 for:手机支持170号段*/
  177. "e":/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
  178. "url":/^(\w+:\/\/)?\w+(\.\w+)+.*$/
  179. },
  180. toString:Object.prototype.toString,
  181. isEmpty:function(val){
  182. return val==="" || val===$.trim(this.attr("tip"));
  183. },
  184. getValue:function(obj){
  185. var inputval,
  186. curform=this;
  187. if(obj.is(":radio")){
  188. inputval=curform.find(":radio[name='"+obj.attr("name")+"']:checked").val();
  189. inputval= inputval===undef ? "" : inputval;
  190. }else if(obj.is(":checkbox")){
  191. inputval="";
  192. curform.find(":checkbox[name='"+obj.attr("name")+"']:checked").each(function(){
  193. inputval +=$(this).val()+',';
  194. })
  195. inputval= inputval===undef ? "" : inputval;
  196. }else if(obj.is(":textarea")){
  197. inputval= obj.html();
  198. }else{
  199. inputval=obj.val();
  200. }
  201. inputval=$.trim(inputval);
  202. return Validform.util.isEmpty.call(obj,inputval) ? "" : inputval;
  203. },
  204. enhance:function(tiptype,usePlugin,tipSweep,addRule){
  205. var curform=this;
  206. //页面上不存在提示信息的标签时,自动创建;
  207. curform.find("[datatype]").each(function(){
  208. if(tiptype==2){
  209. if($(this).parent().next().find(".Validform_checktip").length==0){
  210. $(this).parent().next().append("<span class='Validform_checktip' />");
  211. }
  212. }else if(tiptype==3 || tiptype==4){
  213. if($(this).siblings(".Validform_checktip").length==0){
  214. $(this).parent().append("<span class='Validform_checktip' />");
  215. }
  216. }
  217. })
  218. //表单元素值比较时的信息提示增强;
  219. curform.find("input[recheck]").each(function(){
  220. //已经绑定事件时跳过;
  221. if(this.validform_inited=="inited"){return true;}
  222. this.validform_inited="inited";
  223. var _this=$(this);
  224. var recheckinput=curform.find("input[name='"+$(this).attr("recheck")+"']");
  225. recheckinput.bind("keyup",function(){
  226. if(recheckinput.val()==_this.val() && recheckinput.val() != ""){
  227. if(recheckinput.attr("tip")){
  228. if(recheckinput.attr("tip") == recheckinput.val()){return false;}
  229. }
  230. _this.trigger("blur");
  231. }
  232. }).bind("blur",function(){
  233. if(recheckinput.val()!=_this.val() && _this.val()!=""){
  234. if(_this.attr("tip")){
  235. if(_this.attr("tip") == _this.val()){return false;}
  236. }
  237. _this.trigger("blur");
  238. }
  239. });
  240. });
  241. //hasDefaultText;
  242. curform.find("[tip]").each(function(){//tip是表单元素的默认提示信息,这是点击清空效果;
  243. //已经绑定事件时跳过;
  244. if(this.validform_inited=="inited"){return true;}
  245. this.validform_inited="inited";
  246. var defaultvalue=$(this).attr("tip");
  247. var altercss=$(this).attr("altercss");
  248. $(this).focus(function(){
  249. if($(this).val()==defaultvalue){
  250. $(this).val('');
  251. if(altercss){$(this).removeClass(altercss);}
  252. }
  253. }).blur(function(){
  254. if($.trim($(this).val())===''){
  255. $(this).val(defaultvalue);
  256. if(altercss){$(this).addClass(altercss);}
  257. }
  258. });
  259. });
  260. //enhance info feedback for checkbox & radio;
  261. curform.find(":checkbox[datatype],:radio[datatype]").each(function(){
  262. //已经绑定事件时跳过;
  263. if(this.validform_inited=="inited"){return true;}
  264. this.validform_inited="inited";
  265. var _this=$(this);
  266. var name=_this.attr("name");
  267. curform.find("[name='"+name+"']").filter(":checkbox,:radio").bind("click",function(){
  268. //避免多个事件绑定时的取值滞后问题;
  269. setTimeout(function(){
  270. _this.trigger("blur");
  271. },0);
  272. });
  273. });
  274. //select multiple;
  275. curform.find("select[datatype][multiple]").bind("click",function(){
  276. var _this=$(this);
  277. setTimeout(function(){
  278. _this.trigger("blur");
  279. },0);
  280. });
  281. //plugins here to start;
  282. Validform.util.usePlugin.call(curform,usePlugin,tiptype,tipSweep,addRule);
  283. },
  284. usePlugin:function(plugin,tiptype,tipSweep,addRule){
  285. /*
  286. plugin:settings.usePlugin;
  287. tiptype:settings.tiptype;
  288. tipSweep:settings.tipSweep;
  289. addRule:是否在addRule时触发;
  290. */
  291. var curform=this,
  292. plugin=plugin || {};
  293. //swfupload;
  294. if(curform.find("input[plugin='swfupload']").length && typeof(swfuploadhandler) != "undefined"){
  295. var custom={
  296. custom_settings:{
  297. form:curform,
  298. showmsg:function(msg,type,obj){
  299. Validform.util.showmsg.call(curform,msg,tiptype,{obj:curform.find("input[plugin='swfupload']"),type:type,sweep:tipSweep});
  300. }
  301. }
  302. };
  303. custom=$.extend(true,{},plugin.swfupload,custom);
  304. curform.find("input[plugin='swfupload']").each(function(n){
  305. if(this.validform_inited=="inited"){return true;}
  306. this.validform_inited="inited";
  307. $(this).val("");
  308. swfuploadhandler.init(custom,n);
  309. });
  310. }
  311. //datepicker;
  312. if(curform.find("input[plugin='datepicker']").length && $.fn.datePicker){
  313. plugin.datepicker=plugin.datepicker || {};
  314. if(plugin.datepicker.format){
  315. Date.format=plugin.datepicker.format;
  316. delete plugin.datepicker.format;
  317. }
  318. if(plugin.datepicker.firstDayOfWeek){
  319. Date.firstDayOfWeek=plugin.datepicker.firstDayOfWeek;
  320. delete plugin.datepicker.firstDayOfWeek;
  321. }
  322. curform.find("input[plugin='datepicker']").each(function(n){
  323. if(this.validform_inited=="inited"){return true;}
  324. this.validform_inited="inited";
  325. plugin.datepicker.callback && $(this).bind("dateSelected",function(){
  326. var d=new Date( $.event._dpCache[this._dpId].getSelected()[0] ).asString(Date.format);
  327. plugin.datepicker.callback(d,this);
  328. });
  329. $(this).datePicker(plugin.datepicker);
  330. });
  331. }
  332. //passwordstrength;
  333. if(curform.find("input[plugin*='passwordStrength']").length && $.fn.passwordStrength){
  334. plugin.passwordstrength=plugin.passwordstrength || {};
  335. plugin.passwordstrength.showmsg=function(obj,msg,type){
  336. Validform.util.showmsg.call(curform,msg,tiptype,{obj:obj,type:type,sweep:tipSweep});
  337. };
  338. curform.find("input[plugin='passwordStrength']").each(function(n){
  339. if(this.validform_inited=="inited"){return true;}
  340. this.validform_inited="inited";
  341. $(this).passwordStrength(plugin.passwordstrength);
  342. });
  343. }
  344. //jqtransform;
  345. if(addRule!="addRule" && plugin.jqtransform && $.fn.jqTransSelect){
  346. var jqTransformHideSelect = function(oTarget){
  347. var ulVisible = $('.jqTransformSelectWrapper ul:visible');
  348. ulVisible.each(function(){
  349. var oSelect = $(this).parents(".jqTransformSelectWrapper:first").find("select").get(0);
  350. //do not hide if click on the label object associated to the select
  351. if( !(oTarget && oSelect.oLabel && oSelect.oLabel.get(0) == oTarget.get(0)) ){$(this).hide();}
  352. });
  353. };
  354. /* Check for an external click */
  355. var jqTransformCheckExternalClick = function(event) {
  356. if ($(event.target).parents('.jqTransformSelectWrapper').length === 0) { jqTransformHideSelect($(event.target)); }
  357. };
  358. var jqTransformAddDocumentListener = function (){
  359. $(document).mousedown(jqTransformCheckExternalClick);
  360. };
  361. if(plugin.jqtransform.selector){
  362. curform.find(plugin.jqtransform.selector).filter('input:submit, input:reset, input[type="button"]').jqTransInputButton();
  363. curform.find(plugin.jqtransform.selector).filter('input:text, input:password').jqTransInputText();
  364. curform.find(plugin.jqtransform.selector).filter('input:checkbox').jqTransCheckBox();
  365. curform.find(plugin.jqtransform.selector).filter('input:radio').jqTransRadio();
  366. curform.find(plugin.jqtransform.selector).filter('textarea').jqTransTextarea();
  367. if(curform.find(plugin.jqtransform.selector).filter("select").length > 0 ){
  368. curform.find(plugin.jqtransform.selector).filter("select").jqTransSelect();
  369. jqTransformAddDocumentListener();
  370. }
  371. }else{
  372. curform.jqTransform();
  373. }
  374. curform.find(".jqTransformSelectWrapper").find("li a").click(function(){
  375. $(this).parents(".jqTransformSelectWrapper").find("select").trigger("blur");
  376. });
  377. }
  378. },
  379. getNullmsg:function(curform){
  380. var obj=this;
  381. var reg=/[\u4E00-\u9FA5\uf900-\ufa2da-zA-Z\s]+/g;
  382. var nullmsg;
  383. var label=curform[0].validform_label || ".Validform_label";
  384. label=obj.siblings(label).text() || obj.siblings().find(label).text() || obj.parent().siblings(label).text() || obj.parent().siblings().find(label).text();
  385. label=label.match(reg) || [""];
  386. reg=/\{(.+)\|(.+)\}/;
  387. nullmsg=curform.data("tipmsg").s || tipmsg.s;
  388. label=label[0].replace(/\s(?![a-zA-Z])/g,"");
  389. if(label != ""){
  390. nullmsg=nullmsg.replace(/\{0\|(.+)\}/,label);
  391. if(obj.attr("recheck")){
  392. nullmsg=nullmsg.replace(/\{(.+)\}/,"");
  393. obj.attr("nullmsg",nullmsg);
  394. return nullmsg;
  395. }
  396. }else{
  397. nullmsg=obj.is(":checkbox,:radio,select") ? nullmsg.replace(/\{0\|(.+)\}/,"") : nullmsg.replace(/\{0\|(.+)\}/,"$1");
  398. }
  399. nullmsg=obj.is(":checkbox,:radio,select") ? nullmsg.replace(reg,"$2") : nullmsg.replace(reg,"$1");
  400. obj.attr("nullmsg",nullmsg);
  401. return nullmsg;
  402. },
  403. getErrormsg:function(curform,datatype,recheck){
  404. var obj=this;
  405. var regxp=/^(.+?)((\d+)-(\d+))?$/,
  406. regxp2=/^(.+?)(\d+)-(\d+)$/,
  407. regxp3=/(.*?)\d+(.+?)\d+(.*)/,
  408. mac=datatype.match(regxp),
  409. temp,str;
  410. //如果是值不一样而报错;
  411. if(recheck=="recheck"){
  412. str=curform.data("tipmsg").reck || tipmsg.reck;
  413. return str;
  414. }
  415. //如果原来就有,直接显示该项的提示信息;
  416. if(mac[0] in tipmsg.w){
  417. return curform.data("tipmsg").w[mac[0]] || tipmsg.w[mac[0]];
  418. }
  419. //没有的话在提示对象里查找相似;
  420. for(var name in tipmsg.w){
  421. if(name.indexOf(mac[1])!=-1 && regxp2.test(name)){
  422. str=(curform.data("tipmsg").w[name] || tipmsg.w[name]).replace(regxp3,"$1"+mac[3]+"$2"+mac[4]+"$3");
  423. tipmsg.w[mac[0]]=str;
  424. return str;
  425. }
  426. }
  427. return curform.data("tipmsg").def || tipmsg.def;
  428. },
  429. _regcheck:function(datatype,gets,obj,curform){
  430. var curform=curform,
  431. info=null,
  432. passed=false,
  433. reg=/\/.+\//g,
  434. regex=/^(.+?)(\d+)-(\d+)$/,
  435. type=3;//default set to wrong type, 2,3,4;
  436. //datatype有三种情况:正则,函数和直接绑定的正则;
  437. //直接是正则;
  438. if(reg.test(datatype)){
  439. var regstr=datatype.match(reg)[0].slice(1,-1);
  440. var param=datatype.replace(reg,"");
  441. var rexp=RegExp(regstr,param);
  442. passed=rexp.test(gets);
  443. //function;
  444. }else if(Validform.util.toString.call(Validform.util.dataType[datatype])=="[object Function]"){
  445. passed=Validform.util.dataType[datatype](gets,obj,curform,Validform.util.dataType);
  446. if(passed === true || passed===undef){
  447. passed = true;
  448. }else{
  449. info= passed;
  450. passed=false;
  451. }
  452. //自定义正则;
  453. }else{
  454. //自动扩展datatype;
  455. if(!(datatype in Validform.util.dataType)){
  456. var mac=datatype.match(regex),
  457. temp;
  458. if(!mac){
  459. passed=false;
  460. info=curform.data("tipmsg").undef||tipmsg.undef;
  461. }else{
  462. for(var name in Validform.util.dataType){
  463. temp=name.match(regex);
  464. if(!temp){continue;}
  465. if(mac[1]===temp[1]){
  466. var str=Validform.util.dataType[name].toString(),
  467. param=str.match(/\/[mgi]*/g)[1].replace("\/",""),
  468. regxp=new RegExp("\\{"+temp[2]+","+temp[3]+"\\}","g");
  469. str=str.replace(/\/[mgi]*/g,"\/").replace(regxp,"{"+mac[2]+","+mac[3]+"}").replace(/^\//,"").replace(/\/$/,"");
  470. Validform.util.dataType[datatype]=new RegExp(str,param);
  471. break;
  472. }
  473. }
  474. }
  475. }
  476. if(Validform.util.toString.call(Validform.util.dataType[datatype])=="[object RegExp]"){
  477. passed=Validform.util.dataType[datatype].test(gets);
  478. }
  479. }
  480. if(passed){
  481. type=2;
  482. info=obj.attr("sucmsg") || curform.data("tipmsg").r||tipmsg.r;
  483. //规则验证通过后,还需要对绑定recheck的对象进行值比较;
  484. if(obj.attr("recheck")){
  485. var theother=curform.find("input[name='"+obj.attr("recheck")+"']:first");
  486. if(gets!=theother.val()){
  487. passed=false;
  488. type=3;
  489. info=obj.attr("errormsg") || Validform.util.getErrormsg.call(obj,curform,datatype,"recheck");
  490. }
  491. }
  492. }else{
  493. info=info || obj.attr("errormsg") || Validform.util.getErrormsg.call(obj,curform,datatype);
  494. //验证不通过且为空时;
  495. if(Validform.util.isEmpty.call(obj,gets)){
  496. info=obj.attr("nullmsg") || Validform.util.getNullmsg.call(obj,curform);
  497. }
  498. }
  499. return{
  500. passed:passed,
  501. type:type,
  502. info:info
  503. };
  504. },
  505. regcheck:function(datatype,gets,obj){
  506. /*
  507. datatype:datatype;
  508. gets:inputvalue;
  509. obj:input object;
  510. */
  511. var curform=this,
  512. info=null,
  513. passed=false,
  514. type=3;//default set to wrong type, 2,3,4;
  515. //ignore;
  516. if(obj.attr("ignore")==="ignore" && Validform.util.isEmpty.call(obj,gets)){
  517. if(obj.data("cked")){
  518. info="";
  519. }
  520. return {
  521. passed:true,
  522. type:4,
  523. info:info
  524. };
  525. }
  526. obj.data("cked","cked");//do nothing if is the first time validation triggered;
  527. var dtype=Validform.util.parseDatatype(datatype);
  528. var res;
  529. for(var eithor=0; eithor<dtype.length; eithor++){
  530. for(var dtp=0; dtp<dtype[eithor].length; dtp++){
  531. res=Validform.util._regcheck(dtype[eithor][dtp],gets,obj,curform);
  532. if(!res.passed){
  533. break;
  534. }
  535. }
  536. if(res.passed){
  537. break;
  538. }
  539. }
  540. return res;
  541. },
  542. parseDatatype:function(datatype){
  543. /*
  544. 字符串里面只能含有一个正则表达式;
  545. Datatype名称必须是字母,数字、下划线或*号组成;
  546. datatype="/regexp/|phone|tel,s,e|f,e";
  547. ==>[["/regexp/"],["phone"],["tel","s","e"],["f","e"]];
  548. */
  549. var reg=/\/.+?\/[mgi]*(?=(,|$|\||\s))|[\w\*-]+/g,
  550. dtype=datatype.match(reg),
  551. sepor=datatype.replace(reg,"").replace(/\s*/g,"").split(""),
  552. arr=[],
  553. m=0;
  554. arr[0]=[];
  555. arr[0].push(dtype[0]);
  556. for(var n=0;n<sepor.length;n++){
  557. if(sepor[n]=="|"){
  558. m++;
  559. arr[m]=[];
  560. }
  561. arr[m].push(dtype[n+1]);
  562. }
  563. return arr;
  564. },
  565. showmsg:function(msg,type,o,triggered){
  566. /*
  567. msg:提示文字;
  568. type:提示信息显示方式;
  569. o:{obj:当前对象, type:1=>正在检测 | 2=>通过, sweep:true | false},
  570. triggered:在blur或提交表单触发的验证中,有些情况不需要显示提示文字,如自定义弹出提示框的显示方式,不需要每次blur时就马上弹出提示;
  571. tiptype:1\2\3时都有坑能会弹出自定义提示框
  572. tiptype:1时在triggered bycheck时不弹框
  573. tiptype:2\3时在ajax时弹框
  574. tipSweep为true时在triggered bycheck时不触发showmsg,但ajax出错的情况下要提示
  575. */
  576. //如果msg为undefined,那么就没必要执行后面的操作,ignore有可能会出现这情况;
  577. if(msg==undef){return;}
  578. //tipSweep为true,且当前不是处于错误状态时,blur事件不触发信息显示;
  579. if(triggered=="bycheck" && o.sweep && (o.obj && !o.obj.is(".Validform_error") || typeof type == "function")){return;}
  580. $.extend(o,{curform:this});
  581. if(typeof type == "function"){
  582. type(msg,o,Validform.util.cssctl);
  583. return;
  584. }
  585. if(type==1 || triggered=="byajax" && type!=4){
  586. msgobj.find(".Validform_info").html(msg);
  587. }
  588. //tiptypt=1时,blur触发showmsg,验证是否通过都不弹框,提交表单触发的话,只要验证出错,就弹框;
  589. if(type==1 && triggered!="bycheck" && o.type!=2 || triggered=="byajax" && type!=4){
  590. msghidden=false;
  591. msgobj.find(".iframe").css("height",msgobj.outerHeight());
  592. msgobj.show();
  593. setCenter(msgobj,100);
  594. }
  595. if(type==2 && o.obj){
  596. o.obj.parent().next().find(".Validform_checktip").html(msg);
  597. Validform.util.cssctl(o.obj.parent().next().find(".Validform_checktip"),o.type);
  598. }
  599. if((type==3 || type==4) && o.obj){
  600. o.obj.siblings(".Validform_checktip").html(msg);
  601. Validform.util.cssctl(o.obj.siblings(".Validform_checktip"),o.type);
  602. }
  603. },
  604. cssctl:function(obj,status){
  605. switch(status){
  606. case 1:
  607. obj.removeClass("Validform_right Validform_wrong").addClass("Validform_checktip Validform_loading");//checking;
  608. break;
  609. case 2:
  610. obj.removeClass("Validform_wrong Validform_loading").addClass("Validform_checktip Validform_right");//passed;
  611. break;
  612. case 4:
  613. obj.removeClass("Validform_right Validform_wrong Validform_loading").addClass("Validform_checktip");//for ignore;
  614. break;
  615. default:
  616. obj.removeClass("Validform_right Validform_loading").addClass("Validform_checktip Validform_wrong");//wrong;
  617. }
  618. },
  619. check:function(curform,brothers,subpost,bool){
  620. /*
  621. 检测单个表单元素;
  622. 验证通过返回true,否则返回false、实时验证返回值为ajax;
  623. bool,传入true则只检测不显示提示信息;
  624. */
  625. var settings=brothers.settings;
  626. var subpost=subpost || "";
  627. var inputval=Validform.util.getValue.call(curform,$(this));
  628. //隐藏或绑定dataIgnore的表单对象不做验证;
  629. if(settings.ignoreHidden && $(this).is(":hidden") || $(this).data("dataIgnore")==="dataIgnore"){
  630. return true;
  631. }
  632. //dragonfly=true时,没有绑定ignore,值为空不做验证,但验证不通过;
  633. if(settings.dragonfly && !$(this).data("cked") && Validform.util.isEmpty.call($(this),inputval) && $(this).attr("ignore")!="ignore"){
  634. return false;
  635. }
  636. var flag=Validform.util.regcheck.call(curform,$(this).attr("datatype"),inputval,$(this));
  637. //值没变化不做检测,这时要考虑recheck情况;
  638. //不是在提交表单时触发的ajax验证;
  639. if(inputval==this.validform_lastval && !$(this).attr("recheck") && subpost==""){
  640. return flag.passed ? true : false;
  641. }
  642. this.validform_lastval=inputval;//存储当前值;
  643. var _this;
  644. errorobj=_this=$(this);
  645. if(!flag.passed){
  646. //取消正在进行的ajax验证;
  647. Validform.util.abort.call(_this[0]);
  648. if(!bool){
  649. //传入"bycheck",指示当前是check方法里调用的,当tiptype=1时,blur事件不让触发错误信息显示;
  650. Validform.util.showmsg.call(curform,flag.info,settings.tiptype,{obj:$(this),type:flag.type,sweep:settings.tipSweep},"bycheck");
  651. !settings.tipSweep && _this.addClass("Validform_error");
  652. }
  653. return false;
  654. }
  655. //验证通过的话,如果绑定有ajaxurl,要执行ajax检测;
  656. //当ignore="ignore"时,为空值可以通过验证,这时不需要ajax检测;
  657. var ajaxurl=$(this).attr("ajaxurl");
  658. if(ajaxurl && !Validform.util.isEmpty.call($(this),inputval) && !bool){
  659. var inputobj=$(this);
  660. //当提交表单时,表单中的某项已经在执行ajax检测,这时需要让该项ajax结束后继续提交表单;
  661. if(subpost=="postform"){
  662. inputobj[0].validform_subpost="postform";
  663. }else{
  664. inputobj[0].validform_subpost="";
  665. }
  666. if(inputobj[0].validform_valid==="posting" && inputval==inputobj[0].validform_ckvalue){return "ajax";}
  667. inputobj[0].validform_valid="posting";
  668. inputobj[0].validform_ckvalue=inputval;
  669. Validform.util.showmsg.call(curform,brothers.tipmsg.c||tipmsg.c,settings.tiptype,{obj:inputobj,type:1,sweep:settings.tipSweep},"bycheck");
  670. Validform.util.abort.call(_this[0]);
  671. var ajaxsetup=curform[0].validform_config || {};
  672. ajaxsetup=ajaxsetup.ajaxurl || {};
  673. var localconfig={
  674. type: "POST",
  675. cache:false,
  676. url: ajaxurl,
  677. data: "param="+encodeURIComponent(inputval)+"&name="+encodeURIComponent($(this).attr("name")),
  678. success: function(data){
  679. if($.trim(data.status)==="y"){
  680. inputobj[0].validform_valid="true";
  681. data.info && inputobj.attr("sucmsg",data.info);
  682. Validform.util.showmsg.call(curform,inputobj.attr("sucmsg") || brothers.tipmsg.r||tipmsg.r,settings.tiptype,{obj:inputobj,type:2,sweep:settings.tipSweep},"bycheck");
  683. _this.removeClass("Validform_error");
  684. errorobj=null;
  685. if(inputobj[0].validform_subpost=="postform"){
  686. curform.trigger("submit");
  687. }
  688. }else{
  689. inputobj[0].validform_valid=data.info;
  690. Validform.util.showmsg.call(curform,data.info,settings.tiptype,{obj:inputobj,type:3,sweep:settings.tipSweep});
  691. _this.addClass("Validform_error");
  692. }
  693. _this[0].validform_ajax=null;
  694. },
  695. error: function(data){
  696. if(data.status=="200"){
  697. if(data.responseText=="y"){
  698. ajaxsetup.success({"status":"y"});
  699. }else{
  700. ajaxsetup.success({"status":"n","info":data.responseText});
  701. }
  702. return false;
  703. }
  704. //正在检测时,要检测的数据发生改变,这时要终止当前的ajax。不是这种情况引起的ajax错误,那么显示相关错误信息;
  705. if(data.statusText!=="abort"){
  706. var msg="status: "+data.status+"; statusText: "+data.statusText;
  707. Validform.util.showmsg.call(curform,msg,settings.tiptype,{obj:inputobj,type:3,sweep:settings.tipSweep});
  708. _this.addClass("Validform_error");
  709. }
  710. inputobj[0].validform_valid=data.statusText;
  711. _this[0].validform_ajax=null;
  712. //localconfig.error返回true表示还需要执行temp_err;
  713. return true;
  714. }
  715. }
  716. if(ajaxsetup.success){
  717. var temp_suc=ajaxsetup.success;
  718. ajaxsetup.success=function(data){
  719. localconfig.success(data);
  720. temp_suc(data,inputobj);
  721. }
  722. }
  723. if(ajaxsetup.error){
  724. var temp_err=ajaxsetup.error;
  725. ajaxsetup.error=function(data){
  726. //localconfig.error返回false表示不需要执行temp_err;
  727. localconfig.error(data) && temp_err(data,inputobj);
  728. }
  729. }
  730. ajaxsetup=$.extend({},localconfig,ajaxsetup,{dataType:"json"});
  731. _this[0].validform_ajax=$.ajax(ajaxsetup);
  732. return "ajax";
  733. }else if(ajaxurl && Validform.util.isEmpty.call($(this),inputval)){
  734. Validform.util.abort.call(_this[0]);
  735. _this[0].validform_valid="true";
  736. }
  737. if(!bool){
  738. Validform.util.showmsg.call(curform,flag.info,settings.tiptype,{obj:$(this),type:flag.type,sweep:settings.tipSweep},"bycheck");
  739. _this.removeClass("Validform_error");
  740. }
  741. errorobj=null;
  742. return true;
  743. },
  744. submitForm:function(settings,flg,url,ajaxPost,sync){
  745. /*
  746. flg===true时跳过验证直接提交;
  747. ajaxPost==="ajaxPost"指示当前表单以ajax方式提交;
  748. */
  749. var curform=this;
  750. //表单正在提交时点击提交按钮不做反应;
  751. if(curform[0].validform_status==="posting"){return false;}
  752. //要求只能提交一次时;
  753. if(settings.postonce && curform[0].validform_status==="posted"){return false;}
  754. var beforeCheck=settings.beforeCheck && settings.beforeCheck(curform);
  755. if(beforeCheck===false){return false;}
  756. var flag=true,
  757. inflag;
  758. curform.find("[datatype]").each(function(){
  759. //跳过验证;
  760. if(flg){
  761. return false;
  762. }
  763. //隐藏或绑定dataIgnore的表单对象不做验证;
  764. if(settings.ignoreHidden && $(this).is(":hidden") || $(this).data("dataIgnore")==="dataIgnore"){
  765. return true;
  766. }
  767. var inputval=Validform.util.getValue.call(curform,$(this)),
  768. _this;
  769. errorobj=_this=$(this);
  770. inflag=Validform.util.regcheck.call(curform,$(this).attr("datatype"),inputval,$(this));
  771. if(!inflag.passed){
  772. Validform.util.showmsg.call(curform,inflag.info,settings.tiptype,{obj:$(this),type:inflag.type,sweep:settings.tipSweep});
  773. _this.addClass("Validform_error");
  774. if(!settings.showAllError){
  775. _this.focus();
  776. flag=false;
  777. return false;
  778. }
  779. flag && (flag=false);
  780. return true;
  781. }
  782. //当ignore="ignore"时,为空值可以通过验证,这时不需要ajax检测;
  783. if($(this).attr("ajaxurl") && !Validform.util.isEmpty.call($(this),inputval)){
  784. if(this.validform_valid!=="true"){
  785. var thisobj=$(this);
  786. Validform.util.showmsg.call(curform,curform.data("tipmsg").v||tipmsg.v,settings.tiptype,{obj:thisobj,type:3,sweep:settings.tipSweep});
  787. _this.addClass("Validform_error");
  788. thisobj.trigger("blur",["postform"]);//continue the form post;
  789. if(!settings.showAllError){
  790. flag=false;
  791. return false;
  792. }
  793. flag && (flag=false);
  794. return true;
  795. }
  796. }else if($(this).attr("ajaxurl") && Validform.util.isEmpty.call($(this),inputval)){
  797. Validform.util.abort.call(this);
  798. this.validform_valid="true";
  799. }
  800. Validform.util.showmsg.call(curform,inflag.info,settings.tiptype,{obj:$(this),type:inflag.type,sweep:settings.tipSweep});
  801. _this.removeClass("Validform_error");
  802. errorobj=null;
  803. });
  804. if(settings.showAllError){
  805. curform.find(".Validform_error:first").focus();
  806. }
  807. if(flag){
  808. var beforeSubmit=settings.beforeSubmit && settings.beforeSubmit(curform);
  809. if(beforeSubmit===false){return false;}
  810. curform[0].validform_status="posting";
  811. var config=curform[0].validform_config || {}
  812. if(settings.ajaxPost || ajaxPost==="ajaxPost"){
  813. //获取配置参数;
  814. var ajaxsetup=config.ajaxpost || {};
  815. //有可能需要动态的改变提交地址,所以把action所指定的url层级设为最低;
  816. ajaxsetup.url=url || ajaxsetup.url || config.url || curform.attr("action");
  817. //byajax:ajax时,tiptye为1、2或3需要弹出提示框;
  818. //如果setup里已经自定义了success或error方法,这里不显示提示信息;
  819. ajaxsetup.success || ajaxsetup.error || Validform.util.showmsg.call(curform,curform.data("tipmsg").p||tipmsg.p,settings.tiptype,{obj:curform,type:1,sweep:settings.tipSweep},"byajax");
  820. //方法里的优先级要高;
  821. //有undefined情况;
  822. if(sync){
  823. ajaxsetup.async=false;
  824. }else if(sync===false){
  825. ajaxsetup.async=true;
  826. }
  827. if(ajaxsetup.success){
  828. var temp_suc=ajaxsetup.success;
  829. ajaxsetup.success=function(data){
  830. settings.callback && settings.callback(data);
  831. curform[0].validform_ajax=null;
  832. if($.trim(data.status)==="y"){
  833. curform[0].validform_status="posted";
  834. }else{
  835. curform[0].validform_status="normal";
  836. }
  837. temp_suc(data,curform);
  838. }
  839. }
  840. if(ajaxsetup.error){
  841. var temp_err=ajaxsetup.error;
  842. ajaxsetup.error=function(data){
  843. settings.callback && settings.callback(data);
  844. curform[0].validform_status="normal";
  845. curform[0].validform_ajax=null;
  846. temp_err(data,curform);
  847. }
  848. }
  849. var localconfig={
  850. type: "POST",
  851. async:true,
  852. data: curform.serializeArray(),
  853. success: function(data){
  854. if($.trim(data.status)==="y"){
  855. //成功提交;
  856. curform[0].validform_status="posted";
  857. Validform.util.showmsg.call(curform,data.info,settings.tiptype,{obj:curform,type:2,sweep:settings.tipSweep},"byajax");
  858. }else{
  859. //提交出错;
  860. curform[0].validform_status="normal";
  861. Validform.util.showmsg.call(curform,data.info,settings.tiptype,{obj:curform,type:3,sweep:settings.tipSweep},"byajax");
  862. }
  863. settings.callback && settings.callback(data);
  864. curform[0].validform_ajax=null;
  865. },
  866. error: function(data){
  867. var msg="status: "+data.status+"; statusText: "+data.statusText;
  868. Validform.util.showmsg.call(curform,msg,settings.tiptype,{obj:curform,type:3,sweep:settings.tipSweep},"byajax");
  869. settings.callback && settings.callback(data);
  870. curform[0].validform_status="normal";
  871. curform[0].validform_ajax=null;
  872. }
  873. }
  874. ajaxsetup=$.extend({},localconfig,ajaxsetup,{dataType:"json"});
  875. curform[0].validform_ajax=$.ajax(ajaxsetup);
  876. }else{
  877. if(!settings.postonce){
  878. curform[0].validform_status="normal";
  879. }
  880. var url=url || config.url;
  881. if(url){
  882. curform.attr("action",url);
  883. }
  884. return settings.callback && settings.callback(curform);
  885. }
  886. }
  887. return false;
  888. },
  889. resetForm:function(){
  890. var brothers=this;
  891. brothers.each(function(){
  892. this.reset();
  893. this.validform_status="normal";
  894. });
  895. brothers.find(".Validform_right").text("");
  896. brothers.find(".passwordStrength").children().removeClass("bgStrength");
  897. brothers.find(".Validform_checktip").removeClass("Validform_wrong Validform_right Validform_loading");
  898. brothers.find(".Validform_error").removeClass("Validform_error");
  899. brothers.find("[datatype]").removeData("cked").removeData("dataIgnore").each(function(){
  900. this.validform_lastval=null;
  901. });
  902. brothers.eq(0).find("input:first").focus();
  903. },
  904. abort:function(){
  905. if(this.validform_ajax){
  906. this.validform_ajax.abort();
  907. }
  908. }
  909. }
  910. $.Datatype=Validform.util.dataType;
  911. Validform.prototype={
  912. dataType:Validform.util.dataType,
  913. eq:function(n){
  914. var obj=this;
  915. if(n>=obj.forms.length){
  916. return null;
  917. }
  918. if(!(n in obj.objects)){
  919. obj.objects[n]=new Validform($(obj.forms[n]).get(),obj.settings,true);
  920. }
  921. return obj.objects[n];
  922. },
  923. resetStatus:function(){
  924. var obj=this;
  925. $(obj.forms).each(function(){
  926. this.validform_status="normal";
  927. });
  928. return this;
  929. },
  930. setStatus:function(status){
  931. var obj=this;
  932. $(obj.forms).each(function(){
  933. this.validform_status=status || "posting";
  934. });
  935. return this;
  936. },
  937. getStatus:function(){
  938. var obj=this;
  939. var status=$(obj.forms)[0].validform_status;
  940. return status;
  941. },
  942. ignore:function(selector){
  943. var obj=this;
  944. var selector=selector || "[datatype]"
  945. $(obj.forms).find(selector).each(function(){
  946. $(this).data("dataIgnore","dataIgnore").removeClass("Validform_error");
  947. });
  948. return this;
  949. },
  950. unignore:function(selector){
  951. var obj=this;
  952. var selector=selector || "[datatype]"
  953. $(obj.forms).find(selector).each(function(){
  954. $(this).removeData("dataIgnore");
  955. });
  956. return this;
  957. },
  958. addRule:function(rule){
  959. /*
  960. rule => [{
  961. ele:"#id",
  962. datatype:"*",
  963. errormsg:"出错提示文字!",
  964. nullmsg:"为空时的提示文字!",
  965. tip:"默认显示的提示文字",
  966. altercss:"gray",
  967. ignore:"ignore",
  968. ajaxurl:"valid.php",
  969. recheck:"password",
  970. plugin:"passwordStrength"
  971. },{},{},...]
  972. */
  973. var obj=this;
  974. var rule=rule || [];
  975. for(var index=0; index<rule.length; index++){
  976. var o=$(obj.forms).find(rule[index].ele);
  977. for(var attr in rule[index]){
  978. attr !=="ele" && o.attr(attr,rule[index][attr]);
  979. }
  980. }
  981. $(obj.forms).each(function(){
  982. var $this=$(this);
  983. Validform.util.enhance.call($this,obj.settings.tiptype,obj.settings.usePlugin,obj.settings.tipSweep,"addRule");
  984. });
  985. return this;
  986. },
  987. ajaxPost:function(flag,sync,url){
  988. var obj=this;
  989. //创建pop box;
  990. if( obj.settings.tiptype==1 || obj.settings.tiptype==2 || obj.settings.tiptype==3 ){
  991. creatMsgbox();
  992. }
  993. Validform.util.submitForm.call($(obj.forms[0]),obj.settings,flag,url,"ajaxPost",sync);
  994. return this;
  995. },
  996. submitForm:function(flag,url){
  997. /*flag===true时不做验证直接提交*/
  998. var obj=this;
  999. //让该对象的第一个表单提交;
  1000. var subflag=Validform.util.submitForm.call($(obj.forms[0]),obj.settings,flag,url);
  1001. subflag === undef && (subflag=true);
  1002. if(subflag===true){
  1003. obj.forms[0].submit();
  1004. }
  1005. return this;
  1006. },
  1007. resetForm:function(){
  1008. var obj=this;
  1009. Validform.util.resetForm.call($(obj.forms));
  1010. return this;
  1011. },
  1012. abort:function(){
  1013. var obj=this;
  1014. $(obj.forms).each(function(){
  1015. Validform.util.abort.call(this);
  1016. });
  1017. return this;
  1018. },
  1019. check:function(bool,selector){
  1020. /*
  1021. bool:传入true,只检测不显示提示信息;
  1022. */
  1023. var selector=selector || "[datatype]",
  1024. obj=this,
  1025. curform=$(obj.forms),
  1026. flag=true;
  1027. curform.find(selector).each(function(){
  1028. Validform.util.check.call(this,curform,obj,"",bool) || (flag=false);
  1029. });
  1030. return flag;
  1031. },
  1032. config:function(setup){
  1033. /*
  1034. config={
  1035. url:"ajaxpost.php",//指定了url后,数据会提交到这个地址;
  1036. ajaxurl:{
  1037. timeout:1000,
  1038. ...
  1039. },
  1040. ajaxpost:{
  1041. timeout:1000,
  1042. ...
  1043. }
  1044. }
  1045. */
  1046. var obj=this;
  1047. setup=setup || {};
  1048. $(obj.forms).each(function(){
  1049. this.validform_config=$.extend(true,this.validform_config,setup);
  1050. });
  1051. return this;
  1052. }
  1053. }
  1054. $.fn.Validform=function(settings){
  1055. return new Validform(this,settings);
  1056. };
  1057. function setCenter(obj,time){
  1058. var left=($(window).width()-obj.outerWidth())/2,
  1059. top=($(window).height()-obj.outerHeight())/2,
  1060. top=(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)+(top>0?top:0);
  1061. obj.css({
  1062. left:left
  1063. }).animate({
  1064. top : top
  1065. },{ duration:time , queue:false });
  1066. }
  1067. function creatMsgbox(){
  1068. if($("#Validform_msg").length!==0){return false;}
  1069. //update-begin--Author:xuelin Date:20170512 for:TASK #1947 【样式优化】默认采用validform的提示框信息,美化下-------------------
  1070. msgobj=$('<div id="Validform_msg"><div class="Validform_title">'+tipmsg.tit+'<a class="Validform_close" href="javascript:void(0);">&chi;</a></div><div class="Validform_info"></div><div class="iframe"><iframe frameborder="0" scrolling="no" height="100%" width="100%"></iframe></div><div class="Validform_btn"><a class="Validform_btn_a">O&nbsp;K</a></div></div>').appendTo("body");//提示信息框;
  1071. msgobj.find("a.Validform_btn_a").click(function() {
  1072. msgobj.hide();
  1073. msghidden = true;
  1074. if (errorobj) {
  1075. errorobj.focus().addClass("Validform_error");
  1076. }
  1077. return false;
  1078. }).focus(function() {
  1079. this.blur();
  1080. });
  1081. //update-end--Author:xuelin Date:20170512 for:TASK #1947 【样式优化】默认采用validform的提示框信息,美化下--------------------
  1082. msgobj.find("a.Validform_close").click(function(){
  1083. msgobj.hide();
  1084. msghidden=true;
  1085. if(errorobj){
  1086. errorobj.focus().addClass("Validform_error");
  1087. }
  1088. return false;
  1089. }).focus(function(){this.blur();});
  1090. $(window).bind("scroll resize",function(){
  1091. !msghidden && setCenter(msgobj,400);
  1092. });
  1093. };
  1094. //公用方法显示&关闭信息提示框;
  1095. $.Showmsg=function(msg){
  1096. creatMsgbox();
  1097. Validform.util.showmsg.call(win,msg,1,{});
  1098. };
  1099. $.Hidemsg=function(){
  1100. msgobj.hide();
  1101. msghidden=true;
  1102. };
  1103. })(jQuery,window);