Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1693 Is it possible to stop a form from submitting when enter is pressed?

You are here: irt.org | FAQ | JavaScript | Frame | Q1693 [ previous next ]

Yes - The following code will block the enter key in all browsers 4.0 above, except when enter is pressed in a Textarea or on the Submit button itself.

<script language="JavaScript"><!--
var nav = window.Event ? true : false;
if (nav) {
   window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
} else {
   document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) {
  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false; }
  return true;
}

function MicrosoftEventHandler_KeyDown() {
  if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')
    return false;
  return true;
}
//--></script>

Submitted by Paul Ellender

Feedback on 'Q1693 Is it possible to stop a form from submitting when enter is pressed?'

©2018 Martin Webb