Script Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function checkMaxLength() {
var textArea = document.getElementsByTagName(‘textarea’);
for (i=0; i<textArea.length; i++) {
textArea[i].onkeyup = textArea[i].onkeydown = function() {
var max = this.getAttribute(‘maxlength’);
var textValue = this.value;
var totalText = textValue.length;
if(totalText<=max) {
var curNum = document.getElementsByTagName(‘span’);
for(j=0; j<curNum.length; j++) {
if (curNum[j].name == this.name) curNum[j].innerHTML = totalText + ‘/’ + max;
}
} else {
alert(max + ‘자리를 넘길 수 없습니다.’);
this.value = this.value.substr(0,max);
}
}
}
}
window.onload=checkMaxLength;
|
Html Code:
1
2
3
4
5
6
7
8
|
<form>
<textarea name=“subject” maxlength=“10”></textarea>
(<span name=“subject”></span>)<br />
<textarea name=“content” maxlength=“20”></textarea>
(<span name=“content”></span>)<br />
<textarea name=“email” maxlength=“30”></textarea>
(<span name=“email”></span>)
</form>
|
Explain:
본문내에 textarea를 전부 찾아 설정된 최대입력값을 넘기면 메세지가 출력되게 하는 함수입니다.