來源http://www.dotblogs.com.tw/newmonkey48/archive/2009/11/22/12104.aspx
JS代碼網站http://www.cftea.com/c/2006/08/1F94458YM61AMFVD.asp
// JScript File//******************加強javascript功能*****//String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); } String.prototype.LTrim = function() { return this.replace(/(^\s*)/g, ""); } String.prototype.RTrim = function() { return this.replace(/(\s*$)/g, ""); } //******************驗證*******************////檢測中英文夾雜字串實際長度function CheckLen(ControlID,Length,FieldDesc,blnRequest){var control = GetControl(ControlID); var value = GetControlValue(ControlID); var strAlert = ""; if(blnRequest==true){ strAlert = CheckControl("Request",ControlID,FieldDesc);if(strAlert != "")return strAlert;}else if(value=="") return ""; if( blen(value) > Length ) { return FieldDesc+" 欄位中文字不可超過"+(Length/2)+"個字或英文字不可超過"+Length+"個字!\r\n"; } return "";}//檢測中英文夾雜字串實際長度function blen(str) { //參考網址 //http://www.360doc.com/content/081222/15/16915_2177384.html var len = str.match(/[^ -~]/g) == null ? str.length : str.length + str.match(/[^ -~]/g).length ; return len;}//判斷起始時間是否大於結束時間function CheckDateRange (BegDateID,EndDateID,FieldDesc,blnRequest){ var objBegDate; var objEndDate;var valueBegDate,valueEndDate; objBegDate = GetControl(BegDateID); objEndDate = GetControl(EndDateID);valueBegDate = GetControlValue(BegDateID);valueEndDate = GetControlValue(EndDateID); dtBegDate = Date.parse(objBegDate.value); dtEndDate = Date.parse(objEndDate.value);if(blnRequest==true){strAlert = CheckControl("Request",BegDateID,FieldDesc+"開始日期");if(strAlert != "")return strAlert;strAlert = CheckControl("Request",EndDateID,FieldDesc+"結束日期");if(strAlert != "")return strAlert;}else if(valueBegDate=="" || valueEndDate) return ""; if(dtBegDate > dtEndDate) { return "日期較大的, 不能放在前面 !\r\n"; } return "";}//判斷是否為日期格式function CheckDate(ControlID,FieldDesc,blnRequest) {var control = GetControl(ControlID); var value = GetControlValue(ControlID);if(blnRequest==true){strAlert = CheckControl("Request",ControlID,FieldDesc);if(strAlert != "")return strAlert;}else if(value=="") return ""; //規則 yyyy/mm/dd var obj,strValue,flag=false; strValue = GetControlValue(ControlID); if(strValue == "" ) return ""; var objRegExp = /^\d{4}\/\d{1,2}\/\d{1,2}$/; //check to see if in correct format if(!objRegExp.test(strValue)) { return FieldDesc+" 欄位必須為西元日期格式 ! (yyyy/mm/dd)\r\n";//doesn't match pattern, bad date } else{ var strSeparator = '/'; var arrayDate = strValue.split(strSeparator); //create a lookup for months not equal to Feb. var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31, '06' : 30,'07' : 31, '08' : 31,'09' : 30, '10' : 31,'11' : 30,'12' : 31} var intDay = parseInt(arrayDate[2],10); //check if month value and day value agree if(arrayLookup[arrayDate[1]] != null) { if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0) flag= true; //found in lookup table, good date } //check for February (bugfix 20050322) //bugfix for parseInt kevin //bugfix biss year O.Jp Voutat var intMonth = parseInt(arrayDate[1],10); if (intMonth == 2) { var intYear = parseInt(arrayDate[0]); if (intDay > 0 && intDay < 29) { flag= true; } else if (intDay == 29) { if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0)) { // year div by 4 and ((not div by 100) or div by 400) ->ok flag= true; } } } } if(flag==false) { return FieldDesc+" 欄位日期資料有誤 !\r\n"; } return ""; }//判斷是否為數字格式function CheckNumber(ControlID,FieldDesc,blnRequest){var control = GetControl(ControlID); var value = GetControlValue(ControlID);if(blnRequest==true){strAlert = CheckControl("Request",ControlID,FieldDesc);if(strAlert != "")return strAlert;}else if(value=="") return ""; if(IsNumber(value)==false) { return FieldDesc+" 欄位必須為數字 !\r\n"; } return "";}//判斷是否為整數格式function CheckInteger(ControlID,FieldDesc,blnRequest){var control = GetControl(ControlID); var value = GetControlValue(ControlID);if(blnRequest==true){strAlert = CheckControl("Request",ControlID,FieldDesc);if(strAlert != "")return strAlert;}else if(value=="") return ""; if(IsInteger(value)==false) { return FieldDesc +" 欄位必須為整數 !\r\n"; } return ""; }//判斷是否為Email格式function CheckEmail(ControlID,FieldDesc,blnRequest){ var control = GetControl(ControlID); var value = GetControlValue(ControlID);if(blnRequest==true){strAlert = CheckControl("Request",ControlID,FieldDesc);if(strAlert != "")return strAlert;}else if(value=="") return ""; if(IsEmail(value)==false) { return FieldDesc +" 欄位必須為E-mail!\r\n"; } return ""; }//判斷是否為身份證字號格式function CheckIdNumber(ControlID,FieldDesc,blnRequest){var control = GetControl(ControlID); var value = GetControlValue(ControlID);if(blnRequest==true){strAlert = CheckControl("Request",ControlID,FieldDesc);if(strAlert != "")return strAlert;}else if(value=="") return ""; if(IsIdNumber(value)==false) { return FieldDesc +" 欄位必須為身份證字號格式!\r\n"; } return ""; }//確認是否輸入資料function CheckRequest(ControlID,FieldDesc){ var control =GetControl(ControlID);var value = GetControlValue(ControlID);var tagName = control.tagName.toLowerCase();if(tagName=="select"){if(control.options[control.selectedIndex].value==""){return "請選擇"+FieldDesc+"\r\n"; }else return "";}else if(tagName=="input"){var type = control.typeswitch(type){case "text":if(IsEmpty(value)){return "請輸入"+FieldDesc+"\r\n"; }break;case "checkbox": break;case "radio":break;}}return "";}//*******************************共用方法***********************************////取得控制項function GetControlValue(ControlID){ var control;var value; control = GetControl(ControlID); value = control.value;return value}//取得控制項的值function GetControl(ControlID){ return document.getElementById(ControlID); }//強化版測試Functionfunction CheckControl(type,ControlID,FieldDesc,blnRequest){switch(type){ case "Date": return CheckDate(ControlID,FieldDesc,blnRequest) ;break;case "Number":return CheckNumber(ControlID,FieldDesc,blnRequest);break;case "Integer":return CheckInteger(ControlID,FieldDesc,blnRequest);break;case "Email":return CheckEmail(ControlID,FieldDesc,blnRequest);break;case "IdNumber":return CheckIdNumber(ControlID,FieldDesc,blnRequest);break;case "Request":return CheckRequest(ControlID,FieldDesc,blnRequest);break;}}//控制項焦點設定function ControlFocus(ControlID){var control = GetControl(ControlID);control.focus();}//*******************************基本判斷Function***************************////判斷是否為數字function IsNumber(value){ if(isNaN(value)) return false; return true;}//判斷是否為整數function IsInteger(value){ var objRegExp = /^-?\d+$/; if(!objRegExp.test(value)) return false; return true;}//判斷控制項必須要有值function IsEmpty(value){ if(value.Trim()=="") return true;else return false; }//判斷是否為E-mailfunction IsEmail(value){ var objRegExp = /^.+@.+\..+$/; if(!objRegExp.test(value)) return false; return true;}//判斷是否為身份證字號function IsIdNumber(value){ // 依照字母的編號排列,存入陣列備用。 var letters = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'W', 'Z', 'I', 'O'); // 儲存各個乘數 var multiply = new Array(1, 9, 8, 7, 6, 5, 4, 3, 2, 1); var nums = new Array(2); var firstChar; var firstNum; var lastNum; var total = 0; // 撰寫「正規表達式」。第一個字為英文字母, // 第二個字為1或2,後面跟著8個數字,不分大小寫。 var regExpID=/^[a-z](1|2)\d{8}$/i; // 使用「正規表達式」檢驗格式 if (value.search(regExpID)==-1) { // 基本格式錯誤 return false; } else { // 取出第一個字元和最後一個數字。 firstChar = value.charAt(0).toUpperCase(); lastNum = value.charAt(9); } // 找出第一個字母對應的數字,並轉換成兩位數數字。 for (var i=0; i<26; i++) { if (firstChar == letters[i]) { firstNum = i + 10; nums[0] = Math.floor(firstNum / 10); nums[1] = firstNum - (nums[0] * 10); break; } } // 執行加總計算 for(var i=0; i<multiply.length; i++){ if (i<2) { total += nums[i] * multiply[i]; } else { total += parseInt(value.charAt(i-1)) * multiply[i]; } } // 和最後一個數字比對 if ((10 - (total % 10))!= lastNum) { return false; } return true;}