Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Frame | Q1693 [ previous next ]

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

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.