HTML · The document layer
How does native form validation work?
Short answer
Native validation uses attributes such as required, type, pattern, min and max, plus the Constraint Validation API. Always repeat validation on the server and make error messages clear and associated with the field.
The browser can block submission and show a message when a control violates its constraints. That gives a basic layer of validation before any JavaScript runs, but it is a convenience, not a security boundary.
<form>
<label for="username">Username</label>
<input
id="username"
name="username"
required
minlength="3"
maxlength="20"
pattern="[a-z0-9_]+"
/>
<label for="age">Age</label>
<input id="age" name="age" type="number" min="18" max="120" />
<button type="submit">Create account</button>
</form>The constraint API
- checkValidity() returns whether a control or form satisfies its constraints
- reportValidity() shows the browser messages
- validity exposes flags such as valueMissing, typeMismatch and patternMismatch
- setCustomValidity() lets you attach a custom message, then clear it when fixed
- novalidate on the form disables native validation when you need full control
Native messages are not consistent across browsers and may not be translated well. For critical forms, provide inline error text that is linked with aria-describedby and announced when it appears.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Is client-side validation enough?
- 2What does setCustomValidity do?
- 3When would you use novalidate?
- 4How do you associate an error message with an input?
Say it in the interview
Native validation uses attributes like required, pattern and min or max, and the browser blocks submission when constraints fail. The Constraint Validation API lets me inspect validity and set custom messages. I treat this as a usability layer only, because it can be bypassed, so the server always validates again. I also make sure errors are visible and linked to the input, not just shown in a transient browser bubble.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live HTML mock interview and get scored feedback on depth, clarity and confidence.