views:

56

answers:

3

I replaced the type="submit" on my form with a type="button", to prevent form submits when the user presses enter. Will this prevent enter from submitting the form in all browsers?

+2  A: 

This is a poor decision for usability and should be avoided.

Delan Azabani
See my above comment explaining why it make sense for enter not to be used to submit
babonk
@babonk it doesnt make sense and would irritate me if i was trying to use your form
Chris
+1  A: 

It's seems not. This question suggests that submission will still happen in Chrome.

Alohci
A: 

Some browsers will still automatically submit when enter is pressed. If you really need some feature apparented to this, you might rather implement your own filtering with the onSubmit event handler.

Then, you can force the submission of the form by actually calling the submit method from Javascript. Of course, you would need to set a specific flag to allow your submit to go through this time. Something along the lines of (with jQuery)

//flag definition
var form_is_ready = false;

//event handler for the form submission
$('#your-form-id').submit(function() {
  if (!form_is_ready) {
    return false;
  }
});

//function you have to call to actually submit the form
function do_submit() {
  form_is_ready = true;
  $('#your-form-id').submit();
}

That's just a crack shot piece of code written on the fly. You should adapt it to suit your needs.

Guillaume Bodi