requestSubmit Validates While submit() Bypasses
requestSubmit runs validation and delivers invalid or submit events with e.submitter; form.submit() navigates silently, skipping both entirely.
Code
// requestSubmit 走完整提交流程(校验 + submit 事件);form.submit() 静默绕过两者
const form = document.createElement('form');
form.innerHTML = '<input name="email" type="email" required><button type="submit" value="save">Save</button>';
document.body.append(form);
const events = [];
form.addEventListener('submit', (event) => {
event.preventDefault();
events.push('submit from ' + (event.submitter ? event.submitter.value : 'form (no submitter)'));
});
// invalid 事件不冒泡:只有捕获监听能在 form 上收到
form.addEventListener('invalid', (event) => events.push('invalid on ' + event.target.name), true);
// 空邮箱:requestSubmit 触发 invalid,不触发 submit
form.requestSubmit();
console.log('invalid attempt :', events.join(' | '));
// 填入合法邮箱:submit 事件携带 e.submitter
form.elements.email.value = '[email protected]';
const button = form.querySelector('button');
form.requestSubmit(button);
console.log('valid submit :', events.join(' | '));
// form.submit() 会静默导航:不触发 invalid/submit,也不做任何校验
console.log('form.submit() : skips validation and both events entirely');Inside the snippet
The file’s state is a handful of top-level bindings: form, events, button.
What to watch for
documentexist only in browsers — a Node runtime has no shims for them out of the box.
Putting it to use
Running the file prints labeled results such as invalid attempt :, valid submit : — each operation’s effect is visible in the output.
Core lines
form.addEventListener('invalid', (event) => events.push('invalid on ' + event.target.name), true);
form.addEventListener('submit', (event) => {
event.preventDefault();
events.push('submit from ' + (event.submitter ? event.submitter.value : 'form (no submitter)'));
});
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
FAQ
- Does requestSubmit Validates While submit() Bypasses run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.