HTML Required Fields and Constraint Validation
Require a value
The required boolean attribute makes an empty supported control invalid. A required checkbox must be checked; a required radio group needs a selection. The browser normally checks constraints before interactive form submission.
The empty field has valueMissing; typing a name satisfies this constraint.
Inspect validity without submitting
ValidityState exposes individual failure flags. checkValidity() returns a boolean and fires invalid on an invalid control; reportValidity() can additionally display native feedback. The following small JavaScript preview reads properties and writes a result into output.
<label> Email <input id="email" type="email" value="wrong"></label><output id="result"></output><script> const field = document.getElementById("email"); document.getElementById("result").textContent = field.validity.typeMismatch;</script>The output is true because the nonempty value is not a valid email syntax.
Custom validity must be cleared
setCustomValidity(message) marks a control invalid when the message is nonempty. Clear it with an empty string once the condition is fixed. This JavaScript example shows both states without a network request.
<label> Code <input id="code" value="A12"></label><output id="result"></output><script> const field = document.getElementById("code"); field.setCustomValidity("Code unavailable"); const before = field.validity.customError; field.setCustomValidity(""); document.getElementById("result").textContent = before + " / " + field.validity.valid;</script>Output: true / true. The custom error existed, then the field became valid.
Common mistakes and practice
Use required, not required="false" (presence still means true). Native email syntax does not prove an address exists. A form with novalidate skips interactive submission checks, not all validity APIs. Practice changing the value and predict the flag.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Required Fields and Constraint Validation — Knowledge Check
Apply the control concepts taught in this lesson.
Which flag identifies an empty required text field?
What does required="false" mean?
What does checkValidity() return?
How do you clear a custom error?
What does valid email syntax prove?