Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tracking use of the Backspace in a Textarea box 1

Status
Not open for further replies.

Scanlan

Technical User
Sep 7, 2001
35
US

Hello,

I have a text area box that I want to limit to 500 characters. I have a little Javascript function that is triggered by the textarea's onkeypress event. The event adds one to a global variable and when the variable reaches 501, an alert box pops up.

That all works well, but I need to account for the user pressing the backspace key. Right now, my code looks like this, but the backspace if statement seems not to be working:

function countcomchar()
{
//check for backspace, decrease counting var by 2
if (window.event.keyCode==8){
charcount = charcount-2}
else{
//if not backspace, add one to counting var
charcount = charcount+1;
}

//when counting var reaches 501, give a warning
if (charcount > 501){
var mesg
mesg = "You exceeded 500 characters."
alert(mesg);
}
}

Any help would be much appreciated.

Thanks,
Ann

 
um...no that won't work...what would happen if the user held backspace or highlighted and typed over..or used insert??? you would need to use onChange="checkLength()"
Code:
var msg = "msg = You exceeded 500 characters.";
var charCount;
function checkLength() {

     //load contents to var
     var charCount = document.form.textArea.value.length;

     //check length
     if (charCount == 499 || charCount > 499) {

          //alert
          alert(msg);
     }
}

-Greg :-Q

flaga.gif
 
yea, & i think she should use this function onkeyup as well.. Victor
 
Thanks very much, that worked. I'm using it for onkeypress, onkeyup, onkeydown, and onkeychange.

Thanks,
Ann
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top