Click to See Complete Forum and Search --> : Textarea Limitation
jordanj
06-09-2003, 10:52 AM
Hello,
I was browsing the free javascripts looking for a script that limits the number of characters that are put in a textarea. I found one that I like, but it doesn't seem to work with Netscape. Couls anyone shed some light on this?
Here is the URL that has the script I like:
http://javascript.internet.com/forms/limit-textarea.html
Thank You;
Jeremy
This will limit it to 5 characters...
<html>
<head>
<script language="javascript" type="text/javascript">
function checkLength(what) {
if (what.value.length >= 5) {
what.value = what.value.substring(0, 4);
}
}
</script>
</head>
<body>
<form name="myform">
<textarea name="mytextarea" onkeydown="checkLength(this);"></textarea>
</form>
</body>
</html>
requestcode
06-10-2003, 07:24 AM
Here is another way of doing it:
<html>
<head>
<title>Maximum Characters Form</title>
<script language="JavaScript">
NS = document.layers? 1:0;
NS6=document.getElementById&&!document.all? 1:0;
var maxcount=0
function Keydown(e,what_len,len)
{
if (NS||NS6)
{var keycode = e.which} // capture key pressed for netscape
else
{var keycode = event.keyCode} // capture key pressed for IE
if(what_len>=len && keycode!='8') // if over specified length and not back space
{
window.status='Ok Ok thats it!'
alert('Please enter your response \r in '+ len +' characters or less!')
return false
}
else
{window.status='Keep Typing!'}
}
</script>
</head>
<body bgcolor="lightblue" onload="document.myform.txta.focus();document.myform.txta.select()">
<center>
<h1><b>Maximum Characters</b></h1>
<form name="myform">
<textarea name="txta" cols="30" rows="4" WRAP="hard" onKeyPress="return Keydown(event,this.value.length,70)">Please keep your message 70 characters or less!</textarea>
</form>
</center>
</body>
</html>
Originally posted by requestcode
Here is another way of doing it: A bit more complex than it has to be, don't you think?