Form Validation
The browser can check a lot of form input for you before any JavaScript runs. Native validation is fast, accessible and free.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 1, 2026
Learning objectives#
- Use required, minlength, maxlength, min, max and pattern
- Choose input types that validate automatically
- Style valid and invalid fields
- Understand why server-side validation is still required
Explanation#
HTML input types such as email, url and number come with built-in validation. Attributes like required and minlength add further rules.
When the user submits, the browser checks every field. If one fails, submission is blocked and a message is shown next to the first invalid field.
Client-side validation is a convenience for users, not a security measure. Anyone can bypass it, so your server must always validate the data again.
Syntax#
<input type="email" name="email" required>
<input name="username" minlength="3" maxlength="20" pattern="[a-z0-9_]+">
Code example: A sign-up form with native validation#
<style>
label { display:block; margin-top:12px; font-family:sans-serif; }
input:user-invalid { border:2px solid #dc2626; }
input:user-valid { border:2px solid #16a34a; }
</style>
<form>
<label for="name">Username (3–20 lowercase letters or digits)</label>
<input id="name" name="username" required minlength="3" maxlength="20" pattern="[a-z0-9]+">
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="age">Age</label>
<input id="age" name="age" type="number" min="13" max="120">
<p><button>Create account</button></p>
</form>
Submitting with empty or invalid fields shows browser error messages; fields turn red or green after you interact with them.
Step-by-step explanation#
- Each <label for> is linked to an input id, so clicking the label focuses the field and screen readers announce it.
- required blocks submission while the field is empty.
- minlength, maxlength and pattern restrict the username. pattern is a regular expression the whole value must match.
- type="email" checks for a plausible email address.
- min and max on a number input enforce a range.
- :user-invalid only applies after the user has interacted, so the form does not start out red.
Real-world use case#
Registration, checkout and contact forms all use native validation as the first line of feedback, with server-side validation behind it.
Common mistakes#
Validation in the browser can be disabled with developer tools. Always re-validate on the server.
Placeholders disappear as you type and are often low-contrast. Use a visible <label>.
Best practices#
- Pair every input with a <label>
- Use the most specific input type
- Explain the rule in the label or hint text, not only in the error
- Validate again on the server
Remember this#
required, type="email", min/max, pattern.
Browser validation helps users; the server must validate again.
Every input needs a <label>.
Practice exercise#
Your task: Add a password field that is required, at least 8 characters long, and a “confirm password” field.
Need a hint?
Show solution
<label for="pw">Password (at least 8 characters)</label>
<input id="pw" name="password" type="password" required minlength="8" autocomplete="new-password">
<label for="pw2">Confirm password</label>
<input id="pw2" name="confirm" type="password" required minlength="8" autocomplete="new-password">
Mini quiz#
If a form uses HTML validation, the server does not need to validate the data.
Want more? Practise all HTML MCQs · HTML interview questions
My notes#
Saved in this browser.