W
wing
Hi all,
I find a JavaScript that limits the field length in a textarea, but it
is not completed.
The script does not handle the copy and paste case. For example, says
the textarea field length is limited to 5 and a 6-character length
text is pasted, no event is triggered. (The script only handles
onKeyPress and onKeyUp)
It would be grateful if you could enlighted me on how to capture the
paste event in a textarea.
Thanks in advance for any inputs and ideas.
Wing
<!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document
-->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- textCounter() parameters are: text field, the count field, max
length -->
<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up
to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4
onKeyDown="textCounter(this.form.message,this.form.remLen,125);"
onMouseOut="textCounter(this.form.message,this.form.remLen,125);"
onKeyUp="textCounter(this.form.message,this.form.remLen,125);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125">
characters left</font>
</form>
</center>
<!-- Script Size: 1.37 KB -->
I find a JavaScript that limits the field length in a textarea, but it
is not completed.
The script does not handle the copy and paste case. For example, says
the textarea field length is limited to 5 and a 6-character length
text is pasted, no event is triggered. (The script only handles
onKeyPress and onKeyUp)
It would be grateful if you could enlighted me on how to capture the
paste event in a textarea.
Thanks in advance for any inputs and ideas.
Wing
<!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document
-->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- textCounter() parameters are: text field, the count field, max
length -->
<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up
to 125 characters. )<br>
<textarea name=message wrap=physical cols=28 rows=4
onKeyDown="textCounter(this.form.message,this.form.remLen,125);"
onMouseOut="textCounter(this.form.message,this.form.remLen,125);"
onKeyUp="textCounter(this.form.message,this.form.remLen,125);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125">
characters left</font>
</form>
</center>
<!-- Script Size: 1.37 KB -->