Sunday, July 17, 2011

JavaScript: Enable/Disable Form submission on enter key press

We can enable or disable form submission when enter key is pressed using JavaScript OnKeyPress handler. The OnKeyPress event handler executes the specified JavaScript code or function on the occurance of a KeyPress event.  A KeyPress event occurs when the user presses or holds down a key.

JavaScript source code to enable form submission when enter key is pressed


<script language="JavaScript" type="text/JavaScript">
document.onkeypress = stopRKey;
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)  {
                           //submit form
                        document.forms[0].submit();
              }
}
</script>

JavaScript source code to disable form submission when enter key is pressed


<script language="JavaScript" type="text/JavaScript">
document.onkeypress = stopRKey;
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)  {
                           //disable form submission
                           return false;
              }
}
</script>


0 Responses to “JavaScript: Enable/Disable Form submission on enter key press”

Post a Comment

© 2011 a2ztechguide.com • All rights reserved.
Blogger Template by Bloggermint