Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textarea - how to set maximum length? 1

Status
Not open for further replies.

andycheese

Programmer
Jun 10, 2002
38
GB
Hi, I'd like to know how to setup a textarea in my form which can be no longer than 400 characters long (displayed as 5 lines of 80).

My code is as follows:

<textarea name=&quot;textfield&quot; cols=&quot;80&quot; rows=&quot;5&quot; wrap=&quot;soft&quot;></textarea>

Now, I would logically expect that having 5 rows of 80 columns (presumably these are equivalent to characters?) would limit the size of the textarea to 400 chars, however, it appears this is not the case. In its current form, there is no way to stop the user typing more than 400 characters (the text keeps wrapping and a scroll-bar appears on the side of the box). This results in important information being truncated when I retreive the field's contents.

Is there an attribute that I'm not using, or am I doing this incorrectly? Or do I need a script to interrogate the length of the textarea whilst a user types? If so, could a kind soul point me in the right direction?!

Any help would be greatly appreciated!
 
does this help?
<textarea name=&quot;textfield&quot; max=&quot;400&quot;></textarea>

Known is handfull, Unknown is worldfull
 
Nope! Thanks for the reply though! Maxlength=&quot;400&quot; doesn't work either.
 
Easiest solution is to do on onBlur event to check the length and alert the user.

<script>
function checkLength(){
if (document.myForm.textfield.value.length > 400){
alert(&quot;Your text is too long, it has been truncated.&quot;)
document.myForm.textfield.value = document.myForm.textfield.value.substr(0,400)
}
}

</script>

<form name=&quot;myForm&quot;>
<textarea name=&quot;textfield&quot; cols=&quot;80&quot; rows=&quot;5&quot; wrap=&quot;soft&quot; onBlur=&quot;checkLength()&quot;></textarea>
</form>


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
Try this (just change &quot;Maxlen&quot; to your maximum lenth):

<html><head><title>textarea length</title>
<SCRIPT language=&quot;JavaScript&quot;>
<!--
function count_change(form){
Msg = form.txtarea.value;
Msglen= Msg.length;
//maximum length
MaxLen=20;
if (Msglen > MaxLen ){
form.txtarea.value=Msg.substring(0,MaxLen);
alert(MaxLen+&quot; symbols allowed only!!&quot;)
} form.txtarea.focus(); }
//-->
</SCRIPT>
</head>
<BODY>
<FORM name=&quot;form1&quot; METHOD=&quot;POST&quot; >
<TEXTAREA cols=50 name=txtarea rows=5 MAXLENGTH=20 WRAP=virtual onChange=&quot;count_change(document.forms[0])&quot; onkeypress=&quot;count_change(document.forms[0])&quot;></TEXTAREA><br>
<input type=button value=&quot;show length&quot; onclick=&quot;alert(this.form.txtarea.value.length)&quot;>
</form>
</body>
</html>

If you don't want the buttton to show the length you can change:

<input type=button value=&quot;show length&quot; onclick=&quot;alert(this.form.txtarea.value.length)&quot;>

to:

<input type=button value=&quot;your own button text&quot;>

Hope this helps,
Erik



<-- My sport: Boomerang throwing !!
!! Many Happy Returns !! -->
 
Cheers Boomarang/Erik! That's exactly what I was looking for.
 
I have one way of dealing with the problem that could be interesting for some people as it uses visual channels to help usubility. All this using onkeypress. :)

<form onsubmit=&quot;var tArea = document.getElementById('tArea'); var max = tArea.getAttribute('maxlength'); if (tArea.value.length > max){ alert('The amount of characters in the textarea is limited to ' + max + ' characters\nPlease truncate your message'); tArea.focus(); return false } &quot;>

<input type=&quot;text&quot; value=&quot;100&quot; size=&quot;3&quot; id=&quot;charsLeft&quot;>Characters left.

<br>

<textarea id=&quot;tArea&quot; maxlength=&quot;100&quot; onkeypress=&quot;var len = this.value.length; var max = this.getAttribute('maxlength'); document.getElementById('charsLeft').value = max - len; return (len < max)&quot;></textarea>

<input type=&quot;submit&quot; value=&quot;submit&quot;>
</form>

Gary Haran
==========================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top