티스토리 뷰
[ 텍스트 글자수 제한 ]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>limit_txext</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.12.1.min.js" ></script>
</head>
<body>
<textarea name="" id="" cols="30" rows="10" onkeyup="limitText(this, 9, '.current', '.total' )" ></textarea>
<div class="reply-limit">
<em class="current">0</em> / <em class="total">9</em>
</div>
<script type="text/javascript">
function limitText(obj, maxByte, $current, $totoal) {
var strValue = obj.value;
var strLen = strValue.length;
var totalByte = 0;
var len = 0;
var oneChar = "";
var str2 = "";
function count_text(){
for (var i = 0; i < strLen; i++) {
oneChar = strValue.charAt(i);
if (escape(oneChar).length > 4) {
totalByte += 2;
} else {
totalByte++;
}
// 입력한 문자 길이보다 크면 잘라내기 위해 길이저장
if (totalByte <= maxByte) {
len = i + 1;
}
}
if($current == undefined || $totoal == undefined ) { return; }
$($current).text(totalByte);
$($totoal).text(maxByte);
}
count_text();
// 넘어가는 글자를 자른다.
if (totalByte > maxByte) {
alert(maxByte + " 바이트를 초과 입력 할 수 없습니다.");
str2 = strValue.substr(0, len);
obj.value = str2;
// 자른상태로 다시 보기
limitText(obj, maxByte, $current, $totoal);
}
}
</script>
</body>
</html>
'HMLT&CSS&JS > jQuery' 카테고리의 다른 글
[window scroll target positon] (0) | 2022.04.21 |
---|---|
# 특정위치로 스크롤 ( scrollTop ) (0) | 2015.04.22 |
jQuery css 크기 측정 (0) | 2013.08.09 |
# jQuery 회전 (0) | 2013.08.07 |
# chapter06 객체지향 프로그래밍 (0) | 2013.02.04 |
- Total
- Today
- Yesterday