Skip to content

Instantly share code, notes, and snippets.

@johnantoni
Created April 28, 2009 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnantoni/103343 to your computer and use it in GitHub Desktop.
Save johnantoni/103343 to your computer and use it in GitHub Desktop.
function killKeys() {
//stop backspace / escape / enter key
if (typeof window.event != 'undefined') // IE
document.onkeydown = function() // IE
{
var t=event.srcElement.type;
var kc=event.keyCode;
//alert('Type: ' + t);
return ((kc != 8 && kc != 13 && kc != 27) || (t == 'text' && kc != 13 && kc != 27) ||
(t == 'textarea' && kc != 27) || (t == 'button' && kc == 13) || (t == 'submit' && kc == 13) ||
(t == 'password' && kc != 27 && kc !=13) || (t == '' && kc == 13))
// Type = '' is what I get from the A HREF elements that need to remain functional in my form
}
else
document.onkeypress = function(e) // FireFox/Others
{
var t=e.target.type;
var kc=e.keyCode;
if ((kc != 8 && kc != 13 && kc != 27) || (t == 'text' && kc != 13 && kc != 27) ||
(t == 'textarea' && kc != 27) || (t == 'button' && kc == 13) || (t == 'submit' && kc == 13) ||
(t == 'password' && kc != 27 && kc !=13) || (t == '' && kc == 13))
// Type = '' is what I get from the A HREF elements that need to remain functional in my form
return true
else {
// alert('Sorry Backspace / Enter is not allowed here'); // Demo code
return false
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment