/************************************************************************************* 程式功能 : 表單驗證 建置日期 : 2011-06-02 版本 : 1.0 版權所有 : 尚峪資訊科技有限公司 http://www.shang-yu.com.tw -------------------------------------------------------------------------------------- fromsAuth 表單元件驗證參數(fun) checkNull : 驗證空白 checkNum : 驗證數字 checkNumNull : 驗證數字必填 checkEnNum : 驗證英文數字 checkEnNumNull : 驗證英文數字必填 checkPersonID : 驗證身份證字號 checkPersonIDNull : 驗證身份證字號必填 checkEmail : 驗證電子信箱 checkEmailNull : 驗證電子信箱必填 checkItems : 驗證核選方塊及選項方塊 checkSame : 驗證和checkSame欄位的值是否相同 checkSameNull : 驗證和checkSame欄位的值是否相同必填 checkExt : 驗證上傳格式(屬性ext="jpg,xls....") col_check 表單欄位驗證 *************************************************************************************/ (function ($){ jQuery.fn.extend ({ fromsAuth: function(option){ return this.each(function(){ var settings = { type: "AJAX", //AJAX,NONE checkbox: false, //checkbox全值傳送 callprev: function(){}, //送出前先執行 }; $.extend(settings, option); var $form = $(this); var _this = this; this._thisInfo = function($this, $form){ if($this.attr('fun')){ var ret = check_tools[$this.attr('fun')]($this, $form); }else{ var ret = "PASS"; } var id = $form.find("#" + $this.attr("id")).attr("id"); var name = $form.find("#" + $this.attr("id")).attr("name"); var nullstr = $form.find("#" + $this.attr("id")).attr("nullstr"); var errorstr = $form.find("#" + $this.attr("id")).attr("errorstr"); var repeatstr = $form.find("#" + $this.attr("id")).attr("repeatstr"); var type = $form.find("#" + $this.attr("id")).attr("show_type") || '1'; switch(ret){ case "PASS": $form.find("#" + $this.attr("id")).removeClass("text-eye"); if(type == '1'){ $form.find(".forms_show[for='" + name + "']").fadeOut().attr('error', 'false'); }else{ $form.find(".forms_show[for='" + name + "']").attr('error', 'false'); $form.find(".forms_show2[for='" + name + "']").fadeOut(); } break; case "ERROR": $form.find("#" + $this.attr("id")).addClass("text-eye"); if(type == '1'){ if(errorstr)$form.find(".forms_show[for='" + name + "']").text(errorstr).show().attr('error', 'true'); }else{ if(errorstr)$form.find(".forms_show[for='" + name + "']").text(errorstr).hide().attr('error', 'true'); $form.find(".forms_show2[for='" + name + "']").html('*').show(); } break; case "NULL": $form.find("#" + $this.attr("id")).addClass("text-eye"); if(type == '1'){ if(nullstr)$form.find(".forms_show[for='" + name + "']").text(nullstr).show().attr('error', 'true'); }else{ if(nullstr)$form.find(".forms_show[for='" + name + "']").text(nullstr).hide().attr('error', 'true'); $form.find(".forms_show2[for='" + name + "']").html('*').show(); } break; case "REPEAT": $form.find("#" + $this.attr("id")).addClass("text-eye"); if(type == '1'){ if(repeatstr)$form.find(".forms_show[for='" + name + "']").text(repeatstr).show().attr('error', 'true'); }else{ if(repeatstr)$form.find(".forms_show[for='" + name + "']").text(repeatstr).hide().attr('error', 'true'); $form.find(".forms_show2[for='" + name + "']").html('*').show(); } break; }; }; this._serializeJSON = function($form){ if(settings.checkbox == true){ var datas = $("#"+ $form.attr('id') + " :input[type!=checkbox]").serializeArray();//將表單裡取出 datas = datas.concat( $("#" + $form.attr('id') + " :input[type=checkbox]").map( function(){ var val = ''; (this.checked == true)? val = this.value: val = ''; return {"name": this.name, "value": val} }).get() ); } else{ var datas = $form.serializeArray(); }; return JSON.stringify(datas); }; this._ajax = function($form, datastr, callback){ $.ajax({ type: "POST", cache: false, async: true, jsonp: false, url: $form.attr('action'), data: datastr, dataType: 'json', error: function(data){ // alert('網路連線過慢,網頁請重新整理'); // window.location.reload(); alert(data.responseText); }, success: function(ret){ if($.isFunction(callback))callback(ret); } }); }; //綁定事件-focus, blur $form.find("[fun]").each(function(){ var id = $form.find("#" + $(this).attr("id")).attr("id"); var leftw = parseInt($form.find(".forms_help[for='" + id + "']").attr('leftw')); $(this).focus(function(){ if(leftw){ $form.find(".forms_help[for='" + id + "']").css({ left : $(this).offset().left + leftw, top : $(this).offset().top }).show(); }; }).blur(function(){ $form.find(".forms_help[for='" + id + "']").fadeOut(); }); }); //綁定事件-change, blur $form.find("[fun]").each(function(){ $(this).change(function(){ _this._thisInfo($(this), $form); }).blur(function(){ _this._thisInfo($(this), $form); }); }); //綁定事件-送出表單 $form.submit(function(){ //送出去執行 if($.isFunction(settings.callprev)){ var getmsg = settings.callprev(); if(getmsg){ bootbox.alert(getmsg); return false; }; }; $form.find("[fun]").each(function(){ _this._thisInfo($(this), $form); }); if($form.find(".forms_show[error='true']").size() <= 0){ if($.isFunction(settings.callafter)){ var getmsg = settings.callafter(); if(!getmsg){ return false; }; }; if($.isFunction(settings.callafter2)){ var getmsg = settings.callafter2() if(!getmsg){ return false; }; }; if(settings.type == 'AJAX'){ $.ajax({ url:Project_Country+'ajax/ajax_loading_page.php', success:function(d){ $("body").html(d) } }); _this._ajax($form, _this._serializeJSON($form), function(ret){ if(ret.msg){ alert(ret.msg); }; if(ret.fun)eval(ret.fun); }); return false; }; } else{ var error_str = ""; $form.find(".forms_show[error='true']").each(function(){ error_str += $(this).text()+"\r\n"; }); alert(error_str); return false; }; }); }); } }); })(jQuery);