views:

4011

answers:

8

I just want to know how to prevent a 'ENTER' key pressed to submit a form in a web based application. I need a detailed answer.

A: 

You can trap the keydown on a form in javascript and prevent the even bubbling, I think. ENTER on a webpage basically just submits the form that the currently selected control is placed in.

Neil Barnwell
depends on a control tho ;) in a textarea it just moves to next line.So, catching all 'enter' keypress events is not a good idea.
kender
Possibly, but in that case would the form have a keydown event anyway?
Neil Barnwell
You would do it directly on all input-type-text and other similar controls. It's a bit of a pain and not really worth bothering with in most cases, but if you have to...
bobince
+5  A: 

The ENTER key merely activates the form's default submit button, which will be the first

<input type="submit" />

the browser finds within the form.

Therefore don't have a submit button, but something like

<input type="button" value="Submit" onclick="submitform()" />


EDIT: In response to discussion in comments:

This doesn't work if you have only one text field - but it may be that is the desired behaviour in that case.

The other issue is that this relies on Javascript to submit the form. This may be a problem from an accessibility point of view. This can be solved by writing the <input type='button'/> with javascript, and then put an <input type='submit' /> within a <noscript> tag. The drawback of this approach is that for javascript-disabled browsers you will then have form submissions on ENTER. It is up to the OP to decide what is the desired behaviour in this case.

I know of no way of doing this without invoking javascript at all.

DanSingerman
Weirdly this does not work in Firefox if there is only one field.
DanSingerman
-1. Terrible for accessibility.
bobince
Terrible for accessibility? Maybe. However it *does* answer they guy's question, which is what SO is for... Maybe an edit to the answer to point out the accessibility issue is a good compromise?
Neil Barnwell
@bobince - maybe the question deserves -1 due to accessibility, but this accurate answer to the question? Don't think so. Also, there are valid uses cases where you want to do this.
DanSingerman
You don't need to make form impossible to submit without JavaScript, though, eh. That's if it worked though, which it doesn't because browsers will happily submit a form that contains no submit-button.
bobince
@bobince - Have tested in Safari, Chrome, FF and IE - it works as I describe in all of them. How else can you solve the problem without javascript?
DanSingerman
Tested in all of those plus Opera with one text input and one input-button: all still submitted the form on Enter. You obviously need JavaScript to ‘solve’ the Enter issue, but the trick is to do that whilst not making the page completely inoperable where JS is unavailable.
bobince
@bobince - See my first comment. It works when there is more than one text field.You could put a submit button in a <noscript> tag if that was desired, but I'd say that is for the OP to decide
DanSingerman
+11  A: 

You will have to call this function whic will just cancel the default submit behaviour of the form. You can attach it to any input field or event.

function doNothing() {  
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if( keyCode == 13 ) {


 if(!e) var e = window.event;

 e.cancelBubble = true;
 e.returnValue = false;

 if (e.stopPropagation) {
  e.stopPropagation();
  e.preventDefault();
 }
}
hash
e should be an argument and then you should check for the window.event. Also, you should be getting the keycode from the event. Finally, you should return false.
ntownsend
+3  A: 
function checkEnter(e){
 e = e || event;
 return (e.keyCode || event.which || event.charCode || 0) !== 13;
}

Now you can define a keypress handler on the form:

<form [...] onkeypress="return checkEnter(event)">

(tested in FF3, IE7, Chrome)

KooiInc
By the way: you could add a check for the input type (via event source) in the checkEnter function to exclude textarea's.
KooiInc
A: 

nice post thanks you

+3  A: 

Simply return false from the onsubmit handler

<form onsubmit="return false;">

or if you want a handler in the middle

<script>
var submitHandler = function() {
  // do stuff
  return false;
}
</script>
<form onsubmit="return submitHandler()">
Paul Tarjan
+1  A: 

Here is a jQuery handler that can be used to stop enter submits, and also stop backspace key -> back. The (keyCode: selectorString) pairs in the "keyStop" object are used to match nodes that shouldn't fire their default action.

Remember that the web should be an accessable place, and this is breaking keyboard users expectations. That said, in my case the web application I am working on doesn't like the back button anyway, so disabling its' key shortcut is OK. The "should enter -> submit" discussion is important, but not related to the actual question asked.

Here is the code, up to you to think about accessability and why you would actually want to do this!

$(function(){
 var keyStop = {
   8: ":not(input:text, textarea, input:file, input:password)", // stop backspace = back
   13: "input:text, input:password", // stop enter = submit 

   end: null
 };
 $(document).bind("keydown", function(event){
  var selector = keyStop[event.which];

  if(selector !== undefined && $(event.target).is(selector)) {
      event.preventDefault(); //stop event
  }
  return true;
 });
});
thetoolman
This was tested in iE6-8 and FF3.5-win.
thetoolman
In a simplest way:$("#myinput").keydown(function (e) { if(e.which == 13) e.preventDefault(); });The key is to use "keydown" and event.preventDefault() together. It doesn't work with "keyup".
lepe
A: 
stopSubmitOnEnter (e) {
  var eve = e || window.event;
  var keycode = eve.keyCode || eve.which;

  if (keycode == 13) {
    eve.cancelBubble = true;
    eve.returnValue = false;

    if (eve.stopPropagation) {   
      eve.stopPropagation();
      eve.preventDefault();
    }
  }
  return false;
}

Then on your form:

<form id="foo" onkeypress="stopSubmitOnEnter(e);">
ntownsend