Home  »  Code Snippets  »  Java Script  »

How to Disable the Enter Key

 Posted By : Manoranjan SahooPosted Date : 07/07/2011 01:42:35 PM Category : Java Script Points : 3

Sometimes, we need to disable the submission of a form on Enter Key press. In this codesnippet you will find the code to disable the form submission on Enter/Return Key press.
First write the below javascript function before </head> tag.


<script language="JavaScript">
function disableEnterKeyPress(e)
{
    var key;   
    if(window.event)
        key = window.event.keyCode;
    else
        key = e.which;   
    return (key != 13);
}
</script>


If you want to disable it completely for whole page, then you need to use OnKeyPress handler on <body> tag of your page.
Ex:

<body OnKeyPress="return disableKeyPress(event);">


If you want to disable it when enter key is pressed in textbox field, then you need to use OnKeyPress handler on that textbox.
Ex:

<asp:TextBox id="TextBox1" runat="server" Text="" OnKeyPress="return disableKeyPress(event);"/>

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.