A submit button can stay disabled until the required form conditions are satisfied. Recheck those conditions every time the user changes relevant fields, because a form that was valid can become invalid again.
const submitButton = document.getElementById("btnSignUp");
submitButton.disabled = !isFormValid();
Try the submit-button validation demo
The original example required a name, gender selection, game selection, and agreement checkbox. The same teaching case is retained:
<form id="signupForm">
<input id="t1" name="FirstName" required>
<select id="s1" name="games" required>...</select>
<input type="checkbox" id="c1" required>
<button type="submit" id="btnSignUp" disabled>Sign Up</button>
</form>
function isFormValid() {
const nameOk = document.getElementById("t1").value.trim().length >= 3;
const genderOk = document.querySelector('input[name="gender"]:checked') !== null;
const gameOk = document.getElementById("s1").value !== "";
const agreed = document.getElementById("c1").checked;
return nameOk && genderOk && gameOk && agreed;
}
Instead of assigning several old-style onclick/onkeyup properties from window.onload, listen for input and change events on the form.
const form = document.getElementById("signupForm");
const submitButton = document.getElementById("btnSignUp");
function updateSubmitState() {
submitButton.disabled = !isFormValid();
}
form.addEventListener("input", updateSubmitState);
form.addEventListener("change", updateSubmitState);
The controls can still be accessed individually with getElementById() when that is the clearest option.
Disabling the button improves the interface, but it must not be the only validation gate. A script can enable controls, trigger submission programmatically, or be bypassed. Check the state again in the submit handler.
form.addEventListener("submit", (event) => {
if (!isFormValid()) {
event.preventDefault();
message.textContent = "Complete all required fields.";
}
});
form.addEventListener("reset", () => {
submitButton.disabled = true;
});
See the separate JavaScript form reset tutorial for how reset() restores default values.
Attributes such as required, minlength, type="email", min, max, and pattern can handle many common constraints without custom JavaScript. JavaScript can then add interface behavior instead of recreating every browser validation feature.
Client-side checks improve feedback and reduce avoidable submissions, but the server must validate submitted data independently. Never trust the browser as the final authority for security, database writes, authorization, or business rules.
JavaScript Validation Validation of dropdown ListboxAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Steffan | 04-08-2010 |
| And what do i have to change if I use a textarea :D ? | |
| Mashal Khan | 25-05-2012 |
| very knowledgeable and helpful for JavaScript coders. Excellent. Fantastic. | |
| Vvk | 11-02-2013 |
| Really helped! Thanks!! | |
| riya | 14-07-2015 |
| helped a lot | |
| Hirdey | 08-09-2015 |
| Working on demo. But not working on my site. Please help me | |
| fashn | 30-06-2016 |
| Thanks - it's just what I was looking for! | |