Last updated: August 25, 2026.
Pressing Enter in a form can trigger its default submit button. Keep that behavior for most forms: keyboard submission is efficient and expected. If one specific field causes accidental or costly submissions, block Enter only for that field.
Mark only the fields that need protection
<form id="profile-form" action="/save-profile" method="post">
<label for="display-name">Display name</label>
<input id="display-name" name="display_name"
data-prevent-enter-submit>
<label for="notes">Notes</label>
<textarea id="notes" name="notes"></textarea>
<button type="submit">Save profile</button>
</form>The textarea and submit button keep their normal keyboard behavior. The custom data attribute makes the exception visible in the markup.
Prevent Enter on the marked controls
const form = document.querySelector('#profile-form');
form.addEventListener('keydown', (event) => {
if (event.key !== 'Enter' || event.isComposing) {
return;
}
const control = event.target;
if (control.matches('[data-prevent-enter-submit]')) {
event.preventDefault();
}
});The handler uses event.key and ignores keyboard composition events. It is attached to the form rather than the whole document, so unrelated forms and controls are unaffected.
Cases where Enter should continue to work
- Textareas, where Enter inserts a new line.
- Buttons and submit controls activated from the keyboard.
- Search boxes, login forms, and other short forms where Enter is the expected submit action.
- Controls used with an input method editor while text composition is active.
Prefer form design over global key blocking
- Use
type="button"for buttons that should not submit. - Use a confirmation step for destructive actions.
- Use HTML validation attributes and server-side validation before saving.
- For multi-step forms, make only the final action a submit button.
The browser’s behavior is defined as implicit form submission. Do not make Enter imitate Tab; that changes familiar keyboard navigation and can make the form harder to use.