HTML · Forms

Form Validation

The browser can check a lot of form input for you before any JavaScript runs. Native validation is fast, accessible and free.

Intermediate 5 min Lesson 12 of 22 Runnable examplesFollows the WHATWG HTML Living Standard.
Before you start:FormsInput Types
0/22 in HTML
On this page · 5 min · Intermediate

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#

HTML Illustrative
<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#

HTMLA sign-up form with native validation Runnable
<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>
Expected output
Submitting with empty or invalid fields shows browser error messages; fields turn red or green after you interact with them.
Opens this example in the full playground

Step-by-step explanation#

  1. Each <label for> is linked to an input id, so clicking the label focuses the field and screen readers announce it.
  2. required blocks submission while the field is empty.
  3. minlength, maxlength and pattern restrict the username. pattern is a regular expression the whole value must match.
  4. type="email" checks for a plausible email address.
  5. min and max on a number input enforce a range.
  6. :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#

Relying only on client-side checks

Validation in the browser can be disabled with developer tools. Always re-validate on the server.

Placeholder instead of label

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?
Matching two fields needs JavaScript — HTML can only check each field on its own.
Show solution
HTML Illustrative
<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#

Form Validation quizTrue or false Question 1 of 2

If a form uses HTML validation, the server does not need to validate the data.

Score so far: 0/0

Want more? Practise all HTML MCQs · HTML interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.