Created
May 14, 2015 08:14
-
-
Save dstyle0210/15ad420a7b90500be475 to your computer and use it in GitHub Desktop.
[jquery] textarea 글자수 , 바이트수 체크하기.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 글자수(바이트 체크) | |
* @param oid : 글자를 받을 Textarea ID | |
* @param tid : 바이트 체크된 값 리턴될 엘리먼트 ID | |
*/ | |
function pubByteCheckTextarea(oid,tid){ | |
$(oid).on("keyup",function(){ | |
var byteTxt = ""; | |
var byte = function(str){ | |
var byteNum=0; | |
for(i=0;i<str.length;i++){ | |
byteNum+=(str.charCodeAt(i)>127)?2:1; | |
if(byteNum<500){ | |
byteTxt+=str.charAt(i); | |
}; | |
}; | |
return Math.round( byteNum/2 ); | |
}; | |
if(byte($(this).val())>250){ | |
alert("250자 이상 입력할수 없습니다."); | |
$(this).val(""); | |
$(this).val(byteTxt); | |
}else{ | |
$(tid).html( byte($(this).val()) ) | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment