/** * String Utils * @author Grandy(grandy.kao@gmail.com) * @version 1.0 */ /** * 將字串以 delim 為分離符號, 並在前後加上 quote * 如: "a,b,c" , delim=",", quote="#", 結果: #a#,#b#,#c# */ String.prototype.addQuoteByDelim = function( delim, quote ) { return quote + this.replace( new RegExp( delim, 'g' ), quote + delim + quote ) + quote; }; /** * 含有多少的中文字(含全形) */ String.prototype.chineseCount = function() { if( this.isBlank() ) return 0; var str = this.trim(); var count = 0; for( var i = 0 ; i < str.length ; i++ ) if( str.charCodeAt( i ) > 256 ) count++; return count; }; String.prototype.convertToJavaConvenience = function(){ return this.toLowerCase().replace( /_([a-z])/g, function( findString, parameter1 ){ return parameter1.toUpperCase(); }); }; /** * 將字串的第一個字母改為小寫 */ String.prototype.firstCharToLowercase = function() { if( this.length == 0 ) return this; if( this.length == 1 ) return this.toLowerCase(); return this.substr( 0, 1 ).toLowerCase() + this.substr( 1 ); }; /** * 檢查欄位字串, 如果為空白或空字串, 則傳回 true * @return true: 為空白或空字串 */ String.prototype.isBlank = function() { return ( this == null ) || ( this.trim().length == 0 ) || /^\s+$/.test( this ); }; /** * ROC 日期格式: 只有 TTT/MM/dd */ String.prototype.isDateFormatBySlashByRoc = function() { return this.isBlank() || /^\-?(|0|1|2|3|4|5|6|7|8|9)\d\d\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])$/.test( this ) && isValidDate(this); }; String.prototype.isDateFormatExcludeZeroNine = function() { return this.isBlank() || /^\-?(|0|1)\d\d\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])$/.test( this ) && isValidDateExcludeZeroNine(this); }; /** * ROC 日期時間格式(不含秒): 只有 TTT/MM/dd HH:MM */ String.prototype.isDateTimeNoSecondFormatBySlashByRoc = function() { return this.isBlank() || /^(0|1)\d\d\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01]) ([0-5][0-9]):([0-5][0-9])$/.test( this ); }; /** * ROC 日期時間格式(含秒): 只有 TTT/MM/dd HH:MM:SS */ String.prototype.isDateTimeFormatBySlashByRoc = function() { return this.isBlank() || /^(0|1)\d\d\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01]) ([0-5][0-9]):([0-5][0-9]):([0-5][0-9])$/.test( this ); }; /** * 檢查是否為 email 格式 * @return true: 為 email 格式 */ String.prototype.isEmail = function() { return this.isBlank() || /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test( this ); }; /** * 檢查是否為身分證格式 * @return true: 身分證格式 */ String.prototype.isIdentity = function() { var sMsg = ""; if (this == '') { sMsg = "請輸入身分證字號"; } else if (this.length != 10) { sMsg = "長度應為 10 !"; } else { sPID = trim(this.toUpperCase()); if (!chkPID_CHAR(sPID)) return; var iChkNum = getPID_SUM(sPID); if (iChkNum % 10 != 0) { var iLastNum = sPID.substr(9, 1) * 1; for (i=0; i<10; i++) { var xRightAlpNum = iChkNum - iLastNum + i; if ((xRightAlpNum % 10) ==0) { sMsg = "最後一個數應為:" + i; break; } } } } console.log(sMsg); if(sMsg == ""){ return true; }else{ return false; } }; function chkPID_CHAR(sPID) { var ALP_STR = "ABCDEFGHJKLMNPQRSTUVXYWZIO"; var NUM_STR = "0123456789"; var sMsg = ""; var iPIDLen = String(sPID).length; var sChk = ALP_STR + NUM_STR; for(i=0;i 1 || padChar.length == 0 ) alert( 'The lenght of padChar must be 1.' ); if( this.length >= size ) return this; var temp = this; var minusLength = size - this.length; for( var i = 0; i < minusLength; i++ ) temp = padChar + temp; return temp; }; /** * 去除左邊空白 * @return 去除左邊空白的字串 */ String.prototype.leftTrim = function() { return this.replace( /(^\s*)/g, '' ); }; /** * right pad a char with a specified char * @param {Object} size * @param {Object} padChar * @memberOf {TypeName} * @return {TypeName} */ String.prototype.rightPad = function( size, padChar ) { if( padChar.length > 1 || padChar.length == 0 ) alert( 'The lenght of padChar must be 1.' ); if( this.length >= size ) return this; var temp = this; var minusLength = size - this.length; for( var i = 0; i < minusLength; i++ ) temp += padChar; return temp; }; /** * 去除右邊空白 * @return 去除右邊空白的字串 */ String.prototype.rightTrim = function() { return this.replace( /(\s*$)/g, '' ); }; /** * 去除前後空白 * @return 去除前後空白的字串 */ String.prototype.trim = function() { return this.replace( /^\s+|\s+$/g, '' ); }; /** * ROC 日期時間格式(不含秒): 只有 yyy/MM/dd HH:MM */ String.prototype.isDateTimeNoSecondFormatBySlashByRoc = function() { return this.isBlank() || /^(0|1)\d\d\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01]) ([0-5][0-9]):([0-5][0-9])$/.test( this ); }; /** * 走私方式 是否存在 * @returns {Boolean} */ String.prototype.isExistBySmugWayCd = function() { return this.isBlank() || isExistBySmugWayCd( this ); }; /** * 檢查是否為英數字,空白 * @return {Boolean} */ String.prototype.isWordOrBlank = function() { return this.isBlank() || /^[A-Za-z0-9 ]*$/.test( this ); }; /** * 走私方式 是否存在 * @param smugWayCd */ function isExistBySmugWayCd( smugWayCd ){ var isExist = false; var url = getContextPath() + '/common!isExistBySmugWayCd'; $.ajaxSetup({async:false}); $.post( url, { 'smugWayCd' : smugWayCd}, function( callback ){ showStatusMsg( callback.status, callback.msg ); $.ajaxSetup({async:true}); isExist = callback.data.isExist; }); return isExist; } /** * 代碼 是否存在 * @returns {Boolean} */ String.prototype.isExistByCode = function(codeKind) { return this.isBlank() || isExistByCode( codeKind, this ); }; /** * 代碼 是否存在 * @param code */ function isExistByCode( codeKind, code ){ var isExist = false; var url = getContextPath() + '/common!isExistByCode'; $.ajaxSetup({async:false}); $.post( url, { 'codeKind' : codeKind, 'code' : code }, function( callback ){ showStatusMsg( callback.status, callback.msg ); $.ajaxSetup({async:true}); isExist = callback.data.isExist; }); return isExist; } /** * @param value 日期 * @return {Boolean} */ function isValidDate(value){ var val = value.replace(/\//g, ''); var year = parseInt(val.substring(0,3)) + 1911 ; var month = parseInt(val.substring(3,5))-1 ;//month start from 0 var date = val.substring(5,7); var presumedDate = new Date(year, month, date); if (presumedDate.getDate() != date){ return false; } else{ return true; } }; /** * @param value 日期 * @return {Boolean} */ function isValidDateExcludeZeroNine(value){ var val = value.replace(/\//g, ''); var year = parseInt(val.substring(0,3)) + 1911 ; if(year == 1911 || year == 2910){ return false; }else{ var month = parseInt(val.substring(3,5))-1 ;//month start from 0 var date = val.substring(5,7); var presumedDate = new Date(year, month, date); if (presumedDate.getDate() != date){ return false; } else{ return true; } } }; /************* 日期檢查 ************* 1.日期字串格式必須為 yyyymmdd. 2.必須搭配 isLeap() 來判斷閏年. *************************************/ String.prototype.isDateValidateByString = function() { if(this<0 || isNaN(this) || (this.length != 8)) { //alert("日期格式錯誤, 請用yyyymmdd"); return false; } var leap = 28; if (isLeap(parseInt(this.substring(0,4)))) leap = 29; var mtmp = parseInt(this.substring(4,6)) if (this.substring(4,6) == '08') mtmp = 8; if (this.substring(4,6) == '09') mtmp = 9; if (mtmp < 1 || mtmp > 12) { //alert("月錯誤"); return false; } var dayInMonth = new Array(12); dayInMonth[1] = 31; dayInMonth[2] = leap; dayInMonth[3] = 31; dayInMonth[4] = 30; dayInMonth[5] = 31; dayInMonth[6] = 30; dayInMonth[7] = 31; dayInMonth[8] = 31; dayInMonth[9] = 30; dayInMonth[10] = 31; dayInMonth[11] = 30; dayInMonth[12] = 31; var dtmp = parseInt(this.substring(6)); if (this.substring(6) == '08') dtmp = 8; if (this.substring(6) == '09') dtmp = 9; if(dtmp < 1 || dtmp > dayInMonth[mtmp]) { //alert("日錯誤"); return false; } return true; }; /** * ROC 日期格式: 只有 YYY/MM */ String.prototype.isYearMonthWithSlashValidate = function() { if(isNaN(this.substring(0,3)) || this.length != 6) { //alert("日期格式錯誤, 請用YYY/MM"); return false; } var leap = 28; if (isLeap(parseInt(this.substring(0,3))+1911)) leap = 29; var mtmp = parseInt(this.substring(4,6)); if (mtmp < 1 || mtmp > 12) { //alert("月錯誤"); return false; } return true; }; /** * ROC 日期格式: 只有 YYY/MM */ String.prototype.isYearMonthWithSlashValidateExcludeZeroNine = function() { if(isNaN(this.substring(0,3)) || this.length != 6) { //alert("日期格式錯誤, 請用YYY/MM"); return false; } if(this.substring(0,3) == "000" || this.substring(0,3) == "999") { return false; } var leap = 28; if (isLeap(parseInt(this.substring(0,3))+1911)) leap = 29; var mtmp = parseInt(this.substring(4,6)); if (mtmp < 1 || mtmp > 12) { //alert("月錯誤"); return false; } return true; }; /** * 檢察是否為正確的報單號碼格式。 * 如果是, 則傳回 true * other,則回傳 false。 * @return {Boolean} */ String.prototype.isDeclNo = function() { /** * 目前只驗證 * 1.長度應等於14碼 * 2.是否為 A-Z, a-z, 0-9 空白 的字串 * */ return (this.length == 14) && /^[A-Za-z0-9 ]*$/.test( this ); };