script

textarea byte check

준용 2008. 9. 10. 14:44
function textAreaLengthCheck(thisObj, maxLengthByte) {
	var tempByteLength = 0, cutByteLength = 0; 
	for(var i = 0; i < thisObj.value.length; i++) { 
		if(escape(thisObj.value.charAt(i)).length > 4) { 
			tempByteLength += 2;
		} else {
			tempByteLength++; 
		} 
if(tempByteLength < maxLengthByte) { cutByteLength++; } } if(tempByteLength > maxLengthByte) { alert("max byte : " + maxLengthByte); thisObj.value = thisObj.value.substring(0, (cutByteLength%2==1)?cutByteLength:cutByteLength+1); } }

<textarea onkeyup="textAreaLengthCheck(this, 10)" name="test"></textarea>
ex.