/************************************************************************************* 程式功能 : 表單驗證 建檔日期 : 2011-06-02 版本 : 1.0 版權所有 : 尚峪資訊科技有限公司 http://www.shang-yu.com.tw -------------------------------------------------------------------------------------- 表單元件驗證參數(fun) checkNull : 驗證空白 checkNum : 驗證數字 checkNumNull : 驗證數字必填 checkEnNum : 驗證英文數字 checkEnNumNull : 驗證英文數字必填 checkPersonID : 驗證身份證字號 checkPersonIDNull : 驗證身份證字號必填 checkEmail : 驗證電子信箱 checkEmailNull : 驗證電子信箱必填 checkItems : 驗證核選方塊及選項方塊 checkSame : 驗證和checkSame欄位的值是否相同 checkSameNull : 驗證和checkSame欄位的值是否相同必填 checkExt : 驗證上傳格式(屬性ext="jpg,xls....") Length : 驗證至少輸入 checkOne one : 驗證其中一個必填 checkOneNum one : 驗證數字,其中一個必填 checkTEmailNull : 驗證兩組電子信箱必填 checkMaxNull : 驗證字數上限必填 *************************************************************************************/ (function ($){ jQuery.fn.extend ({ form_check: function(option){ return this.each(function(){ var settings = {}; $.extend(settings, option); //綁定事件-送出表單 $(this).submit(function(){ var $_this = $(this); var start_check = (function(){ var message = ""; //驗證工具 var check_tools = { checkNull : function($this){ //驗證空白 if($this.val() == ''){ return $this.attr('des') + " - 必填\n"; } return ""; }, checkNum : function($this){ //驗證數字 var number = "0123456789"; var string; for(var i = 0;i <= $this.val().length - 1;i++) { string = $this.val().substring(i,i + 1); if(number.indexOf(string) == -1) { return $this.attr('des') + " - 只能輸入0~9的數字\n"; } } return ""; }, checkNumNull : function($this){ //驗證數字必填 //驗證空白 var msg = check_tools.checkNull($this); if(msg)return msg; var number = "0123456789"; var string; for(var i = 0;i <= $this.val().length - 1;i++) { string = $this.val().substring(i,i + 1); if(number.indexOf(string) == -1) { return $this.attr('des') + " - 只能輸入0~9的數字\n"; } } return ""; }, checkNum8 : function($this){ //驗證數字必填 var number = "0123456789"; var string; for(var i = 0;i <= $this.val().length - 1;i++) { string = $this.val().substring(i,i + 1); if(number.indexOf(string) == -1) { return $this.attr('des') + " - 只能輸入0~9的數字\n"; } } if($this.val().length != '8' && $this.val().length != '0' ){ return $this.attr('des') + " - 只能輸入8個數字\n"; } return ""; }, checkEnNum : function($this){ //驗證英文數字 var number = "00123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"; var string; for(var i = 0;i <= $this.val().length - 1;i++) { string = $this.val().substring(i,i + 1); if(number.indexOf(string) == -1) { return $this.attr("des") + " - 只能輸入數字、大小寫英文字母及-符號\n"; } } return ""; }, checkEnNumNull : function($this){ //驗證英文數字必填 //驗證空白 var msg = check_tools.checkNull($this); if(msg)return msg; var number = "00123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"; var string; for(var i = 0;i <= $this.val().length - 1;i++) { string = $this.val().substring(i,i + 1); if(number.indexOf(string) == -1) { return $this.attr("des") + " - 只能輸入數字、大小寫英文字母及-符號\n"; } } return ""; }, checkPersonID : function($this){ //驗證身份證字號 if($this.val()){ var c, n, i; var t = "ABCDEFGHJKLMNPQRSTUVXYWZIO"; c = $this.val().substring(0,1); c = t.indexOf(c.toUpperCase()); if (($this.val().length != 10) || (c < 0)) { return $this.attr('des') + " - 身份證號格式不正確\n"; } n = parseInt(c/10) + c%10*9 + 1; for (i = 1; i<9; i++) n = n + parseInt($this.val().substring(i, i+1)) * (9-i); n = (10 - (n % 10)) % 10; if (n != parseInt($this.val().substring(9, 10))) { return $this.attr('des') + " - 身份證號格式不正確\n"; } } return ""; }, checkPersonIDNull : function($this){ //驗證身份證字號必填 //驗證空白 var msg = check_tools.checkNull($this); if(msg)return msg; var c, n, i; var t = "ABCDEFGHJKLMNPQRSTUVXYWZIO"; c = $this.val().substring(0,1); c = t.indexOf(c.toUpperCase()); if (($this.val().length != 10) || (c < 0)) { return $this.attr('des') + " - 身份證號格式不正確\n"; } n = parseInt(c/10) + c%10*9 + 1; for (i = 1; i<9; i++) n = n + parseInt($this.val().substring(i, i+1)) * (9-i); n = (10 - (n % 10)) % 10; if (n != parseInt($this.val().substring(9, 10))) { return $this.attr('des') + " - 身份證號格式不正確\n"; } return ""; }, checkEmail : function($this){ //驗證電子信箱 var str_pos = $this.val().indexOf("@"); var str_pos1 = $this.val().indexOf("."); var str_len = $this.val().length; if(str_len <= 0) { return ""; } var chk_str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"; if(str_pos != -1 && str_pos1 != -1) { for(var i= 0;i <= str_pos - 1;i++) { if(chk_str.indexOf($this.val().substring(i,i+1)) == -1) { return $this.attr('des') + " - 格式錯誤\n"; } } for(var i= str_pos + 1;i <= $this.val().length - 1;i++) { if(chk_str.indexOf($this.val().substring(i,i+1)) == -1) { return $this.attr('des') + " - 格式錯誤\n"; } } return ""; } return $this.attr('des') + " - 格式錯誤\n"; }, checkEmailNull : function($this){ //驗證電子信箱必填 //驗證空白 var msg = check_tools.checkNull($this); if(msg)return msg; var str_pos = $this.val().indexOf("@"); var str_pos1 = $this.val().indexOf("."); var str_len = $this.val().length; if(str_len <= 0) { return ""; } var chk_str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"; if(str_pos != -1 && str_pos1 != -1) { for(var i= 0;i <= str_pos - 1;i++) { if(chk_str.indexOf($this.val().substring(i,i+1)) == -1) { return $this.attr('des') + " - 格式錯誤\n"; } } for(var i= str_pos + 1;i <= $this.val().length - 1;i++) { if(chk_str.indexOf($this.val().substring(i,i+1)) == -1) { return $this.attr('des') + " - 格式錯誤\n"; } } return ""; } return $this.attr('des') + " - 格式錯誤\n"; }, checkItems : function($this){ //驗證核選方塊及選項方塊 if($_this.find("[name=" + $this.attr('name') + "]:checked").size() == 0){ return $this.attr('des') + " - 至少點選一個\n"; } return ""; }, checkItem2s : function($this){ //驗證核選方塊及選項方塊 if($_this.find("[name=" + $this.attr('name') + "]:checked").size() == 0){ return $this.attr('des') + " - 請勾選\n"; } return ""; }, checkSame : function($this){ var msg = ""; $_this.find("[checkSame = 'true']").each(function(){ if($this.val() != $(this).val()){ msg = $this.attr('des') + " - 與" + $(this).attr('des') + "的值不相同\n"; } }); if(msg)return msg; return ""; }, checkSameNull : function($this){ //驗證空白 var msg = check_tools.checkNull($this); if(msg)return msg; $_this.find("[checkSame = 'true']").each(function(){ if($this.val() != $(this).val()){ msg = $this.attr('des') + " - 與" + $(this).attr('des') + "的值不相同\n"; } }); if(msg)return msg; return ""; }, checkExt : function($this){ if($this.val() && $this.attr('ext')){ var msg = ""; var ext = $this.attr('ext'); var val = $this.val().split(".")[$this.val().split(".").length - 1]; var ext_arr = ext.split(","); for(k in ext_arr){ if(ext_arr[k] == val){ var chk = true; break; } } if(!chk)msg = $this.attr('des') + '檔案類型限定:' + ext + "格式\n"; } if(msg)return msg; return ""; }, checkExtNull : function($this){ var msg = check_tools.checkNull($this); if($this.val() && $this.attr('ext')){ var ext = $this.attr('ext'); var val = $this.val().split(".")[$this.val().split(".").length - 1]; var ext_arr = ext.split(","); for(k in ext_arr){ if(ext_arr[k] == val){ var chk = true; break; } } if(!chk)msg = $this.attr('des') + '檔案類型只能是' + ext; } if(msg)return msg; return ""; }, checkOne : function($this){ //驗證其中一個必填 var not_null = 0 ; var str = '' ; $_this.find("[one="+$this.attr("one")+"]").each(function(){ if( $(this).val() != '' ){ not_null++; } str += $this.attr('des') + " " ; }); if( not_null == 0 ){ return str + " - 選填一項\n"; }else{ return "" ; } }, checkOneNum : function($this){ //驗證數字其中一個必填 var number = "0123456789,#-"; var str_pos = '' , str = '' , string = '' , type_err = '' , pos_err = '' , pos_num = 0 ; var not_null = 0 ; $_this.find("[one="+$this.attr("one")+"]").each(function(){ if( $(this).val() != '' ){ pos_num = 0 ; str_pos = $(this).val().split(",").length - 1; if( str_pos > 1){ type_err += $(this).attr('des') + " - 格式錯誤\n" ; } for(var i = 0;i <= $(this).val().length - 1;i++) { string = $(this).val().substring(i,i + 1); if(number.indexOf(string) == -1 && pos_num == 0 ) { pos_err += $(this).attr('des') + " - 只能輸入0~9及,\n"; pos_num++; } } not_null++; } str += $(this).attr('des') + " " ; }); if( type_err ) return type_err ; if( pos_err ) return pos_err ; if( not_null == 0 ) return str + " - 選填一項\n"; return "" ; }, checkTEmailNull : function($this){ //驗證電子信箱 //驗證空白 var msg = check_tools.checkNull($this); if(msg)return msg; var str_pos2 = $this.val().split(",").length-1 ; var str_email = $this.val().split(",") ; var chk_str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"; var str_len = $this.val().length; var email_err = 0 ; if(str_len <= 0) { return ""; } if( str_pos2 > 3 ){ return $this.attr('des') + " - 格式錯誤\n"; } for( var aa = 0 ; aa <= str_pos2 ; aa++ ){ var str_pos = str_email[aa].indexOf("@") ; var str_pos1 = str_email[aa].indexOf(".") ; if(str_pos != -1 && str_pos1 != -1) { for(var i= 0;i <= str_pos - 1;i++) { if(chk_str.indexOf(str_email[aa].substring(i,i+1)) == -1) { email_err++; } } for(var i= str_pos + 1;i <= str_email[aa].length - 1;i++) { if(chk_str.indexOf(str_email[aa].substring(i,i+1)) == -1) { email_err++; } } }else{ email_err++; } } if( email_err != 0 ){ return $this.attr('des') + " - 格式錯誤\n"; }else{ return ""; } }, checkMaxNull : function($this){ //驗證空白 var n = $this.val().length , s; var len = 0; if($this.val() == ''){ return $this.attr('des') + " - 必填\n"; } //計算字數 英文 1 中文 2 for(var i = 0 ; i < n ; i++){ s=$this.val().charCodeAt(i); while( s > 0 ){ len++; s = s >> 8 ; } } if( len > 48 ){ return $this.attr('des') + ' - 中文字不可大於24,英文字不可大於48\n'; } return ""; }, checkLengthMin : function($this){ //驗證空白 var n = $this.val().length , s; var len = $this.attr('Length'); if( n < len ){ return $this.attr('des') + " - 至少輸入六個數字加英文字母\n"; } return ""; } } $_this.find("[fun]").each(function(){ if($(this).attr('fun')){ if(check_tools[$(this).attr('fun')]){ message += check_tools[$(this).attr('fun')]($(this)); if(check_tools[$(this).attr('fun')]($(this)) != "" ){ $(this).css({"border-color":"#e60012"}); }else{ $(this).css({"border-color":""}); } if( $(this).attr('length') ){ message += check_tools["checkLengthMin"]($(this)); } } else{ alert($(this).attr('fun')); }; } }); if(message){ alert(message); return false; } else{ return true; } })(); return start_check; }); }); } }); })(jQuery);