Jump to content

Archived

This topic is now archived and is closed to further replies.

tejesht

How to set max length for textarea tag

Recommended Posts

Hello everybody,

          is it possible to set maxlength for textarea tag so that user can only write comments up to the max length. please suggest

Thanks in Advance
Tejesh

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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.

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites

×

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.