11

I have a form with a disabled submit button. Even though the user can't press this button, he can still hit Enter to submit the form. How do I prevent that?

akamike
  • 2,148
  • 13
  • 16
Pieter
  • 31,619
  • 76
  • 167
  • 242
  • 4
    Possible Duplicates: http://stackoverflow.com/search?q=prevent+submit+form+enter , http://stackoverflow.com/questions/923683/prevent-users-from-submitting-form-by-hitting-enter-2 , http://stackoverflow.com/questions/895171/prevent-users-from-submitting-form-by-hitting-enter , http://stackoverflow.com/questions/1906376/safari-disable-form-submit-on-enter – NibblyPig May 10 '10 at 13:23

7 Answers7

31

If you want to completely disable the form submit (However I am wondering why the whole <form> element is then there in first place), then you need to let its submit event handler return false.

So, basically:

<form onsubmit="return false;">

You can add it using Javascript/DOM manipulation during onload as previous answerers pointed out.

If you only want to disable the Enter key to submit the form, then you need to let its keydown event handler return false when the key of the event matches Enter (this one is crossbrowser compatible and covers both the default Enter as well as the numpad Enter!).

<form onkeypress="return event.key != 'Enter';">

This however also disables the Enter key in any <textarea> elements in the form. If you have any of them and you would like to keep them functioning, then you'll need to remove the onkeydown from the <form> and copy it over all <input> and <select> elements. jQuery can be helpful in this:

$('input, select').keydown(event => event.key != 'Enter');

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
10

Assuming HTML:

<form id="myForm"> ...

You can do this with JavaScript:

document.getElementById("myForm").onsubmit = function () {
    return false;
};
Matt
  • 43,482
  • 6
  • 101
  • 102
7

Instead of implementing two methods of disabling form submitting, add an event handler to form's onsubmit which will check the disabled property of the button:

myForm.onsubmit = function () { 
    if (myForm.mySubmit.disabled) 
        return false;
}
Andy E
  • 338,112
  • 86
  • 474
  • 445
3

jQuery Solution:

$(document).ready(function(){
  $('form[name="form"]').keypress( function(evt){
    return !(evt.which==13 && evt.target.type!='textarea');
  });
  $('form[name="form"] input[type="submit"]').attr('tabIndex',-1); // this prevents submit also by spacebar (keyCode==32)
  //$('form[name="form"]').submit(function(){ alert('test'); return false; }); // test
});
Stano
  • 8,749
  • 6
  • 30
  • 44
  • Thanks for posting to SO. Next time, adding some explanatory text would be helpful. Just a few lines explaining what the code is doing. Like why setting tabIndex to -1 prevents the spacebar from submitting – Devon_C_Miller Jun 18 '12 at 12:24
  • Thanks Devon, I'll give it more description next time. – Stano Jul 07 '12 at 19:27
3

Say you had a form with an id of "myForm":

<form id="myForm" action="some_file" method="post">
   ...
</form>
<script type="text/javascript">
    document.getElementById( "myForm").onsubmit = function() {
          return false;
    };
    //or with jQuery: $( '#myForm' ).submit( function() { return false; } );
</script>
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • If you're putting ` – Matt May 10 '10 at 13:26
  • Eh, how many times did you edit this answer? I lost the count. First it was an oneliner, then a copypaste of @Matt, then a window.onload, then code formatting, then jQuery. Did I miss some more? :) – BalusC May 10 '10 at 13:28
  • @BalusC, um I never copy-pasted. Just happens to be that `myForm` was the same `id` that we used. :) – Jacob Relkin May 10 '10 at 13:34
3

If you don't want to edit the onsubmit event of the form, you can do it like this.
Add this to the text input:

onKeyPress="return noEnter(event)"

And this to the header:


    function noEnter(e)
    {
        var key;
        // firefox vs. ie
        (window.event) ? key = window.event.keyCode : key = e.which;
        (key == 13) ? return false : return true;
    }

easier to pull of with jquery.

Dänu
  • 5,791
  • 9
  • 43
  • 56
1
document.getElementById("myForm").onsubmit = function () {
    return false;
};

These codes doesn't work on Chrome and Safari. It submits form to action url.

But <form onsubmit="return false;"> is useful.

sundowatch
  • 3,012
  • 3
  • 38
  • 66