Subscribe Now!

Enter your email address:

Wednesday, December 22, 2010

How To: Disable Form Submit on Enter Key Press


The post Enter Key as the Default Button describes how to set the default behaviour for enter key press. However, sometimes, you need to disable form submission on Enter Key press. If you want to prevent it completely, you need to use OnKeyPress handler on  tag of your page.

The javascript code should be:

<script language="JavaScript">
function disableEnterKey(e)
{
     var key;    
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox    

     return (key != 13);
}
</script>
If you want to disable form submission when enter key is pressed in an input field, you must use the function above on the OnKeyPress handler of the input field as follows

<input disableenterkey(event)”="" name="”txtInput”" onkeypress="”return" type="”text”" />


0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...