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!");
}
});