Hey all,
I'm writing an event listener that is trapping for certain keys (single quote & ampersand mainly) in a text input field.
I'm able to determine the key press through the keyCode. If an ampersand or single quote comes up, I give an alert box that those entries are not valid.
However, after the alert, it still writes the single quote or ampersand into text field.
I tried to get the length of the field and do a substring of the length -1, hoping to kill the ampersand or single quote. But it doesn't work. It cuts off a legitimate character and then writes in the single quote or ampersand.
I'm suspecting there's some sort of key buffer, but I can't seem to locate information on the web.
Here is the function code. I only wrote it for the ampersand.
function checkText(evt,lcID)
{
var charCode = (evt.which) ? evt.which : event.keyCode
switch(charCode)
{
case 38:
{ alert("Ampersands are not allowed");
var lcText = document.getElementById(lcID).value;
var lnLen = document.getElementById(lcID).value.length;
lnLen = lnLen-2;
document.getElementById(lcID).value = lcText.substr(0,lnLen);
break; }
case 39:
{ alert("Single quotes are not allowed");
var lcText = document.getElementById(lcID).value;
break; }
default: { break; }
}
}
Hope anyone can shed some light.
Thanks
Donna
