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:
<input type="text" name="textfield" <font color=red>maxlength="25"</font id=red>>
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
<input type="text" name="textfield" <font color=red>maxlength="25"</font id=red>>
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.
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>
<html>
<head>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function saveBtn_onclick() {
/*Validate textarea length before submitting the form*/
var maxChar = 5
if (document.form.field.value.length > maxChar) {
diff=document.form.field.value.length - maxChar;
if (diff>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);
}
}
//-->
</SCRIPT>
</head>
<body>
<form name=form>
<table>
<tr><td>
<TEXTAREA rows=5 cols=40 id=field name=field>ActionJackson.com</TEXTAREA>
</td></tr>
<tr><td align=center>
<input type="submit" class=button align="center" value="Run Script"
id="saveBtn" name="saveBtn" LANGUAGE=javascript onclick="return saveBtn_onclick()">
</td></tr>
</table>
</form>
</body>
</html>
</b>
Dave
ASP|VBScript|IIS5.1|Access|WinXPPro & WinXPHome
<b>
<html>
<head>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function saveBtn_onclick() {
/*Validate textarea length before submitting the form*/
var maxChar = 5
if (document.form.field.value.length > maxChar) {
diff=document.form.field.value.length - maxChar;
if (diff>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);
}
}
//-->
</SCRIPT>
</head>
<body>
<form name=form>
<table>
<tr><td>
<TEXTAREA rows=5 cols=40 id=field name=field>ActionJackson.com</TEXTAREA>
</td></tr>
<tr><td align=center>
<input type="submit" class=button align="center" value="Run Script"
id="saveBtn" name="saveBtn" LANGUAGE=javascript onclick="return saveBtn_onclick()">
</td></tr>
</table>
</form>
</body>
</html>
</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>
<!-- 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 -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->
<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: www.shiningstar.net -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! javascript.internet.com -->
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > 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 -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- textCounter() parameters are: text field, the count field, max length -->
<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br>
<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);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125"> characters left</font>
</form>
</center>
<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 1.37 KB -->
</font id=code></pre id=code>
Thanks again for the help guys!
<pre id=code><font face=courier size=2 id=code>
<!-- 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 -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->
<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: www.shiningstar.net -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! javascript.internet.com -->
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > 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 -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- textCounter() parameters are: text field, the count field, max length -->
<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br>
<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);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125"> characters left</font>
</form>
</center>
<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 1.37 KB -->
</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>
<!-- 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 -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->
<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: www.shiningstar.net -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! javascript.internet.com -->
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > 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 -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- textCounter() parameters are: text field, the count field, max length -->
<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br>
<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);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125"> characters left</font>
</form>
</center>
<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 1.37 KB -->
</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>
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>
<!-- 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 -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->
<!-- Dynamic 'fix' by: Nannette Thacker -->
<!-- Web Site: www.shiningstar.net -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! javascript.internet.com -->
<!-- Begin
function textCounter(field, countfield, maxlimit) {
if (field.value.length > 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 -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<!-- textCounter() parameters are: text field, the count field, max length -->
<center>
<form name=myform action="YOUR-SCRIPT.CGI">
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br>
<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);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="125"> characters left</font>
</form>
</center>
<p><center>
<font face="arial, helvetica" SIZE="-2">Free JavaScripts provided<br>
by <a href="javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 1.37 KB -->
</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>