Validform.js 43 KB

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