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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<TEXTAREA> Question!! 1

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
I have been looking through the forum at previous posts for fixing the maximum length of a textarea. I am trying to limit the content to 90 characters as that is all that the database will accept. According to previous posts, the only way to do this is with a JS function which spits out an alert saying that it is greater than 90 chars.

Surely there is some feature of the textarea itself that will just not allow the text box to scroll and hence not allow any more characters to be typed once the maximum has been typed. Mise Le Meas,

Mighty :)
 
have a try at this, I have set the max chrs to 90.

<input type=&quot;text&quot; name=&quot;textfield&quot; maxlength=&quot;90&quot;>

hope this helps

Manic -----------------------------
I've broken it again !!
-----------------------------
 
Manic,

Thanks for the response but I wanted to use a textarea and not a text input field. Mise Le Meas,

Mighty :)
 
Mighty, I don't think you can limit it. It says here that &quot;users may enter up to 32,700 characters in a text area&quot; -- no mention of being able to limit that. :-(
 
The only way to limit the input on a textarea is to use a client side script.

I have included an example script below. This script will check the length of the entry when the user moves out of the textarea and also when they attempt to submit the form.

If the entry is more than 90 characters, a message box advises the user that the entry is too long and tells them what the current length is. It then returns focus to the relevant text box and prevents the form from being submitted.

Give it a try:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script language=&quot;javascript&quot;>
<!--
function CheckLength()
{
TextLength = document.TheForm.TheTextArea.value.length
if (TextLength > 90)
{
alert('The maximum length for the TEXTAREA field is 90 characters.\n\nYour entry is currently ' + TextLength + ' characters.\n\nPlease amend your entry.');
document.TheForm.TheTextArea.focus();
return false;
}
return true;
}
//-->
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;TheForm&quot; onSubmit=&quot;return CheckLength()&quot;>
<textarea name=&quot;TheTextArea&quot; value=&quot;&quot; onBlur=&quot;CheckLength()&quot;></textarea>
<br>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>

This script should work in both IE and NN, although I've only tested it in IE.
 
I have seen scripts that continuously count the number of characters entered in a textarea an refuse to accept any additional keystrokes once the maximum is reached. It probably uses a keypress event handler to do it. The suncom site ( under &quot;messaging center&quot; has a textbox which shows the number of characters entered into the textarea. Here's how they do it (just the input tags shown, not the surrounding html):
Code:
<TEXTAREA cols=15 name=message onkeyup=this.form.sizebox.value=this.value.length rows=5 wrap=PHYSICAL></TEXTAREA>

<INPUT SIZE=&quot;3&quot; NAME=&quot;sizebox&quot; VALUE=&quot;0&quot; disabled>
This doesn't limit the text length, but does tell you how many characters you've entered.

You could call a function for onkeyup instead and check the length, or you could also use an onSubmit event handler to check the length of the textarea and pop up an alert box if it exceeds the maximum.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top