[Javascript] Q on interuppting char entry

Walter Torres walter at torres.ws
Fri Apr 11 16:50:41 CDT 2003


I am trying to create a method that limits the character count in a
TEXTAREA, like MAXLENGTH does in TEXT and PASSWORD.

What I have works (see below), to a point.

What it does is allow another character to flash in the box, and then it is
removed.

Anyone have any idea on how to interrupt this character entry at this point,
thus no character flash?

Thanks

Walter

=================

var objTA = [favorite method to get Element Reference]

objTA.onkeypress = setLimit;
objTA.onkeydown  = setLimit;
objTA.onkeyup    = setLimit;

function setLimit ( )
{
   // Get name of given TEXTAREA
   var strName = this.id;

   // What is the limit set at for this TEXTAREA
   var intLimit = new Number ( this.getAttribute("maxlength") );

   // How many characters do we have yet?
   var intCharRemaing = new Number ( intLimit - this.value.length );
   if ( intCharRemaing < 0 )
   {
      this.value = this.value.substring( 0, intLimit  );
      intCharRemaing = 0;
   }
}



More information about the Javascript mailing list