I'm playing with a textarea and trying to make it so the user cannot exceed maxLength unless the key pressed is a backspace or delete key. The problem is my keyCode conditional is always coming out to true. Why is that?
var maxLength = 500;
window.onload = init; // this seems to be a problem
commentBox.onkeydown = checkLength;
function checkLength(e)
{
var evt = e || window.event;
if(countText() < maxLength) //countText successfully returns number of nonwhitespace text.
return true;
else if(evt.keyCode == 46 || evt.keycode == 8) // 46 = delete, 8 = backspace.
return true;
else
return false;
}
Here's a JSFiddle to help better understand. The delete key works in JSFiddle but not locally for some reason or another. Delete on the otherhand still does not work.
Found it finally....
You are using evt.keycode which is a small typo error
else if(evt.keyCode == 46 || evt.keyCode == 8) // 46 = delete, 8 = backspace.
Cheers