How to prevent default button from firing
In my previous Tips and Tricks blog entry I talked about a workaround for ASP.NET not submitting when the Enter key is pressed on the keyboard. Now in this blog entry lets talk about how to prevent the button from submitting the form when you press ENTER key on the keyboard. Its very simple.. we need to capture the keycode from the onkeypress event of the form and return false
This is the code to capture the ENTER keycode...
<script language="javascript">
function test()
{
if (window.event.keyCode == 13)
{
window.event.cancelBubble = true;
window.event.returnValue = false;
}
}
</script>
This is the code to for the form wide onkeypress event.
<form id="Form1" onkeypress="test();" method="post" runat="server">
You could customize this.. If you want to prevent the button to submit only for a particular textbox, you could just capture the onkeypress event for that textbox and but not the whole form