?
Solved

Setting TextArea Character Limit

Posted on 2003-03-28
5
Medium Priority
?
1,672 Views
Last Modified: 2013-12-20
Ok, Is there a way to set a character limit
on a textarea form element?  Is there a way
to check if the entered text exceeds a character
limit?
0
Comment
Question by:rodnice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
danrosenthal earned 320 total points
ID: 8228052
You can verify the text entered, but you cannot pre-set a limit.  Here is how you can verify client-side:

<SCRIPT language="JavaScript">
var fieldmaxlength = 10;
function chkchars(){
     if(document.myform.testfield.value.length>fieldmaxlength){
          alert('You have exceded the maxmimum of '+fieldmaxlength+' characters');
          document.myform.testfield.select();
     }
}

function chkform(){
     if(document.myform.testfield.value.length>fieldmaxlength){
          chkchars();
          return false
     }
}

</script>


<FORM name="myform" onsubmit="return chkform();">
     <TEXTAREA name="testfield"  onkeyup="chkchars();"></textarea>
     <INPUT type="Submit">
</form>

It is also a good idea to verify on you form processing page as well to guard against hackers and those who have javascript disabled.  To do this you just need a simple code like this:

<CFIF len(testfield) gt 10>
  -- display error, prevent submit, etc. --
</CFIF>
0
 

Author Comment

by:rodnice
ID: 8228100
Ok, I'll try this and get back to you. THANX!
0
 

Author Comment

by:rodnice
ID: 8228637
SUPERB ANSWER!!!!
0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 8228678
thanks!
0
 
LVL 17

Expert Comment

by:anandkp
ID: 8378744
hi,

How abt running this script on a html page & see how it goes !!!


<HEAD>
<SCRIPT LANGUAGE="JavaScript">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;
}
</SCRIPT>
</HEAD>
<!--- ========================================================================================================== --->
<BODY>
<CENTER>
<FORM NAME="MYFORM" ACTION="anandkp.cfm"><FONT SIZE="1" FACE="verdana">( 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>
</BODY>

K'Rgds
Anand
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question