WebPublisher/JavaScript & jQuery
[javaScript] textarea 글자수 카운팅
amanda
2023. 8. 16. 09:55
// textarea count
function fn_checkByte(obj){
const maxByte = 400; //최대 400바이트
const text_val = obj.value; //입력한 문자
const text_len = text_val.length; //입력한 문자수
let totalByte=0;
for(let i=0; i<text_len; i++){
const each_char = text_val.charAt(i);
const uni_char = escape(each_char) //유니코드 형식으로 변환
if(uni_char.length>4){
// 한글 : 2Byte
totalByte += 2;
}else{
// 영문,숫자,특수문자 : 1Byte
totalByte += 1;
}
}
document.getElementById('js_count_crt').innerText = totalByte;
if(totalByte>maxByte){
alert('최대 400Byte까지만 입력가능합니다.');
}
}