đź§© Fix a Form’s required Attribute Skipped on Enter
Pressing Enter inside a text input triggers the browser’s implicit form submission, which normally runs the same constraint validation as clicking a submit button — but when a form has multiple buttons, or a plain `<button>` with no explicit `type` elsewhere in the markup, browsers resolve the Enter key to whichever button the spec treats as the form’s default, and if THAT button carries `formnovalidate` (common on a “Save Draft” action), pressing Enter submits the form through a path that was never meant to skip validation, bypassing every `required` field’s check that a deliberate click on the real submit button would have enforced.
🔎 The Problem
<form id="signup">
<input type="text" name="email" required>
<!-- A hidden, always-present, non-formnovalidate submit button
guarantees Enter resolves to a path that still validates -->
<button type="submit" style="position:absolute;left:-9999px;"
aria-hidden="true">Submit</button>
<button type="submit" formnovalidate id="save-draft">Save Draft</button>
</form>
âś… Fix: Give Enter a Button That Still Validates
- Make sure exactly one real `<button type=”submit”>` without `formnovalidate` comes before any other button in DOM order, since that’s the one browsers resolve Enter-key implicit submission to — a visually hidden one works fine if your visible submit button is actually later in the markup.
- Explicitly mark any non-validating action (“Save Draft”, “Cancel”) with `type=”button”` or `formnovalidate` rather than leaving its `type` to default, since a plain `<button>` inside a `<form>` defaults to `type=”submit”` and can silently become the Enter-key target.
- Add a `submit` event listener that itself calls `form.reportValidity()` as defense-in-depth, so even if a browser’s default-button resolution differs from what you expect, validation still runs before anything reaches the server.
🚨 Why This Is Dangerous
- A signup or payment form that skips `required` validation only on Enter, not on a deliberate button click, is exactly the kind of inconsistency manual testing misses — most testers click the button, while a meaningful share of real users just press Enter.
The browser’s idea of your form’s default button and your idea of it aren’t guaranteed to be the same button.
