/** * 文字列の数値変換(エラー時は「NaN」) * @param inValue 数字文字列(カンマ区切りも可) * @returns 数値(整数) */ function intVal(inValue) { var txt = new String(inValue); var vInt = floatVal(txt, 0); return vInt; } function intValEx(inValue) { var vInt = intVal(inValue); if(isNaN(vInt)) { return ""; } return vInt; } /** * 文字列の数値変換(エラー時は「NaN」) * @param inValue 数字文字列(カンマ区切りも可) * @param inDigit 小数点以下有効桁数 * @returns 数値(小数点以下) */ function floatVal(inValue, inDigit) { var txt = new String(inValue); var vFloat = parseFloat(txt.split(',').join('').trim()); if(inDigit || inDigit === 0) { var cnvFloat = vFloat.toFixed(inDigit); vFloat = parseFloat(cnvFloat); } return vFloat; } function floatValEx(inValue, inDigit) { var vFloat = floatVal(inValue, inDigit); if(isNaN(vFloat)) { return ""; } return vFloat; } /** * 指定コントロールIDの値をINT型数値変換 * @param inControlID */ function cnvInt(inControlID) { var vCnv = intVal(document.getElementById(inControlID).value); if(isNaN(vCnv)) { document.getElementById(inControlID).value = ""; } else { document.getElementById(inControlID).value = vCnv; } } /** * 指定コントロールIDの値をFLOAT型数値変換 * @param inControlID コントロールID * @param inDigit 小数点以下有効桁数 */ function cnvFloat(inControlID, inDigit) { var vCnv = floatVal(document.getElementById(inControlID).value, inDigit); if(isNaN(vCnv)) { document.getElementById(inControlID).value = ""; } else { document.getElementById(inControlID).value = vCnv; } } /** * 文字列をjavascript文字列に変換 * @param inValue 文字列 */ function jsStrVal(inValue) { var vStr =replaceAll(inValue, '\\', '\\\\'); vStr =replaceAll(vStr, '\"', '\\\"'); vStr =replaceAll(vStr, '\'', '\\\''); return vStr; } function replaceAll(expression, org, dest){ return expression.split(org).join(dest); } /** * 文字列のhtmlspecialchars変換 * @param inValue * @returns */ function escapeHTML(inValue) { var escVal = inValue; if(escVal) { escVal = escVal.replace(/&/g,"&") ; escVal = escVal.replace(/"/g,""") ; escVal = escVal.replace(/'/g,"'") ; escVal = escVal.replace(//g,">") ; } else { escVal = ""; } return escVal; } /** * 文字列のhtmlspecialchars変換 * ※改行コードの
変換 * @param inValue * @returns */ function escapeHTMLEX(inValue) { var escVal = escapeHTML(inValue); if(escVal) { escVal = escVal.replace(/\r\n/g, "
"); escVal = escVal.replace(/[\n\r]/g, "
"); } else { escVal = ""; } return escVal; } /** * htmlspecialchars変換済みの文字列を復元 * @param inValue * @returns */ function unescapeHTML(inValue) { var div = document.createElement("div"); div.innerHTML = inValue.replace(//g,">") .replace(/ /g, " ") .replace(/\r/g, " ") .replace(/\n/g, " "); return div.textContent || div.innerText; } /** * IPアドレス書式チェック * @param {type} inValue * @returns {Boolean} */ function isIPAddress(inValue) { if(inValue != "") { // IPアドレス形式のチェック data = inValue.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/); if (!data) { return false; } IP = inValue.split("."); for(idx = 0; idx < 4; idx++) { if(( IP[idx] < 0 ) || ( IP[idx] > 255 )) { return false; } } if(parseInt(IP[0]) === 0 && parseInt(IP[1]) === 0 && parseInt(IP[2]) === 0 && parseInt(IP[3]) === 0) { return false; } } return true; } /** * フォームオブジェクトの要素コピー * @param {type} frmObj * @returns {undefined} */ function directTextCopy(frmObj, baseName, copyName){ frmObj.elements[copyName].value = frmObj.elements[baseName].value; //name(txt)で入力した文字をname(copy)にコピー } /** * 数値をカンマ区切りの文字列に変換 * @param {type} inValue * @param {type} inDigit * @returns {String|cnvNumberText.num} */ function cnvNumberText(inValue, inDigit){ var num = floatVal(inValue, inDigit); if(isNaN(num)) { num = 0; } var txt = new String(num.toFixed(inDigit)); while(txt != (txt = txt.replace(/^(-?\d+)(\d{3})/, "$1,$2"))); return txt; } /** * 文字列長をバイト長(半角1byte, 全角2byte)で取得 * @param {type} str * @returns {Number} */ function getByteLength(str) { var r = 0; if(str) { for (var i = 0; i < str.length; i++) { var c = str.charCodeAt(i); // Shift_JIS: 0x0 ~ 0x80, 0xa0 , 0xa1 ~ 0xdf , 0xfd ~ 0xff // Unicode : 0x0 ~ 0x80, 0xf8f0, 0xff61 ~ 0xff9f, 0xf8f1 ~ 0xf8f3 if ( (c >= 0x0 && c < 0x81) || (c == 0xf8f0) || (c >= 0xff61 && c < 0xffa0) || (c >= 0xf8f1 && c < 0xf8f4)) { r += 1; } else { r += 2; } } } return r; } /** * 文字列をバイト長単位で切り出し * @param {type} text * @param {type} len * @returns {String} */ function substringByte(text, len) { var text_array = text.split(''); var count = 0; var str = ''; for (i = 0; i < text_array.length; i++) { var n = escape(text_array[i]); if (n.length < 4) { count++; } else { count += 2; } if (count > len) { return str; } str += text.charAt(i); } return text; }