👀 Live Preview
Submit with empty fields to see validation — valid input shows success:
Waiting for submit…

The onsubmit attribute is an inline event handler for the submit event on a <form> element. It runs when the user tries to send the form — by clicking a Submit button, pressing Enter in a text field, or when script calls form.requestSubmit(). Use it to validate data, show errors, or handle submission with JavaScript instead of a full page reload. Return false (inline) or call event.preventDefault() (listener) to cancel submission. Prefer addEventListener("submit", …) in production.
Inline JS.
Form sent.
Target element.
Block submit.
Before send.
Preferred way.
onsubmit AttributeThe primary purpose of onsubmit is to run custom logic before the browser sends form data to the server (or navigates to the action URL). Validate fields, show friendly error messages, transform values, or intercept submission for a single-page app with fetch.
If the handler returns false (inline attribute) or calls event.preventDefault(), the form does not submit and the page does not navigate. If it returns true or does nothing blocking, the browser continues with normal submission.
Unless the form has novalidate, the browser checks required, type="email", and other constraints before your onsubmit handler runs. Use custom validation for rules HTML cannot express.
Set onsubmit on <form>, or assign form.onsubmit in JavaScript:
<form action="/login" method="post" onsubmit="return validateForm()">
<input type="text" name="username" required>
<input type="submit" value="Submit">
</form>
<script>
form.onsubmit = validateForm;
form.addEventListener("submit", function (event) {
if (!isValid()) event.preventDefault();
});
</script><form> elements only.submit event fires.return false cancels submission; return true allows it.event.preventDefault() to cancel.button type="submit", Enter in a text field, or requestSubmit().form.onsubmit in script.addEventListener("submit", handler).The onsubmit attribute accepts a string of JavaScript code:
onsubmit="return validateForm()" — Call a function; return value controls submission.onsubmit="return false" — Always block submission (demo pages).onsubmit="event.preventDefault(); handleSubmit()" — Cancel then run custom logic.form.onsubmit = () => { …; return false; } — property assignment.function validateForm() {
var username = document.getElementById("username").value.trim();
var password = document.getElementById("password").value.trim();
if (username === "" || password === "") {
status.textContent = "Please fill in all fields";
status.setAttribute("role", "alert");
return false; // block submission
}
status.textContent = "Validated — sending…";
return true; // allow submission
}
form.addEventListener("submit", function (event) {
if (!isValid()) event.preventDefault();
});| Action | submit fires? | Notes |
|---|---|---|
Click input type="submit" | Yes | Classic submit button |
Click button type="submit" | Yes | Inside the form |
| Press Enter in text field | Yes | Implicit submit |
form.requestSubmit() | Yes | Runs validation + handlers |
form.submit() | No | Skips handlers — avoid for UX |
Click type="reset" | No | Fires reset instead |
| Element | Supported? | Notes |
|---|---|---|
<form> | Yes | Primary and correct target |
form.addEventListener("submit") | Yes | Recommended registration |
<input>, <button> | No | Put handler on parent form |
<button type="submit" form="id"> | Indirect | Button submits linked form — handler on that form |
<div> or other containers | No | Not a form event target |
onsubmit vs onreset vs native validation vs form.submit()| API / handler | When it fires | Typical use |
|---|---|---|
onsubmit | User sends the form | Validate, intercept, fetch API |
onreset | Form cleared to defaults | Feedback, clear custom UI |
Native required / type | Before submit handler | Built-in browser validation |
form.submit() | Immediate, no event | Programmatic send — skips handlers |
Login validation with return false, dynamic assignment, and addEventListener with preventDefault.
Submit with empty fields to see validation — valid input shows success:
Waiting for submit…
Return false to block invalid submissions:
<form onsubmit="return validateForm()">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<input type="submit" value="Submit">
</form>
<script>
function validateForm() {
if (username.value === "" || password.value === "") {
status.textContent = "Please fill in all fields";
return false;
}
return true;
}
</script>When the user clicks Submit, the browser fires submit on the form. Your function runs first; returning false stops the browser from navigating to the action URL.
Assign the handler on the form at runtime:
<script>
document.getElementById("dynamicForm").onsubmit = function () {
log.textContent = "Form submitted dynamically";
return false;
};
</script>Assigning form.onsubmit is equivalent to the inline attribute. Attach after the form exists in the DOM.
Use addEventListener and event.preventDefault() for modern form handling:
form.addEventListener("submit", function (event) {
event.preventDefault();
var email = emailInput.value.trim();
status.textContent = "Thanks! We saved: " + email;
// Later: fetch("/subscribe", { method: "POST", body: … })
});preventDefault() stops navigation. You then read form values and send them with fetch, show errors inline, or update the UI — all without a full page reload.
role="alert" or aria-live="polite" on validation messages.aria-describedby and aria-invalid="true" on invalid inputs.Button or Enter.
Unless novalidate.
onsubmit runs.
Navigate or preventDefault.
The submit event and onsubmit handler are supported in all browsers, including every modern browser and legacy Internet Explorer.
onsubmit supportEvery browser that supports HTML forms fires submit when a form is sent.
Bottom line: One of the most reliable form events — safe to use everywhere.
onsubmit on the <form> elementaddEventListener("submit", …) with preventDefault() in productionaria-liverequired with custom rules where neededrequestSubmit() instead of submit() when triggering from scriptalert() for validation feedbackform.submit() when you need handlers to runonsubmit on individual inputsfalse inside addEventListener — use preventDefault()The onsubmit attribute runs JavaScript when a form is submitted — useful for validation, user feedback, and handling data without a full page reload.
Return false or call preventDefault() to block submission, prefer addEventListener in production, and always validate on the server too.
onsubmitBookmark these before wiring form handlers.
Not inputs.
WhereBlock inline.
CancelIn listener.
ModernNative first.
PairSend vs clear.
Comparesubmit event fires — when the user sends a form via a Submit button or Enter key.return false. In addEventListener, call event.preventDefault(). Both prevent navigation to the action URL.onsubmit runs when the user sends form data. onreset runs when the form is cleared to its default values.form.submit() bypasses the submit event and handlers. Use form.requestSubmit() when you need validation and onsubmit to run.form.addEventListener("submit", handler) is preferred — use event.preventDefault() and attach multiple listeners if needed.Practice the onsubmit attribute with validation and preventDefault demos in the Try It editor.
5 people found this page helpful