Skip to content

Instantly share code, notes, and snippets.

@dstyle0210
Created May 14, 2015 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstyle0210/15ad420a7b90500be475 to your computer and use it in GitHub Desktop.
Save dstyle0210/15ad420a7b90500be475 to your computer and use it in GitHub Desktop.
[jquery] textarea 글자수 , 바이트수 체크하기.
/**
* 글자수(바이트 체크)
* @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