Javascript – Disable backspace and delete key with javascript in IE


Anyone know how can I disable backspace and delete key with Javascript in IE? This is my code below, but seems it's not work for IE but fine for Mozilla.

onkeydown="return isNumberKey(event,this)"

function isNumberKey(evt, obj)

    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode == 8 || charCode == 46) return false;

    return true;

Best Answer

This event handler works in all the major browsers.

function onkeyup(e) {
    var code;
    if (!e) var e = window.event; // some browsers don't pass e, so get it from the window
    if (e.keyCode) code = e.keyCode; // some browsers use e.keyCode
    else if (e.which) code = e.which;  // others use e.which

    if (code == 8 || code == 46)
        return false;

You can attach the event to this function like:

<input onkeyup="return onkeyup()" />