Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1679
  • Last Modified:

Setting TextArea Character Limit

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
rodnice
Asked:
rodnice
  • 2
  • 2
1 Solution
 
danrosenthalCommented:
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
 
rodniceAuthor Commented:
Ok, I'll try this and get back to you. THANX!
0
 
rodniceAuthor Commented:
SUPERB ANSWER!!!!
0
 
danrosenthalCommented:
thanks!
0
 
anandkpCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now