Prevent enter to submit form / disable enter I am trying to prevent to submit the form when a user is pressing enter. I am trying to do so in a form that is also using ajax. I want to have the user click on submit after filling out all the necessary stuff and not have them accidentally press enter and have the form submitted.

I have the code below; the alertbox is popping up when pressing 's' for example. also when I press enter I can get a alertbox popping up. But it is not preventing to submit the form.

$("#edit-searchfield").keydown(function(e){
  var code = e.which; // recommended to use e.which, it's normalized across browsers
  if (code == 13) {
    e.preventDefault();
    return false;
  }
  if (code == 83 || code == 13 || code == 188 || code == 186) {
    alert("I am an alert box!");
  }
});
And the answer is :-)

$form['#attributes'] = array('onsubmit' =&gt; 'return false');


or alternatively (the above will disable submit all together)

  $('#edit-searchfield').keypress(function(event) {
   if (event.keyCode == '13') {
       event.preventDefault();
   }
});