Disable the Enter Key on Form Submission with JavaScript

Web Dev
Sponsored links

In this article, I will introduce how to disable the enter key on form submission with JavaScript.

Disable the enter key on form submission

There are several ways to prevent it from being submitted when the Enter key is pressed, but the most versatile method is to determine the key entered in the form.
Specifically, it disables the input of the Enter key.

The sample code looks like this:

document.getElementById("form").onkeypress = (e) => {
  // Get the key entered in form
  const key = e.keyCode || e.charCode || 0;
  // 13 is the key code for the Enter key
  if (key == 13) {
    // Disable the enter key