4

Quick question.

How do you disable Enter Key when you submit the form?

is there a way in PHP or Javascript?

mistysnake
  • 113
  • 1
  • 2
  • 8

2 Answers2

14
$('input').on('keydown', function(event) {
   var x = event.which;
   if (x === 13) {
       event.preventDefault();
   }
});
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
  • 1
    Works. I needed this for an AJAX form that doesn't have any kind of "Submit" functionality. Without this, pressing "Enter" has the unexpected effect of refreshing the whole page. – PJ Brunet Feb 01 '17 at 21:17
1

Simplest solution:
Don't use a submit-button in the form, but use a normal button.
Using Enter/Return acts as pressing the Submit button

By the way, I personally hate forms that don't allow me to use Enter/Return.
It just feels annoying and slow if you need to grab your mouse to press Submit

Also don't forget that using Enter/Return to submit a form is part of its accessibility. By declining users to submit a form this way, you're negatively impacting people in need of these accessibility features.

BlueCacti
  • 9,729
  • 3
  • 20
  • 24
  • If you dont have too many input fields in the form, e.g. I had only 2, you can add to each input field: onkeypress="javascript:return (event.keyCode != 13) – Peter Apr 15 '15 at 11:50
  • Why would you use JavaScript to disable basic functionality of a form? Also, if you ever forget to add it to an input field, you can still Submit using enter. – BlueCacti Jan 12 '18 at 23:18