...

View Full Version : Is it possible to set max char on text area input box, stops like <text> will?



percept
01-12-2004, 06:36 PM
With <text> you can set max length and when user inputs and reaches limit you can't type anything else in... like the subject input fields here. Is it possible to set these attributes on <textarea> input fields?

Willy Duitt
01-12-2004, 11:16 PM
Try this:

<script type="text/javascript">
function getLength(l,m){
window.status = l + " of " + m + " maximum characters.";
return l < m;
}
function maxLength(t,m){
var l = t.value.length;
if (l > m){
alert("Your entry of " + l
+ " characters exceeds the maximum of " + m + ".\n"
+ "It will now be truncated.");
t.value = t.value.substring(0,m);
}
getLength(t.value.length,m);
}
</script>
</HEAD>

<BODY>
<form>
<textarea cols="44" rows="12"
onkeypress="return getLength(this.value.length,200)"
onkeyup="return getLength(this.value.length,200)"
onblur="maxLength(this,200)"></textarea>
</form>
(change what is highlighted in red to reflect the number of characters to allow)


.....Willy

percept
01-13-2004, 12:15 AM
Thanks Willy... I will try this. No wonder I was getting stumped on how to do this... I had no idea it would be so involved when its smaller counter part <text> merely uses maxlength=x.

Cheers!

Willy Duitt
01-13-2004, 04:16 AM
Originally posted by percept
I had no idea it would be so involved when its smaller counter part <text> merely uses maxlength=x.

In all actuallity; it is not that complex.
This would also work:

<BODY>
<form>
<textarea cols="44" rows="12"
onKeyUp="if (this.value.length>200){this.value=this.value.substring(0,200)}">
</textarea>
</form>

But, it would not be as user friendly. ;)

The first example does its best to notify the user of the status
thru the status bar and accounts for pasting into the textarea.

I suppose, the need would depend upon how savy your users are! :D

.....Willy

percept
01-13-2004, 08:51 AM
The users will probably be like me... wishing they were savy'r

Thanks again :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum