Validform_v5.3.1-dev.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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. "m":/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/,
  175. "e":/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
  176. "url":/^(\w+:\/\/)?\w+(\.\w+)+.*$/
  177. },
  178. toString:Object.prototype.toString,
  179. isEmpty:function(val){
  180. return val==="" || val===$.trim(this.attr("tip"));
  181. },
  182. getValue:function(obj){
  183. var inputval,
  184. curform=this;
  185. if(obj.is(":radio")){
  186. inputval=curform.find(":radio[name='"+obj.attr("name")+"']:checked").val();
  187. inputval= inputval===undef ? "" : inputval;
  188. }else if(obj.is(":checkbox")){
  189. inputval="";
  190. curform.find(":checkbox[name='"+obj.attr("name")+"']:checked").each(function(){
  191. inputval +=$(this).val()+',';
  192. })
  193. inputval= inputval===undef ? "" : inputval;
  194. }else{
  195. inputval=obj.val();
  196. }
  197. inputval=$.trim(inputval);
  198. return Validform.util.isEmpty.call(obj,inputval) ? "" : inputval;
  199. },
  200. enhance:function(tiptype,usePlugin,tipSweep,addRule){
  201. var curform=this;
  202. //页面上不存在提示信息的标签时,自动创建;
  203. curform.find("[datatype]").each(function(){
  204. if(tiptype==2){
  205. if($(this).parent().next().find(".Validform_checktip").length==0){
  206. $(this).parent().next().append("<span class='Validform_checktip' />");
  207. }
  208. }else if(tiptype==3 || tiptype==4){
  209. if($(this).siblings(".Validform_checktip").length==0){
  210. $(this).parent().append("<span class='Validform_checktip' />");
  211. }
  212. }
  213. })
  214. //表单元素值比较时的信息提示增强;
  215. curform.find("input[recheck]").each(function(){
  216. //已经绑定事件时跳过;
  217. if(this.validform_inited=="inited"){return true;}
  218. this.validform_inited="inited";
  219. var _this=$(this);
  220. var recheckinput=curform.find("input[name='"+$(this).attr("recheck")+"']");
  221. recheckinput.bind("keyup",function(){
  222. if(recheckinput.val()==_this.val() && recheckinput.val() != ""){
  223. if(recheckinput.attr("tip")){
  224. if(recheckinput.attr("tip") == recheckinput.val()){return false;}
  225. }
  226. _this.trigger("blur");
  227. }
  228. }).bind("blur",function(){
  229. if(recheckinput.val()!=_this.val() && _this.val()!=""){
  230. if(_this.attr("tip")){
  231. if(_this.attr("tip") == _this.val()){return false;}
  232. }
  233. _this.trigger("blur");
  234. }
  235. });
  236. });
  237. //hasDefaultText;
  238. curform.find("[tip]").each(function(){//tip是表单元素的默认提示信息,这是点击清空效果;
  239. //已经绑定事件时跳过;
  240. if(this.validform_inited=="inited"){return true;}
  241. this.validform_inited="inited";
  242. var defaultvalue=$(this).attr("tip");
  243. var altercss=$(this).attr("altercss");
  244. $(this).focus(function(){
  245. if($(this).val()==defaultvalue){
  246. $(this).val('');
  247. if(altercss){$(this).removeClass(altercss);}
  248. }
  249. }).blur(function(){
  250. if($.trim($(this).val())===''){
  251. $(this).val(defaultvalue);
  252. if(altercss){$(this).addClass(altercss);}
  253. }
  254. });
  255. });
  256. //enhance info feedback for checkbox & radio;
  257. curform.find(":checkbox[datatype],:radio[datatype]").each(function(){
  258. //已经绑定事件时跳过;
  259. if(this.validform_inited=="inited"){return true;}
  260. this.validform_inited="inited";
  261. var _this=$(this);
  262. var name=_this.attr("name");
  263. curform.find("[name='"+name+"']").filter(":checkbox,:radio").bind("click",function(){
  264. //避免多个事件绑定时的取值滞后问题;
  265. setTimeout(function(){
  266. _this.trigger("blur");
  267. },0);
  268. });
  269. });
  270. //select multiple;
  271. curform.find("select[datatype][multiple]").bind("click",function(){
  272. var _this=$(this);
  273. setTimeout(function(){
  274. _this.trigger("blur");
  275. },0);
  276. });
  277. //plugins here to start;
  278. Validform.util.usePlugin.call(curform,usePlugin,tiptype,tipSweep,addRule);
  279. },
  280. usePlugin:function(plugin,tiptype,tipSweep,addRule){
  281. /*
  282. plugin:settings.usePlugin;
  283. tiptype:settings.tiptype;
  284. tipSweep:settings.tipSweep;
  285. addRule:是否在addRule时触发;
  286. */
  287. var curform=this,
  288. plugin=plugin || {};
  289. //swfupload;
  290. if(curform.find("input[plugin='swfupload']").length && typeof(swfuploadhandler) != "undefined"){
  291. var custom={
  292. custom_settings:{
  293. form:curform,
  294. showmsg:function(msg,type,obj){
  295. Validform.util.showmsg.call(curform,msg,tiptype,{obj:curform.find("input[plugin='swfupload']"),type:type,sweep:tipSweep});
  296. }
  297. }
  298. };
  299. custom=$.extend(true,{},plugin.swfupload,custom);
  300. curform.find("input[plugin='swfupload']").each(function(n){
  301. if(this.validform_inited=="inited"){return true;}
  302. this.validform_inited="inited";
  303. $(this).val("");
  304. swfuploadhandler.init(custom,n);
  305. });
  306. }
  307. //datepicker;
  308. if(curform.find("input[plugin='datepicker']").length && $.fn.datePicker){
  309. plugin.datepicker=plugin.datepicker || {};
  310. if(plugin.datepicker.format){
  311. Date.format=plugin.datepicker.format;
  312. delete plugin.datepicker.format;
  313. }
  314. if(plugin.datepicker.firstDayOfWeek){
  315. Date.firstDayOfWeek=plugin.datepicker.firstDayOfWeek;
  316. delete plugin.datepicker.firstDayOfWeek;
  317. }
  318. curform.find("input[plugin='datepicker']").each(function(n){
  319. if(this.validform_inited=="inited"){return true;}
  320. this.validform_inited="inited";
  321. plugin.datepicker.callback && $(this).bind("dateSelected",function(){
  322. var d=new Date( $.event._dpCache[this._dpId].getSelected()[0] ).asString(Date.format);
  323. plugin.datepicker.callback(d,this);
  324. });
  325. $(this).datePicker(plugin.datepicker);
  326. });
  327. }
  328. //passwordstrength;
  329. if(curform.find("input[plugin*='passwordStrength']").length && $.fn.passwordStrength){
  330. plugin.passwordstrength=plugin.passwordstrength || {};
  331. plugin.passwordstrength.showmsg=function(obj,msg,type){
  332. Validform.util.showmsg.call(curform,msg,tiptype,{obj:obj,type:type,sweep:tipSweep});
  333. };
  334. curform.find("input[plugin='passwordStrength']").each(function(n){
  335. if(this.validform_inited=="inited"){return true;}
  336. this.validform_inited="inited";
  337. $(this).passwordStrength(plugin.passwordstrength);
  338. });
  339. }
  340. //jqtransform;
  341. if(addRule!="addRule" && plugin.jqtransform && $.fn.jqTransSelect){
  342. var jqTransformHideSelect = function(oTarget){
  343. var ulVisible = $('.jqTransformSelectWrapper ul:visible');
  344. ulVisible.each(function(){
  345. var oSelect = $(this).parents(".jqTransformSelectWrapper:first").find("select").get(0);
  346. //do not hide if click on the label object associated to the select
  347. if( !(oTarget && oSelect.oLabel && oSelect.oLabel.get(0) == oTarget.get(0)) ){$(this).hide();}
  348. });
  349. };
  350. /* Check for an external click */
  351. var jqTransformCheckExternalClick = function(event) {
  352. if ($(event.target).parents('.jqTransformSelectWrapper').length === 0) { jqTransformHideSelect($(event.target)); }
  353. };
  354. var jqTransformAddDocumentListener = function (){
  355. $(document).mousedown(jqTransformCheckExternalClick);
  356. };
  357. if(plugin.jqtransform.selector){
  358. curform.find(plugin.jqtransform.selector).filter('input:submit, input:reset, input[type="button"]').jqTransInputButton();
  359. curform.find(plugin.jqtransform.selector).filter('input:text, input:password').jqTransInputText();
  360. curform.find(plugin.jqtransform.selector).filter('input:checkbox').jqTransCheckBox();
  361. curform.find(plugin.jqtransform.selector).filter('input:radio').jqTransRadio();
  362. curform.find(plugin.jqtransform.selector).filter('textarea').jqTransTextarea();
  363. if(curform.find(plugin.jqtransform.selector).filter("select").length > 0 ){
  364. curform.find(plugin.jqtransform.selector).filter("select").jqTransSelect();
  365. jqTransformAddDocumentListener();
  366. }
  367. }else{
  368. curform.jqTransform();
  369. }
  370. curform.find(".jqTransformSelectWrapper").find("li a").click(function(){
  371. $(this).parents(".jqTransformSelectWrapper").find("select").trigger("blur");
  372. });
  373. }
  374. },
  375. getNullmsg:function(curform){
  376. var obj=this;
  377. var reg=/[\u4E00-\u9FA5\uf900-\ufa2da-zA-Z\s]+/g;
  378. var nullmsg;
  379. var label=curform[0].validform_label || ".Validform_label";
  380. label=obj.siblings(label).text() || obj.siblings().find(label).text() || obj.parent().siblings(label).text() || obj.parent().siblings().find(label).text();
  381. label=label.match(reg) || [""];
  382. reg=/\{(.+)\|(.+)\}/;
  383. nullmsg=curform.data("tipmsg").s || tipmsg.s;
  384. label=label[0].replace(/\s(?![a-zA-Z])/g,"");
  385. if(label != ""){
  386. nullmsg=nullmsg.replace(/\{0\|(.+)\}/,label);
  387. if(obj.attr("recheck")){
  388. nullmsg=nullmsg.replace(/\{(.+)\}/,"");
  389. obj.attr("nullmsg",nullmsg);
  390. return nullmsg;
  391. }
  392. }else{
  393. nullmsg=obj.is(":checkbox,:radio,select") ? nullmsg.replace(/\{0\|(.+)\}/,"") : nullmsg.replace(/\{0\|(.+)\}/,"$1");
  394. }
  395. nullmsg=obj.is(":checkbox,:radio,select") ? nullmsg.replace(reg,"$2") : nullmsg.replace(reg,"$1");
  396. obj.attr("nullmsg",nullmsg);
  397. return nullmsg;
  398. },
  399. getErrormsg:function(curform,datatype,recheck){
  400. var obj=this;
  401. var regxp=/^(.+?)((\d+)-(\d+))?$/,
  402. regxp2=/^(.+?)(\d+)-(\d+)$/,
  403. regxp3=/(.*?)\d+(.+?)\d+(.*)/,
  404. mac=datatype.match(regxp),
  405. temp,str;
  406. //如果是值不一样而报错;
  407. if(recheck=="recheck"){
  408. str=curform.data("tipmsg").reck || tipmsg.reck;
  409. return str;
  410. }
  411. //如果原来就有,直接显示该项的提示信息;
  412. if(mac[0] in tipmsg.w){
  413. return curform.data("tipmsg").w[mac[0]] || tipmsg.w[mac[0]];
  414. }
  415. //没有的话在提示对象里查找相似;
  416. for(var name in tipmsg.w){
  417. if(name.indexOf(mac[1])!=-1 && regxp2.test(name)){
  418. str=(curform.data("tipmsg").w[name] || tipmsg.w[name]).replace(regxp3,"$1"+mac[3]+"$2"+mac[4]+"$3");
  419. tipmsg.w[mac[0]]=str;
  420. return str;
  421. }
  422. }
  423. return curform.data("tipmsg").def || tipmsg.def;
  424. },
  425. _regcheck:function(datatype,gets,obj,curform){
  426. var curform=curform,
  427. info=null,
  428. passed=false,
  429. reg=/\/.+\//g,
  430. regex=/^(.+?)(\d+)-(\d+)$/,
  431. type=3;//default set to wrong type, 2,3,4;
  432. //datatype有三种情况:正则,函数和直接绑定的正则;
  433. //直接是正则;
  434. if(reg.test(datatype)){
  435. var regstr=datatype.match(reg)[0].slice(1,-1);
  436. var param=datatype.replace(reg,"");
  437. var rexp=RegExp(regstr,param);
  438. passed=rexp.test(gets);
  439. //function;
  440. }else if(Validform.util.toString.call(Validform.util.dataType[datatype])=="[object Function]"){
  441. passed=Validform.util.dataType[datatype](gets,obj,curform,Validform.util.dataType);
  442. if(passed === true || passed===undef){
  443. passed = true;
  444. }else{
  445. info= passed;
  446. passed=false;
  447. }
  448. //自定义正则;
  449. }else{
  450. //自动扩展datatype;
  451. if(!(datatype in Validform.util.dataType)){
  452. var mac=datatype.match(regex),
  453. temp;
  454. if(!mac){
  455. passed=false;
  456. info=curform.data("tipmsg").undef||tipmsg.undef;
  457. }else{
  458. for(var name in Validform.util.dataType){
  459. temp=name.match(regex);
  460. if(!temp){continue;}
  461. if(mac[1]===temp[1]){
  462. var str=Validform.util.dataType[name].toString(),
  463. param=str.match(/\/[mgi]*/g)[1].replace("\/",""),
  464. regxp=new RegExp("\\{"+temp[2]+","+temp[3]+"\\}","g");
  465. str=str.replace(/\/[mgi]*/g,"\/").replace(regxp,"{"+mac[2]+","+mac[3]+"}").replace(/^\//,"").replace(/\/$/,"");
  466. Validform.util.dataType[datatype]=new RegExp(str,param);
  467. break;
  468. }
  469. }
  470. }
  471. }
  472. if(Validform.util.toString.call(Validform.util.dataType[datatype])=="[object RegExp]"){
  473. passed=Validform.util.dataType[datatype].test(gets);
  474. }
  475. }
  476. if(passed){
  477. type=2;
  478. info=obj.attr("sucmsg") || curform.data("tipmsg").r||tipmsg.r;
  479. //规则验证通过后,还需要对绑定recheck的对象进行值比较;
  480. if(obj.attr("recheck")){
  481. var theother=curform.find("input[name='"+obj.attr("recheck")+"']:first");
  482. if(gets!=theother.val()){
  483. passed=false;
  484. type=3;
  485. info=obj.attr("errormsg") || Validform.util.getErrormsg.call(obj,curform,datatype,"recheck");
  486. }
  487. }
  488. }else{
  489. info=info || obj.attr("errormsg") || Validform.util.getErrormsg.call(obj,curform,datatype);
  490. //验证不通过且为空时;
  491. if(Validform.util.isEmpty.call(obj,gets)){
  492. info=obj.attr("nullmsg") || Validform.util.getNullmsg.call(obj,curform);
  493. }
  494. }
  495. return{
  496. passed:passed,
  497. type:type,
  498. info:info
  499. };
  500. },
  501. regcheck:function(datatype,gets,obj){
  502. /*
  503. datatype:datatype;
  504. gets:inputvalue;
  505. obj:input object;
  506. */
  507. var curform=this,
  508. info=null,
  509. passed=false,
  510. type=3;//default set to wrong type, 2,3,4;
  511. //ignore;
  512. if(obj.attr("ignore")==="ignore" && Validform.util.isEmpty.call(obj,gets)){
  513. if(obj.data("cked")){
  514. info="";
  515. }
  516. return {
  517. passed:true,
  518. type:4,
  519. info:info
  520. };
  521. }
  522. obj.data("cked","cked");//do nothing if is the first time validation triggered;
  523. var dtype=Validform.util.parseDatatype(datatype);
  524. var res;
  525. for(var eithor=0; eithor<dtype.length; eithor++){
  526. for(var dtp=0; dtp<dtype[eithor].length; dtp++){
  527. res=Validform.util._regcheck(dtype[eithor][dtp],gets,obj,curform);
  528. if(!res.passed){
  529. break;
  530. }
  531. }
  532. if(res.passed){
  533. break;
  534. }
  535. }
  536. return res;
  537. },
  538. parseDatatype:function(datatype){
  539. /*
  540. 字符串里面只能含有一个正则表达式;
  541. Datatype名称必须是字母,数字、下划线或*号组成;
  542. datatype="/regexp/|phone|tel,s,e|f,e";
  543. ==>[["/regexp/"],["phone"],["tel","s","e"],["f","e"]];
  544. */
  545. var reg=/\/.+?\/[mgi]*(?=(,|$|\||\s))|[\w\*-]+/g,
  546. dtype=datatype.match(reg),
  547. sepor=datatype.replace(reg,"").replace(/\s*/g,"").split(""),
  548. arr=[],
  549. m=0;
  550. arr[0]=[];
  551. arr[0].push(dtype[0]);
  552. for(var n=0;n<sepor.length;n++){
  553. if(sepor[n]=="|"){
  554. m++;
  555. arr[m]=[];
  556. }
  557. arr[m].push(dtype[n+1]);
  558. }
  559. return arr;
  560. },
  561. showmsg:function(msg,type,o,triggered){
  562. /*
  563. msg:提示文字;
  564. type:提示信息显示方式;
  565. o:{obj:当前对象, type:1=>正在检测 | 2=>通过, sweep:true | false},
  566. triggered:在blur或提交表单触发的验证中,有些情况不需要显示提示文字,如自定义弹出提示框的显示方式,不需要每次blur时就马上弹出提示;
  567. tiptype:1\2\3时都有坑能会弹出自定义提示框
  568. tiptype:1时在triggered bycheck时不弹框
  569. tiptype:2\3时在ajax时弹框
  570. tipSweep为true时在triggered bycheck时不触发showmsg,但ajax出错的情况下要提示
  571. */
  572. //如果msg为undefined,那么就没必要执行后面的操作,ignore有可能会出现这情况;
  573. if(msg==undef){return;}
  574. //tipSweep为true,且当前不是处于错误状态时,blur事件不触发信息显示;
  575. if(triggered=="bycheck" && o.sweep && (o.obj && !o.obj.is(".Validform_error") || typeof type == "function")){return;}
  576. $.extend(o,{curform:this});
  577. if(typeof type == "function"){
  578. type(msg,o,Validform.util.cssctl);
  579. return;
  580. }
  581. if(type==1 || triggered=="byajax" && type!=4){
  582. msgobj.find(".Validform_info").html(msg);
  583. }
  584. //tiptypt=1时,blur触发showmsg,验证是否通过都不弹框,提交表单触发的话,只要验证出错,就弹框;
  585. if(type==1 && triggered!="bycheck" && o.type!=2 || triggered=="byajax" && type!=4){
  586. msghidden=false;
  587. msgobj.find(".iframe").css("height",msgobj.outerHeight());
  588. msgobj.show();
  589. setCenter(msgobj,100);
  590. }
  591. if(type==2 && o.obj){
  592. o.obj.parent().next().find(".Validform_checktip").html(msg);
  593. Validform.util.cssctl(o.obj.parent().next().find(".Validform_checktip"),o.type);
  594. }
  595. if((type==3 || type==4) && o.obj){
  596. o.obj.siblings(".Validform_checktip").html(msg);
  597. Validform.util.cssctl(o.obj.siblings(".Validform_checktip"),o.type);
  598. }
  599. },
  600. cssctl:function(obj,status){
  601. switch(status){
  602. case 1:
  603. obj.removeClass("Validform_right Validform_wrong").addClass("Validform_checktip Validform_loading");//checking;
  604. break;
  605. case 2:
  606. obj.removeClass("Validform_wrong Validform_loading").addClass("Validform_checktip Validform_right");//passed;
  607. break;
  608. case 4:
  609. obj.removeClass("Validform_right Validform_wrong Validform_loading").addClass("Validform_checktip");//for ignore;
  610. break;
  611. default:
  612. obj.removeClass("Validform_right Validform_loading").addClass("Validform_checktip Validform_wrong");//wrong;
  613. }
  614. },
  615. ajax_check:function(tableName,fieldName,fieldVlaue,param){
  616. //获取编辑页面的数据主键
  617. var obid = null;
  618. if(param!=null){
  619. var obid_id = param[0];
  620. obid = $("#"+obid_id+"").val();
  621. }else{
  622. obid = $("input[name='obid']").val();
  623. }
  624. $.ajaxSetup({ async: false});//同步ajax
  625. var check_flag="";
  626. $.ajax({
  627. url : 'duplicateCheckAction.do?doDuplicateCheck',
  628. async:false,
  629. data : {
  630. tableName : tableName,
  631. fieldName : fieldName,
  632. fieldVlaue: fieldVlaue,
  633. rowObid : obid
  634. },
  635. dataType : 'json',
  636. success : function(response) {
  637. check_flag = response.msg+"+"+response.success;
  638. //$.messager.alert('提示', response.msg, 'error');
  639. }
  640. });
  641. $.ajaxSetup({ async: false});//同步ajax
  642. return check_flag;
  643. },
  644. check:function(curform,brothers,subpost,bool){
  645. /*
  646. 检测单个表单元素;
  647. 验证通过返回true,否则返回false、实时验证返回值为ajax;
  648. bool,传入true则只检测不显示提示信息;
  649. */
  650. var settings=brothers.settings;
  651. var subpost=subpost || "";
  652. var inputval=Validform.util.getValue.call(curform,$(this));
  653. //隐藏或绑定dataIgnore的表单对象不做验证;
  654. if(settings.ignoreHidden && $(this).is(":hidden") || $(this).data("dataIgnore")==="dataIgnore"){
  655. return true;
  656. }
  657. //dragonfly=true时,没有绑定ignore,值为空不做验证,但验证不通过;
  658. if(settings.dragonfly && !$(this).data("cked") && Validform.util.isEmpty.call($(this),inputval) && $(this).attr("ignore")!="ignore"){
  659. return false;
  660. }
  661. var flag=Validform.util.regcheck.call(curform,$(this).attr("datatype"),inputval,$(this));
  662. //值没变化不做检测,这时要考虑recheck情况;
  663. //不是在提交表单时触发的ajax验证;
  664. if(inputval==this.validform_lastval && !$(this).attr("recheck") && subpost==""){
  665. return flag.passed ? true : false;
  666. }
  667. this.validform_lastval=inputval;//存储当前值;
  668. var _this;
  669. errorobj=_this=$(this);
  670. if(!flag.passed){
  671. //取消正在进行的ajax验证;
  672. Validform.util.abort.call(_this[0]);
  673. if(!bool){
  674. //传入"bycheck",指示当前是check方法里调用的,当tiptype=1时,blur事件不让触发错误信息显示;
  675. Validform.util.showmsg.call(curform,flag.info,settings.tiptype,{obj:$(this),type:flag.type,sweep:settings.tipSweep},"bycheck");
  676. !settings.tipSweep && _this.addClass("Validform_error");
  677. }
  678. return false;
  679. }
  680. //字段重复校验
  681. var validType=$(this).attr("validType");
  682. var tipType=settings.tiptype;
  683. if(tipType==null||tipType!=1){
  684. if(validType!=null){
  685. var params=validType.split(",");
  686. var ajaxResultValue=Validform.util.ajax_check(params[0],params[1],$(this).val(),$("input[name='"+params[2]+"']").val());
  687. var resultParams= new Array(); //定义一数组
  688. resultParams=ajaxResultValue.split("+"); //字符分割
  689. if(resultParams[1]=="false"){
  690. $(_this).next().html(resultParams[0]);
  691. $(_this).next().addClass("Validform_wrong");
  692. _this.addClass("Validform_error");
  693. return false;
  694. }
  695. }
  696. }
  697. //共通方法
  698. //验证通过的话,如果绑定有ajaxurl,要执行ajax检测;
  699. //当ignore="ignore"时,为空值可以通过验证,这时不需要ajax检测;
  700. var ajaxurl=$(this).attr("ajaxurl");
  701. if(ajaxurl && !Validform.util.isEmpty.call($(this),inputval) && !bool){
  702. var inputobj=$(this);
  703. //当提交表单时,表单中的某项已经在执行ajax检测,这时需要让该项ajax结束后继续提交表单;
  704. if(subpost=="postform"){
  705. inputobj[0].validform_subpost="postform";
  706. }else{
  707. inputobj[0].validform_subpost="";
  708. }
  709. if(inputobj[0].validform_valid==="posting" && inputval==inputobj[0].validform_ckvalue){return "ajax";}
  710. inputobj[0].validform_valid="posting";
  711. inputobj[0].validform_ckvalue=inputval;
  712. Validform.util.showmsg.call(curform,brothers.tipmsg.c||tipmsg.c,settings.tiptype,{obj:inputobj,type:1,sweep:settings.tipSweep},"bycheck");
  713. Validform.util.abort.call(_this[0]);
  714. var ajaxsetup=curform[0].validform_config || {};
  715. ajaxsetup=ajaxsetup.ajaxurl || {};
  716. var localconfig={
  717. type: "POST",
  718. cache:false,
  719. url: ajaxurl,
  720. data: "param="+encodeURIComponent(inputval)+"&name="+encodeURIComponent($(this).attr("name")),
  721. success: function(data){
  722. if($.trim(data.status)==="y"){
  723. inputobj[0].validform_valid="true";
  724. data.info && inputobj.attr("sucmsg",data.info);
  725. Validform.util.showmsg.call(curform,inputobj.attr("sucmsg") || brothers.tipmsg.r||tipmsg.r,settings.tiptype,{obj:inputobj,type:2,sweep:settings.tipSweep},"bycheck");
  726. _this.removeClass("Validform_error");
  727. errorobj=null;
  728. if(inputobj[0].validform_subpost=="postform"){
  729. curform.trigger("submit");
  730. }
  731. }else{
  732. inputobj[0].validform_valid=data.info;
  733. Validform.util.showmsg.call(curform,data.info,settings.tiptype,{obj:inputobj,type:3,sweep:settings.tipSweep});
  734. _this.addClass("Validform_error");
  735. }
  736. _this[0].validform_ajax=null;
  737. },
  738. error: function(data){
  739. if(data.status=="200"){
  740. if(data.responseText=="y"){
  741. ajaxsetup.success({"status":"y"});
  742. }else{
  743. ajaxsetup.success({"status":"n","info":data.responseText});
  744. }
  745. return false;
  746. }
  747. //正在检测时,要检测的数据发生改变,这时要终止当前的ajax。不是这种情况引起的ajax错误,那么显示相关错误信息;
  748. if(data.statusText!=="abort"){
  749. var msg="status: "+data.status+"; statusText: "+data.statusText;
  750. Validform.util.showmsg.call(curform,msg,settings.tiptype,{obj:inputobj,type:3,sweep:settings.tipSweep});
  751. _this.addClass("Validform_error");
  752. }
  753. inputobj[0].validform_valid=data.statusText;
  754. _this[0].validform_ajax=null;
  755. //localconfig.error返回true表示还需要执行temp_err;
  756. return true;
  757. }
  758. }
  759. if(ajaxsetup.success){
  760. var temp_suc=ajaxsetup.success;
  761. ajaxsetup.success=function(data){
  762. localconfig.success(data);
  763. temp_suc(data,inputobj);
  764. }
  765. }
  766. if(ajaxsetup.error){
  767. var temp_err=ajaxsetup.error;
  768. ajaxsetup.error=function(data){
  769. //localconfig.error返回false表示不需要执行temp_err;
  770. localconfig.error(data) && temp_err(data,inputobj);
  771. }
  772. }
  773. ajaxsetup=$.extend({},localconfig,ajaxsetup,{dataType:"json"});
  774. _this[0].validform_ajax=$.ajax(ajaxsetup);
  775. return "ajax";
  776. }else if(ajaxurl && Validform.util.isEmpty.call($(this),inputval)){
  777. Validform.util.abort.call(_this[0]);
  778. _this[0].validform_valid="true";
  779. }
  780. if(!bool){
  781. Validform.util.showmsg.call(curform,flag.info,settings.tiptype,{obj:$(this),type:flag.type,sweep:settings.tipSweep},"bycheck");
  782. _this.removeClass("Validform_error");
  783. }
  784. errorobj=null;
  785. return true;
  786. },
  787. submitForm:function(settings,flg,url,ajaxPost,sync){
  788. /*
  789. flg===true时跳过验证直接提交;
  790. ajaxPost==="ajaxPost"指示当前表单以ajax方式提交;
  791. */
  792. var curform=this;
  793. //表单正在提交时点击提交按钮不做反应;
  794. if(curform[0].validform_status==="posting"){return false;}
  795. //要求只能提交一次时;
  796. if(settings.postonce && curform[0].validform_status==="posted"){return false;}
  797. var beforeCheck=settings.beforeCheck && settings.beforeCheck(curform);
  798. if(beforeCheck===false){return false;}
  799. var flag=true,
  800. inflag;
  801. //tipType==1弹出层提示信息。
  802. var tipType=settings.tiptype
  803. //字段重复校验
  804. if(tipType==null||tipType!=1){
  805. curform.find("[validType]").each(function(obj){
  806. var validType=$(this).attr("validType");
  807. if(validType!=null&&$(this).val()!=""){
  808. var params=validType.split(",");
  809. var ajaxResultValue=Validform.util.ajax_check(params[0],params[1],$(this).val(),$("input[name='"+params[2]+"']").val());
  810. var resultParams= new Array(); //定义一数组
  811. resultParams=ajaxResultValue.split("+"); //字符分割
  812. if(resultParams[1]=="false"){
  813. $(_this).next().html(resultParams[0]);
  814. $(_this).next().addClass("Validform_wrong");
  815. _this.addClass("Validform_error");
  816. flag=false;
  817. return false;
  818. }
  819. else{
  820. }
  821. }
  822. });
  823. }
  824. if(tipType!=null&&tipType==1){
  825. curform.find("[validType]").each(function(obj){
  826. var validType=$(this).attr("validType");
  827. if(validType!=null&&$(this).val()!=""){
  828. var params=validType.split(",");
  829. var ajaxResultValue=Validform.util.ajax_check(params[0],params[1],$(this).val(),$("input[name='"+params[2]+"']").val());
  830. var resultParams= new Array(); //定义一数组
  831. resultParams=ajaxResultValue.split("+"); //字符分割
  832. if(resultParams[1]=="false"){
  833. //$(th).next().html(resultParams[0]);
  834. $(this).addClass("Validform_error");
  835. msgobj.find(".Validform_info").html(resultParams[0]);
  836. msghidden=false;
  837. msgobj.find(".iframe").css("height",msgobj.outerHeight());
  838. msgobj.show();
  839. setCenter(msgobj,100);
  840. _this.addClass("Validform_error");
  841. flag=false;
  842. return false;
  843. }
  844. else{
  845. }
  846. }
  847. });
  848. }
  849. curform.find("[datatype]").each(function(){
  850. //跳过验证;
  851. if(flg){
  852. return false;
  853. }
  854. //隐藏或绑定dataIgnore的表单对象不做验证;
  855. if(settings.ignoreHidden && $(this).is(":hidden") || $(this).data("dataIgnore")==="dataIgnore"){
  856. return true;
  857. }
  858. var inputval=Validform.util.getValue.call(curform,$(this)),
  859. _this;
  860. errorobj=_this=$(this);
  861. inflag=Validform.util.regcheck.call(curform,$(this).attr("datatype"),inputval,$(this));
  862. if(!inflag.passed){
  863. Validform.util.showmsg.call(curform,inflag.info,settings.tiptype,{obj:$(this),type:inflag.type,sweep:settings.tipSweep});
  864. _this.addClass("Validform_error");
  865. if(!settings.showAllError){
  866. _this.focus();
  867. flag=false;
  868. return false;
  869. }
  870. flag && (flag=false);
  871. return true;
  872. }
  873. //当ignore="ignore"时,为空值可以通过验证,这时不需要ajax检测;
  874. if($(this).attr("ajaxurl") && !Validform.util.isEmpty.call($(this),inputval)){
  875. if(this.validform_valid!=="true"){
  876. var thisobj=$(this);
  877. Validform.util.showmsg.call(curform,curform.data("tipmsg").v||tipmsg.v,settings.tiptype,{obj:thisobj,type:3,sweep:settings.tipSweep});
  878. _this.addClass("Validform_error");
  879. thisobj.trigger("blur",["postform"]);//continue the form post;
  880. if(!settings.showAllError){
  881. flag=false;
  882. return false;
  883. }
  884. flag && (flag=false);
  885. return true;
  886. }
  887. }else if($(this).attr("ajaxurl") && Validform.util.isEmpty.call($(this),inputval)){
  888. Validform.util.abort.call(this);
  889. this.validform_valid="true";
  890. }
  891. Validform.util.showmsg.call(curform,inflag.info,settings.tiptype,{obj:$(this),type:inflag.type,sweep:settings.tipSweep});
  892. _this.removeClass("Validform_error");
  893. errorobj=null;
  894. });
  895. if(settings.showAllError){
  896. curform.find(".Validform_error:first").focus();
  897. }
  898. if(flag){
  899. var beforeSubmit=settings.beforeSubmit && settings.beforeSubmit(curform);
  900. if(beforeSubmit===false){return false;}
  901. curform[0].validform_status="posting";
  902. var config=curform[0].validform_config || {}
  903. if(settings.ajaxPost || ajaxPost==="ajaxPost"){
  904. //获取配置参数;
  905. var ajaxsetup=config.ajaxpost || {};
  906. //有可能需要动态的改变提交地址,所以把action所指定的url层级设为最低;
  907. ajaxsetup.url=url || ajaxsetup.url || config.url || curform.attr("action");
  908. //byajax:ajax时,tiptye为1、2或3需要弹出提示框;
  909. //如果setup里已经自定义了success或error方法,这里不显示提示信息;
  910. 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");
  911. //方法里的优先级要高;
  912. //有undefined情况;
  913. if(sync){
  914. ajaxsetup.async=false;
  915. }else if(sync===false){
  916. ajaxsetup.async=true;
  917. }
  918. if(ajaxsetup.success){
  919. var temp_suc=ajaxsetup.success;
  920. ajaxsetup.success=function(data){
  921. settings.callback && settings.callback(data);
  922. curform[0].validform_ajax=null;
  923. if($.trim(data.status)==="y"){
  924. curform[0].validform_status="posted";
  925. }else{
  926. curform[0].validform_status="normal";
  927. }
  928. temp_suc(data,curform);
  929. }
  930. }
  931. if(ajaxsetup.error){
  932. var temp_err=ajaxsetup.error;
  933. ajaxsetup.error=function(data){
  934. settings.callback && settings.callback(data);
  935. curform[0].validform_status="normal";
  936. curform[0].validform_ajax=null;
  937. temp_err(data,curform);
  938. }
  939. }
  940. var localconfig={
  941. type: "POST",
  942. async:true,
  943. data: curform.serializeArray(),
  944. success: function(data){
  945. if($.trim(data.status)==="y"){
  946. //成功提交;
  947. curform[0].validform_status="posted";
  948. Validform.util.showmsg.call(curform,data.info,settings.tiptype,{obj:curform,type:2,sweep:settings.tipSweep},"byajax");
  949. }else{
  950. //提交出错;
  951. curform[0].validform_status="normal";
  952. Validform.util.showmsg.call(curform,data.info,settings.tiptype,{obj:curform,type:3,sweep:settings.tipSweep},"byajax");
  953. }
  954. settings.callback && settings.callback(data);
  955. curform[0].validform_ajax=null;
  956. },
  957. error: function(data){
  958. var msg="status: "+data.status+"; statusText: "+data.statusText;
  959. Validform.util.showmsg.call(curform,msg,settings.tiptype,{obj:curform,type:3,sweep:settings.tipSweep},"byajax");
  960. settings.callback && settings.callback(data);
  961. curform[0].validform_status="normal";
  962. curform[0].validform_ajax=null;
  963. }
  964. }
  965. ajaxsetup=$.extend({},localconfig,ajaxsetup,{dataType:"json"});
  966. curform[0].validform_ajax=$.ajax(ajaxsetup);
  967. }else{
  968. if(!settings.postonce){
  969. curform[0].validform_status="normal";
  970. }
  971. var url=url || config.url;
  972. if(url){
  973. curform.attr("action",url);
  974. }
  975. return settings.callback && settings.callback(curform);
  976. }
  977. }
  978. return false;
  979. },
  980. resetForm:function(){
  981. var brothers=this;
  982. brothers.each(function(){
  983. this.reset();
  984. this.validform_status="normal";
  985. });
  986. brothers.find(".Validform_right").text("");
  987. brothers.find(".passwordStrength").children().removeClass("bgStrength");
  988. brothers.find(".Validform_checktip").removeClass("Validform_wrong Validform_right Validform_loading");
  989. brothers.find(".Validform_error").removeClass("Validform_error");
  990. brothers.find("[datatype]").removeData("cked").removeData("dataIgnore").each(function(){
  991. this.validform_lastval=null;
  992. });
  993. brothers.eq(0).find("input:first").focus();
  994. },
  995. abort:function(){
  996. if(this.validform_ajax){
  997. this.validform_ajax.abort();
  998. }
  999. }
  1000. }
  1001. $.Datatype=Validform.util.dataType;
  1002. Validform.prototype={
  1003. dataType:Validform.util.dataType,
  1004. eq:function(n){
  1005. var obj=this;
  1006. if(n>=obj.forms.length){
  1007. return null;
  1008. }
  1009. if(!(n in obj.objects)){
  1010. obj.objects[n]=new Validform($(obj.forms[n]).get(),obj.settings,true);
  1011. }
  1012. return obj.objects[n];
  1013. },
  1014. resetStatus:function(){
  1015. var obj=this;
  1016. $(obj.forms).each(function(){
  1017. this.validform_status="normal";
  1018. });
  1019. return this;
  1020. },
  1021. setStatus:function(status){
  1022. var obj=this;
  1023. $(obj.forms).each(function(){
  1024. this.validform_status=status || "posting";
  1025. });
  1026. return this;
  1027. },
  1028. getStatus:function(){
  1029. var obj=this;
  1030. var status=$(obj.forms)[0].validform_status;
  1031. return status;
  1032. },
  1033. ignore:function(selector){
  1034. var obj=this;
  1035. var selector=selector || "[datatype]"
  1036. $(obj.forms).find(selector).each(function(){
  1037. $(this).data("dataIgnore","dataIgnore").removeClass("Validform_error");
  1038. });
  1039. return this;
  1040. },
  1041. unignore:function(selector){
  1042. var obj=this;
  1043. var selector=selector || "[datatype]"
  1044. $(obj.forms).find(selector).each(function(){
  1045. $(this).removeData("dataIgnore");
  1046. });
  1047. return this;
  1048. },
  1049. addRule:function(rule){
  1050. /*
  1051. rule => [{
  1052. ele:"#id",
  1053. datatype:"*",
  1054. errormsg:"出错提示文字!",
  1055. nullmsg:"为空时的提示文字!",
  1056. tip:"默认显示的提示文字",
  1057. altercss:"gray",
  1058. ignore:"ignore",
  1059. ajaxurl:"valid.php",
  1060. recheck:"password",
  1061. plugin:"passwordStrength"
  1062. },{},{},...]
  1063. */
  1064. var obj=this;
  1065. var rule=rule || [];
  1066. for(var index=0; index<rule.length; index++){
  1067. var o=$(obj.forms).find(rule[index].ele);
  1068. for(var attr in rule[index]){
  1069. attr !=="ele" && o.attr(attr,rule[index][attr]);
  1070. }
  1071. }
  1072. $(obj.forms).each(function(){
  1073. var $this=$(this);
  1074. Validform.util.enhance.call($this,obj.settings.tiptype,obj.settings.usePlugin,obj.settings.tipSweep,"addRule");
  1075. });
  1076. return this;
  1077. },
  1078. ajaxPost:function(flag,sync,url){
  1079. var obj=this;
  1080. //创建pop box;
  1081. if( obj.settings.tiptype==1 || obj.settings.tiptype==2 || obj.settings.tiptype==3 ){
  1082. creatMsgbox();
  1083. }
  1084. Validform.util.submitForm.call($(obj.forms[0]),obj.settings,flag,url,"ajaxPost",sync);
  1085. return this;
  1086. },
  1087. submitForm:function(flag,url){
  1088. /*flag===true时不做验证直接提交*/
  1089. var obj=this;
  1090. //让该对象的第一个表单提交;
  1091. var subflag=Validform.util.submitForm.call($(obj.forms[0]),obj.settings,flag,url);
  1092. subflag === undef && (subflag=true);
  1093. if(subflag===true){
  1094. obj.forms[0].submit();
  1095. }
  1096. return this;
  1097. },
  1098. resetForm:function(){
  1099. var obj=this;
  1100. Validform.util.resetForm.call($(obj.forms));
  1101. return this;
  1102. },
  1103. abort:function(){
  1104. var obj=this;
  1105. $(obj.forms).each(function(){
  1106. Validform.util.abort.call(this);
  1107. });
  1108. return this;
  1109. },
  1110. check:function(bool,selector){
  1111. /*
  1112. bool:传入true,只检测不显示提示信息;
  1113. */
  1114. var selector=selector || "[datatype]",
  1115. obj=this,
  1116. curform=$(obj.forms),
  1117. flag=true;
  1118. curform.find(selector).each(function(){
  1119. Validform.util.check.call(this,curform,obj,"",bool) || (flag=false);
  1120. });
  1121. return flag;
  1122. },
  1123. config:function(setup){
  1124. /*
  1125. config={
  1126. url:"ajaxpost.php",//指定了url后,数据会提交到这个地址;
  1127. ajaxurl:{
  1128. timeout:1000,
  1129. ...
  1130. },
  1131. ajaxpost:{
  1132. timeout:1000,
  1133. ...
  1134. }
  1135. }
  1136. */
  1137. var obj=this;
  1138. setup=setup || {};
  1139. $(obj.forms).each(function(){
  1140. this.validform_config=$.extend(true,this.validform_config,setup);
  1141. });
  1142. return this;
  1143. }
  1144. }
  1145. $.fn.Validform=function(settings){
  1146. return new Validform(this,settings);
  1147. };
  1148. function setCenter(obj,time){
  1149. var left=($(window).width()-obj.outerWidth())/2,
  1150. top=($(window).height()-obj.outerHeight())/2,
  1151. top=(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)+(top>0?top:0);
  1152. obj.css({
  1153. left:left
  1154. }).animate({
  1155. top : top
  1156. },{ duration:time , queue:false });
  1157. }
  1158. function creatMsgbox(){
  1159. if($("#Validform_msg").length!==0){return false;}
  1160. //update-begin--Author:xuelin Date:20170512 for:TASK #1947 【样式优化】默认采用validform的提示框信息,美化下-------------------
  1161. 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");//提示信息框;
  1162. msgobj.find("a.Validform_btn_a").click(function() {
  1163. msgobj.hide();
  1164. msghidden = true;
  1165. if (errorobj) {
  1166. errorobj.focus().addClass("Validform_error");
  1167. }
  1168. return false;
  1169. }).focus(function() {
  1170. this.blur();
  1171. });
  1172. //update-end--Author:xuelin Date:20170512 for:TASK #1947 【样式优化】默认采用validform的提示框信息,美化下--------------------
  1173. msgobj.find("a.Validform_close").click(function(){
  1174. msgobj.hide();
  1175. msghidden=true;
  1176. if(errorobj){
  1177. errorobj.focus().addClass("Validform_error");
  1178. }
  1179. return false;
  1180. }).focus(function(){this.blur();});
  1181. $(window).bind("scroll resize",function(){
  1182. !msghidden && setCenter(msgobj,400);
  1183. });
  1184. };
  1185. //公用方法显示&关闭信息提示框;
  1186. $.Showmsg=function(msg){
  1187. creatMsgbox();
  1188. Validform.util.showmsg.call(win,msg,1,{});
  1189. };
  1190. $.Hidemsg=function(){
  1191. msgobj.hide();
  1192. msghidden=true;
  1193. };
  1194. })(jQuery,window);