/************************************************************************************* 程式功能 : 表單驗證工具 建置日期 : 2012-03-36 版本 : 1.0 版權所有 : 尚峪資訊科技有限公司 http://www.shang-yu.com.tw -------------------------------------------------------------------------------------- fromsAuth 表單元件驗證參數(fun) 函式 checkNull(jquery obj) : 驗證空白 checkNum(jquery obj) : 驗證數字 checkNumNull(jquery obj) : 驗證數字必填 checkEnNum(jquery obj) : 驗證英文數字 checkEnNumNull(jquery obj) : 驗證英文數字必填 checkPersonID(jquery obj) : 驗證身份證字號 checkPersonIDNull(jquery obj) : 驗證身份證字號必填 checkEmail(jquery obj) : 驗證電子信箱 checkEmailNull(jquery obj) : 驗證電子信箱必填 checkItems(jquery obj, jquery formObj) : 驗證核選方塊及選項方塊 checkSame(jquery obj, jquery formObj) : 驗證和checkSame欄位的值是否相同 checkSameNull(jquery obj, jquery formObj) : 驗證和checkSame欄位的值是否相同必填 checkEmailCellphoneNull(jquery obj) : 驗證只允許Email和手機格式必填 checkTarget(jquery obj) : 驗證本欄位與指定欄位,值是否一樣 返回值 PASS : 為驗證成功 ERROR : 為格式錯誤 NULL : 欄位空值 *************************************************************************************/ var check_tools = { _PASS: "PASS", _ERROR: "ERROR", _NULL: "NULL", _REPEAT: "REPEAT", _NOT_EQUAL: "NOT_EQUAL", _TARGET_ERROR: "TARGET_ERROR", checkNull: function($this){ //驗證空值 if($this.val() == ''){ return this._NULL; }; return this._PASS; }, 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._ERROR; }; }; return this._PASS; }, checkNumNull: function($this){ //驗證數字必填 //驗證空白 var ret = check_tools.checkNull($this); if(ret != this._PASS)return ret; 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._ERROR; }; }; return this._PASS; }, 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._ERROR; }; }; return this._PASS; }, checkEnNumNull : function($this){ //驗證英文數字必填 //驗證空白 var ret = check_tools.checkNull($this); if(ret != this._PASS)return ret; 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._ERROR; }; }; return this._PASS; }, 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._ERROR; }; 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._ERROR; }; }; return this._PASS; }, checkPersonIDNull: function($this){ //驗證身份證字號必填 //驗證空白 var ret = check_tools.checkNull($this); if(ret != this._PASS)return ret; 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._ERROR; }; 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._ERROR; }; return this._PASS; }, 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 this._PASS; }; 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._ERROR; }; }; 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._ERROR; }; }; return this._PASS; }; return this._ERROR; }, checkEmailNull: function($this){ //驗證電子信箱必填 //驗證空白 var ret = check_tools.checkNull($this); if(ret != this._PASS)return ret; var str_pos = $this.val().indexOf("@"); var str_pos1 = $this.val().indexOf("."); var str_len = $this.val().length; if(str_len <= 0) { return this._PASS; }; emailRule = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/; if($this.val().search(emailRule)==-1){ return this._ERROR; } 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._ERROR; }; }; 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._ERROR; }; }; return this._PASS; }; return this._ERROR; }, checkSelect: function($this, $form){ //驗證核選方塊及選項方塊 var val = $form.find("[name=" + $this.attr('name') + "]").val(); if(val == '' || val == 0){ return this._NULL; } return this._PASS; }, checkItems: function($this, $form){ //驗證核選方塊及選項方塊 if($form.find("[name=" + $this.attr('name') + "]:checked").size() == 0){ return this._NULL; } return this._PASS; }, checkSame: function($this, $form){ //驗證值是否相同 var ret = ""; $form.find("[checkSame = 'true']").each(function(){ if($this.val() != $(this).val()){ ret = check_tools._ERROR; }; }); if(ret == this._ERROR) return ret; else return this._PASS; }, checkSameNull: function($this, $form){ //驗證值是否相同並必填 //驗證空白 var ret = check_tools.checkNull($this); if(ret != this._PASS)return ret; var ret = ""; $form.find("[checkSame = 'true']").each(function(){ if($this.val() != $(this).val()){ ret = check_tools._ERROR; }; }); if(ret == this._ERROR) return ret; else return this._PASS; }, checkNullOne: function($this, $form){ var ret1 = check_tools.checkNull($this); var ret2 = check_tools.checkNull($form.find("#" + $this.attr('one')) ); if(ret1 == this._PASS || ret2 == this._PASS){ return this._PASS; } else{ return ret1; } }, checkNum_0: function($this){ //判斷大於0的數字 var ret = check_tools.checkNum($this); if(ret != this._PASS)return ret; if($this.val() < 1 && $this.val() != ''){ return this._ERROR; } return this._PASS; }, checkNumNull_0: function($this){ //判斷大於0的數字必填 var ret = check_tools.checkNumNull($this); if(ret != this._PASS)return ret; if($this.val() < 1){ return this._ERROR; } return this._PASS; }, checkNum_100: function($this){ //判斷0~100數字 var ret = check_tools.checkNum($this); if(ret != this._PASS)return ret; if(($this.val() > 100) && $this.val() != ''){ return this._ERROR; } return this._PASS; }, checkNumNull_100: function($this){ //判斷0~100數字必填 var ret = check_tools.checkNumNull($this); if(ret != this._PASS)return ret; if($this.val() > 100){ return this._ERROR; } return this._PASS; }, checkEmailCellphoneNull: function($this){ var ret_null = this.checkNull($this); // var ret1 = this.checkNumNull($this); var ret2 = this.checkEmailNull($this); if(ret_null == this._NULL){ return this._NULL } else if(ret_null == this._PASS || ret2 == this._PASS){ return this._PASS; } else{ return this._ERROR; } }, checkCellphone:function($this){ if($this.val().indexOf('*') != '-1'){ var PASS = this._PASS, ERROR = this._ERROR, REPEAT = this._REPEAT; $.ajax({ url:"/ajax/ajax_check_cellphone.php", type:"POST", cache:false, async:false, data:{type:'check',cellphone:$this.val()}, error:function(d){ alert('網路連線過慢,網頁請重新整理'); }, success:function(d){ if(d == 'OK'){ $return = PASS; }else{ $return = REPEAT; } } }); return $return; }else{ var ret = this.checkNumNull($this), ERROR = this._ERROR, PASS = this._PASS, REPEAT = this._REPEAT; if(ret == this._PASS){ //檢查是否有人註冊 $.ajax({ url:"/ajax/ajax_check_cellphone.php", type:"POST", cache:false, async:false, data:{type:'check',cellphone:$this.val()}, error:function(d){ alert('網路連線過慢,網頁請重新整理'); }, success:function(d){ if(d == 'OK'){ $return = PASS; }else{ $return = REPEAT; } } }); return $return; }else{ return ret; } } }, isValidGUINull:function($this) { //驗證空白 var ret = check_tools.checkNull($this); if(ret != this._PASS)return ret; var taxId = $this.val(); var invalidList = "00000000,11111111"; if (/^\d{8}$/.test(taxId) == false || invalidList.indexOf(taxId) != -1) { return this._ERROR; } var validateOperator = [1, 2, 1, 2, 1, 2, 4, 1], sum = 0, calculate = function(product) { var ones = product % 10, tens = (product - ones) / 10; return ones + tens; }; for (var i = 0; i < validateOperator.length; i++) { sum += calculate(taxId[i] * validateOperator[i]); } if(sum % 10 == 0 || (taxId[6] == 7 && (sum + 1) % 10 == 0)){ return this._PASS; } else{ return this._ERROR; }; }, checkInvo:function($this) { var str_len = $this.val().length; if(str_len <= 0) { return this._PASS; }; var chk_str = /^\/{1}[A-Za-z0-9+-.]{7}$/; if(str_len == 8 && chk_str.test($this.val())) { return this._PASS; }else{ return this._ERROR; } }, checkInvoNull:function($this) { //驗證空白 var ret = check_tools.checkNull($this); var str_len = $this.val().length; if(ret != this._PASS)return ret; var chk_str = /^\/{1}[A-Za-z0-9+-.]{7}$/; if(str_len == 8 && chk_str.test($this.val())) { return this._PASS; }else{ return this._ERROR; } }, checkNature:function($this) { var str_len = $this.val().length; if(str_len <= 0) { return this._PASS; }; var chk_str = /^[a-zA-Z]{2}\d{14}$/; if(str_len == 16 && chk_str.test($this.val())) { return this._PASS; }else{ return this._ERROR; } }, checkNatureNull:function($this) { //驗證空白 var ret = check_tools.checkNull($this); var str_len = $this.val().length; if(ret != this._PASS)return ret; var chk_str = /^[a-zA-Z]{2}\d{14}$/; if(str_len == 16 && chk_str.test($this.val())) { return this._PASS; }else{ return this._ERROR; } }, checkInvoNullSame:function($this,$form) { //驗證空白 var ret = check_tools.checkInvoNull($this); if(ret != this._PASS)return ret; var ret = ""; $form.find("[checkInvoSame = 'true']").each(function(){ if($this.val() != $(this).val()){ ret = check_tools._ERROR; }; }); if(ret == this._ERROR) return ret; else return this._PASS; }, checkNatureNullSame:function($this,$form) { //驗證空白 var ret = check_tools.checkNatureNull($this); if(ret != this._PASS)return ret; var ret = ""; $form.find("[checkInvoSame = 'true']").each(function(){ if($this.val() != $(this).val()){ ret = check_tools._ERROR; }; }); if(ret == this._ERROR) return ret; else return this._PASS; }, //台灣手機 checkTaiwanCell:function($this,$form) { //驗證空白 var ret = check_tools.checkNull($this); var str_len = $this.val().length; if(ret != this._PASS)return ret; var chk_str = /^09[0-9]{8}$/; if(str_len == 10 && chk_str.test($this.val())) { return this._PASS; }else{ return this._ERROR; } }, checkTaiwanCell1:function($this,$form) { //驗證空白 var ret = check_tools.checkNull($this); var str_len = $this.val().length; if(ret != this._PASS)return ret; var chk_str = /^09[0-9*]{8}$/; if(str_len == 10 && chk_str.test($this.val())) { return this._PASS; }else{ return this._ERROR; } }, //收件人姓名 checkNameNull:function($this,$form) { //驗證空白 var ret = check_tools.checkNull($this); if(ret != this._PASS)return ret; var c = $this.val(); var n = c.length,s; var len = 0; for(var i=0; i 0 ) { len++; s = s >> 8; } } if(len >= 2 && len <= 30) { return this._PASS; }else{ return this._ERROR; } }, //確定兩欄位是否等值,target 拿來當指定欄位 checkTarget:function($this,$form) { //與指定欄位,值是否一樣 if($form.find('[name="'+$this.attr('target')+'"]').eq(0).length > 0){ if($this.val() == $form.find('[name="'+$this.attr('target')+'"]').eq(0).val()){ return this._PASS; }else{ return this._NOT_EQUAL; } }else{ return this._TARGET_ERROR; } }, //字串長度 中文轉2字元 checkstrlength:function($this,$form) { //驗證空白 var ret = check_tools.checkNull($this); if(ret != this._PASS)return ret; var str = $this.val(); var len = str.replace(/[^\x00-\xff]/g, "xx").length; if(len <= 40) { return this._PASS; }else{ return this._ERROR; } } };