Get ready for BLACK FRIDAY shopping starting in

Forums

This topic is locked

Specify max length of a textarea

Posted 09 Oct 2003 18:26:58
1
has voted
09 Oct 2003 18:26:58 Dan Berdusco posted:
I am creating a "Classified Ad" site but I would like to be able to limit the amount of text a user can enter. In short, I would like to know if there is a way that I can specify the maximum amount of words or characters that a user can enter into a multiline text box (preferably I would like to limit words, not characters).

Thanks.

Replies

Replied 09 Oct 2003 19:15:42
09 Oct 2003 19:15:42 Jeremy Conn replied:
Not sure about limiting words, but limiting characters is pretty simple - add maxlength="25" to your form item as shown below:

&lt;input type="text" name="textfield" <font color=red>maxlength="25"</font id=red>&gt;

Simply change the number to limit the characters.

<b>Connman21</b>
www.conncreativemedia.com
<b>DEVELOPMENT SETUP</b>
DW MX Studio
Web Server: IIS5
DB: Access2000/SQL2000
OS: XP Pro
Language: ASP/VB
Replied 09 Oct 2003 19:26:25
09 Oct 2003 19:26:25 Dan Berdusco replied:
Connman,

I guess I should have been a little clearer. Setting the max length for a regular textbox is not what I am looking to do. I need to set the max length of a MULTILINE textbox (textarea). I have already tried to add a "maxlength" attribute to the textarea, but it didn't work.

Replied 09 Oct 2003 20:37:14
09 Oct 2003 20:37:14 Dave Clarke replied:
Heres a javascript version that I picked up from www.actionjackson.com, it's for characters not words and I've never tried it so I don't know if it works but here it is anyway

<b>
&lt;html&gt;
&lt;head&gt;
&lt;SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript&gt;
&lt;!--
function saveBtn_onclick() {
/*Validate textarea length before submitting the form*/

var maxChar = 5
if (document.form.field.value.length &gt; maxChar) {
diff=document.form.field.value.length - maxChar;
if (diff&gt;1)
diff = diff + " characters";
else
diff = diff + " character";

alert("This field is limited to " + maxChar + " characters\n" + "Please reduce the text by " + diff);
document.form.field.focus();
return (false);
}
}
//--&gt;
&lt;/SCRIPT&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form name=form&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;
&lt;TEXTAREA rows=5 cols=40 id=field name=field&gt;ActionJackson.com&lt;/TEXTAREA&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td align=center&gt;
&lt;input type="submit" class=button align="center" value="Run Script"
id="saveBtn" name="saveBtn" LANGUAGE=javascript onclick="return saveBtn_onclick()"&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;

&lt;/html&gt;

</b>

Dave

ASP|VBScript|IIS5.1|Access|WinXPPro & WinXPHome
Replied 09 Oct 2003 21:22:35
09 Oct 2003 21:22:35 Dan Berdusco replied:
Thanks For the help. The script offered looks like it will only check the textarea field once the form has been submitted. I needed something that limits the user as they are typing into the field. I found a small javascript that works perfectly.

<pre id=code><font face=courier size=2 id=code>
&lt;!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document --&gt;

&lt;!-- STEP ONE: Paste this code into the HEAD of your HTML document --&gt;

&lt;HEAD&gt;

&lt;SCRIPT LANGUAGE="JavaScript"&gt;
&lt;!-- Original: Ronnie T. Moore --&gt;
&lt;!-- Web Site: The JavaScript Source --&gt;

&lt;!-- Dynamic 'fix' by: Nannette Thacker --&gt;
&lt;!-- Web Site: www.shiningstar.net --&gt;

&lt;!-- This script and many more are available free online at --&gt;
&lt;!-- The JavaScript Source!! javascript.internet.com --&gt;

&lt;!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length &gt; maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// End --&gt;
&lt;/script&gt;
&lt;/HEAD&gt;

&lt;!-- STEP TWO: Copy this code into the BODY of your HTML document --&gt;

&lt;BODY&gt;

&lt;!-- textCounter() parameters are: text field, the count field, max length --&gt;

&lt;center&gt;
&lt;form name=myform action="YOUR-SCRIPT.CGI"&gt;
&lt;font size="1" face="arial, helvetica, sans-serif"&gt; ( You may enter up to 125 characters. )&lt;br&gt;
&lt;textarea name=message wrap=physical cols=28 rows=4 onKeyDown="textCounter(this.form.message,this.form.remLen,125);" onKeyUp="textCounter(this.form.message,this.form.remLen,125);"&gt;&lt;/textarea&gt;
&lt;br&gt;
&lt;input readonly type=text name=remLen size=3 maxlength=3 value="125"&gt; characters left&lt;/font&gt;
&lt;/form&gt;
&lt;/center&gt;

&lt;p&gt;&lt;center&gt;
&lt;font face="arial, helvetica" SIZE="-2"&gt;Free JavaScripts provided&lt;br&gt;
by &lt;a href="javascriptsource.com"&gt;The JavaScript Source&lt;/a&gt;&lt;/font&gt;
&lt;/center&gt;&lt;p&gt;

&lt;!-- Script Size: 1.37 KB --&gt;
</font id=code></pre id=code>

Thanks again for the help guys!
Replied 02 Mar 2006 23:42:41
02 Mar 2006 23:42:41 Lisa Xyz replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Thanks For the help. The script offered looks like it will only check the textarea field once the form has been submitted. I needed something that limits the user as they are typing into the field. I found a small javascript that works perfectly.

<pre id=code><font face=courier size=2 id=code>
&lt;!-- TWO STEPS TO INSTALL LIMIT TEXTAREA:

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document --&gt;

&lt;!-- STEP ONE: Paste this code into the HEAD of your HTML document --&gt;

&lt;HEAD&gt;

&lt;SCRIPT LANGUAGE="JavaScript"&gt;
&lt;!-- Original: Ronnie T. Moore --&gt;
&lt;!-- Web Site: The JavaScript Source --&gt;

&lt;!-- Dynamic 'fix' by: Nannette Thacker --&gt;
&lt;!-- Web Site: www.shiningstar.net --&gt;

&lt;!-- This script and many more are available free online at --&gt;
&lt;!-- The JavaScript Source!! javascript.internet.com --&gt;

&lt;!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length &gt; maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
// End --&gt;
&lt;/script&gt;
&lt;/HEAD&gt;

&lt;!-- STEP TWO: Copy this code into the BODY of your HTML document --&gt;

&lt;BODY&gt;

&lt;!-- textCounter() parameters are: text field, the count field, max length --&gt;

&lt;center&gt;
&lt;form name=myform action="YOUR-SCRIPT.CGI"&gt;
&lt;font size="1" face="arial, helvetica, sans-serif"&gt; ( You may enter up to 125 characters. )&lt;br&gt;
&lt;textarea name=message wrap=physical cols=28 rows=4 onKeyDown="textCounter(this.form.message,this.form.remLen,125);" onKeyUp="textCounter(this.form.message,this.form.remLen,125);"&gt;&lt;/textarea&gt;
&lt;br&gt;
&lt;input readonly type=text name=remLen size=3 maxlength=3 value="125"&gt; characters left&lt;/font&gt;
&lt;/form&gt;
&lt;/center&gt;

&lt;p&gt;&lt;center&gt;
&lt;font face="arial, helvetica" SIZE="-2"&gt;Free JavaScripts provided&lt;br&gt;
by &lt;a href="javascriptsource.com"&gt;The JavaScript Source&lt;/a&gt;&lt;/font&gt;
&lt;/center&gt;&lt;p&gt;

&lt;!-- Script Size: 1.37 KB --&gt;
</font id=code></pre id=code>

Thanks again for the help guys!
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Reply to this topic