Block Key Press Input In TextBox

At times we need to have some key blocked in the textbox field as a validation. Let's see how we can block enter key using JavaScript or jQuery.

Here is a function stopRKey which is capturing the key event, if it is found to be enter key event then it'll return false.

function stopRKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
}

document.onkeypress = stopRKey;

Here evt.keyCode == 13 is checked in if condition where 13 is decimal value for Enter Key / Carriage Return.

If you want to perform the same with any other key can do it by changing the decimal value.

Please refer to the chart below,

Ebook Download
View all
Learn
View all