Jump to content

How to set max length for textarea tag


tejesht

Recommended Posts

You'll want to use javascript for that. Heres an example:
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
< script type="text/javascript" >
function postlimit()
{
var maxlength = 500

if (document.form1.txtarea.value.length > maxlength)
    {
alert("You have typed the maximun amount of characters for your post.\n\n" + maxlength + " Character max per post, this includes the use of BBCode and Smilies");
document.form1.txtarea.value = document.form1.txtarea.value.substring(0, maxlength);
}
    else
    {
document.form1.counter.value = maxlength - document.form1.txtarea.value.length;
}
}
< /script >
</head>
<body>

<form name="form1" action="" method="post">
  <textarea name="txtarea" onkeydown="postlimit()" cols="45" rows="8"></textarea><br />
  <small>Characters left: <input type="text" name="counter" size="4" disabled="disabled" value="500" />&nbsp;&nbsp;Max 500 characters</small>
</form>

</body>
</html>[/code]

Code key:
maxlength - holds the max number fo characters allowed to be entered in the textarea.
form1 - the name of the form (add name="formNameHere" in the form tag) that contains the textarea
txtarea - the name of the textarea
counter - the name of the form field called counter which shows how many characters are left
Link to comment
Share on other sites

If you implement the javascript option then you rely on the client having js on.

But that is the only real option to limit on teh client side. You shoudl also check the length of teh string submitted in your processing script - if its longer than you allow then direct them back to the page and tell them that the text is too long.
Link to comment
Share on other sites

if you were going to choose one or the other, then choose toonmariner's method. use php to check the length of the string submitted, and redirect to the form with an error saying it's too long.  That way, you will not be at the mercy of the user having js enabled.  But ideally, you should implement both options.  Do the js option because it saves you bandwidth and makes it more convenient for the user, but also use php to check it as well, just in case the user has js disabled. 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.