js表單驗證(checkFormItem)
阿新 • • 發佈:2021-07-12
CharType,表單資料型別 "C" 為字串,"N" 為數字,"D"為日期型別。
* MaxLength(最大長度) 整型數字 0表示不限制最大長度
* Precision(精度) 整型數字 表示保留小數位的長度,0表示是整數
* DefiniteLengthMark(定長標誌) 整型數字 0(定長不允許空)/1(不定長不可空)/2(不定長允許空)/3(定長允許空)
* Describe(表單描述) 比如:電話號碼,使用者姓名等等,必須寫
function checkFormItem(FormItemName,CharType,MaxLength,Precision,DefiniteLengthMark,Describe) { var TValue; TValue=FormItemName.value; //得到文字框去掉頭尾空格的值 TValue=TValue.replace(/(^\s*)|(\s*$)/g, ""); //去掉文字框頭尾空格 FormItemName.value=TValue; //允許空,只要文字框為空返回true if((DefiniteLengthMark==2||DefiniteLengthMark==3)&&(TValue.length==0)) { return(true); } //不允許空,只要文字框為空返回false if((DefiniteLengthMark==0||DefiniteLengthMark==1)&&(TValue.length==0)) { alert(Describe+"不允許為空!"); return(false); } //判斷字串,長度和定長。 if(CharType=="C") { if((DefiniteLengthMark==0||DefiniteLengthMark==3) && getStrLength(TValue)!=MaxLength) { alert(Describe+"規定長度是"+MaxLength+",請檢查!"); return(false); } if(MaxLength!=0&&getStrLength(TValue)>MaxLength && (DefiniteLengthMark==1||DefiniteLengthMark==2)) { alert(Describe+"超過最大長度,允許的最大長度是"+MaxLength+"!"); return(false); } } //判斷數字型別,長度,精度,定長 if(CharType=="N") { //檢查整型數字 if(Precision==0&&!isInteger(TValue)) { alert(Describe+"項填寫必須輸入數字,請檢查!"); return(false); } if(Precision==0&&isInteger(TValue)&&MaxLength!=0&&(DefiniteLengthMark==0||DefiniteLengthMark==3)&&getStrLength(TValue)!=MaxLength) { alert(Describe+"項長度必須是"+MaxLength+",請檢查!"); return(false); } if(Precision==0&&isInteger(TValue)&&MaxLength!=0&&(DefiniteLengthMark==1||DefiniteLengthMark==2)&&getStrLength(TValue)>MaxLength) { alert(Describe+"項輸入數字長度不能超過"+MaxLength+",請檢查!"); return(false); } if (TValue<0){ alert(Describe+"項輸入數字應大於等於0,請檢查!"); return(false); } //檢查整型數字結束 //檢查實數 if(Precision!=0&&!isFloat(TValue)) { alert(Describe+"項必須輸入數字,請檢查!"); return(false); } if(Precision!=0&&isFloat(TValue)&&MaxLength!=0&&(DefiniteLengthMark==0||DefiniteLengthMark==3)&&getStrLength(TValue)!=MaxLength) { alert(Describe+"項輸入數字長度必須是"+MaxLength+",請檢查!"); return(false); } if(Precision!=0&&isFloat(TValue)&&MaxLength!=0&&(DefiniteLengthMark==1||DefiniteLengthMark==2)&&getStrLength(TValue)>MaxLength) { alert(Describe+"項輸入數字長度不能超過"+MaxLength+",請檢查!"); return(false); } if(Precision!=0&&isFloat(TValue)&&!checkPrecision(TValue,Precision)) { alert(Describe+"項輸入數字應該保留"+Precision+"位小數,請檢查!"); return(false); } } //電話號碼 if(CharType=="P") { TValue = replacePhone(TValue); if(Precision==0&&!isInteger(TValue)) { alert(Describe+"項填寫必須是數字或 #號 或 -號 ,請檢查!"); return(false); } //return(true); } if(CharType=="D") { return(true); } return(true); } function replacePhone(orgStr) { var changeName = "0"; var indexOfs = 0; var infoStrMain; while (orgStr.indexOf ("#", 0) != -1) { indexOfs = orgStr.indexOf ("#", 0); infoStrMain = orgStr.substring (0, indexOfs) + changeName + orgStr.substring (indexOfs + 1); orgStr = infoStrMain; } while (orgStr.indexOf ("-", 0) != -1) { indexOfs = orgStr.indexOf ("-", 0); infoStrMain = orgStr.substring (0, indexOfs) + changeName + orgStr.substring (indexOfs + 1); orgStr = infoStrMain; } return orgStr; } function getStrLength(str) { var winnt_chinese,sLength,i; var TestString="同方"; if (TestString.length==2){ winnt_chinese =true; }else{ winnt_chinese =false; } if (winnt_chinese){ var l, t, c,kk; l = str.length; t = l; for (kk=0;kk<l;kk++) { c = str.charCodeAt(kk); if (c < 0){ c = c + 65536; } if (c > 255){ t = t + 1; } } sLength = t; }else{ sLength = str.length; } return(sLength); } function isInteger(str) { if(isNaN(str)) { return(false); } else { if(str.search("[.*]")!=-1) { return(false); } } return(true); } function isFloat(str) { //alert(isNaN(parseFloat(str))); // return(!isNaN(parseFloat(str))); return(!isNaN(str)); } function checkPrecision(str,n) { var tureorfalse,i,PrecisionLength; PrecisionLength=0; if(!isNaN(parseFloat(str))) { for(i=0;i<str.length;i++) { if(str.charAt(i)==".") { PrecisionLength=str.length-i-1; break; } } } else { return(false); } if(PrecisionLength<=n) return(true); else return(false); } //跳轉頁面 function Gotopage(formname) { //alert(<%=intPageCount%>); //alert(window.dataform.page.value); if(checkFormItem(dataform.page,"C",4,0,1,"轉到頁數")){ window.dataform.target="_self"; window.dataform.action=formname; window.dataform.submit(); } }