Carpe Programming/javascript
[byte 계산] 문자열 자르기
nolite
2013. 11. 5. 16:39
function CalByte() { var curText; var strLen; var byteIs; var lastByte; var thisChar; var escChar; var curTotalMsg; var okMsg; var result = true; curText = new String(document.sendform.msg.value); strLen = curText.length; byteIs = 0; for(i=0; i<strLen; i++) { thisChar = curText.charAt(i); escChar = escape(thisChar); // ´,¨, ¸ : 2byte 임에도 브라우져에서 1byte로 계산 if (thisChar == "´" || thisChar == "¨" || thisChar == "¸" || thisChar == "§" ) { byteIs++; } if (escChar.length > 4) { byteIs += 2; //특수문자 한글인 경우. } else if(thisChar != '\r') { //개행을 제외한 이외의 경우 byteIs += 1; } if(byteIs > 80) { alert('[안 내] 80byte를 초과하실 수 없습니다.'); thisText = curText.substring(0, i); document.sendform.msg.value = thisText; byteIs = lastByte; result = false; break; } lastByte = byteIs; } curTotalMsg = Math.ceil(byteIs / 80); curEndByte = curTotalMsg * 80; textlimit.innerText = byteIs; return result; }
728x90