Browsers can check form input for you, before anything is sent to the server. This is called client-side validation. It needs no JavaScript, gives immediate feedback and costs just a few attributes.
required#
The simplest rule: the field must not be empty.
<label for="name">Name</label>
<input type="text" id="name" name="name" required>If the user tries to submit with this field empty, the browser blocks the submission, focuses the field and shows a message. For checkboxes it means "must be ticked", for radios "one must be selected", and for selects "an option with a non-empty value must be chosen".
Type-based validation#
Some types validate themselves:
<input type="email" name="email" required>
<input type="url" name="website">
<input type="number" name="guests" min="1" max="8">An email field rejects values that do not look like an address, and a number field rejects letters.
Length limits#
<input type="text" name="nickname" minlength="3" maxlength="20">
<textarea name="message" minlength="10" maxlength="500"></textarea>maxlength stops the user from typing more, while minlength rejects shorter entries on submission.
Number and date limits#
min, max and step apply to numbers, ranges and dates:
<input type="date" name="booking" min="2025-01-01" max="2025-12-31">
<input type="number" name="tip" min="0" step="5">pattern: custom rules#
The pattern attribute accepts a regular expression the whole value must match. Pair it with a title that explains the rule in plain words, because the title is shown in the error message.
<label for="code">Loyalty card number</label>
<input type="text" id="code" name="code"
pattern="[A-Z]{3}-[0-9]{4}"
title="Three capital letters, a hyphen, then four digits, like BRW-1234"
required>Use pattern only when no input type already covers your need, and keep regular expressions simple.
Styling valid and invalid states#
CSS pseudo-classes let you react to validation:
<style>
input:user-invalid { border-color: #b00020; }
input:user-valid { border-color: #1b7f3b; }
</style>:user-invalid and :user-valid apply only after the user has interacted with the field, which avoids showing every field in red on page load. The older :invalid applies immediately, which often feels aggressive.
Turning it off#
Add novalidate to the <form> to skip built-in checks. You might do this when you handle validation with your own scripts, but you take responsibility for accessible error messages.
Validation is not security#
Writing helpful messages#
- Say what is wrong and how to fix it.
- Keep the message next to the field.
- Do not blame the user ("You entered it wrong").
- Do not depend on colour alone to show an error.
Common mistakes#
- Using
patternwithout atitle. - Writing complicated regular expressions that reject real names or phone numbers.
- Treating
maxlengthas a security limit. - Showing red borders before the visitor has typed anything.
- Forgetting that validation only runs when a submit button triggers the form.
Try it yourself#
Create a registration form with a required name (at least 3 characters), a required email, a guests number between 1 and 8, and a loyalty code with a pattern and a helpful title. Try submitting with invalid values and read the messages. Add :user-invalid styling, and then try the form with novalidate to see the difference.
Recap#
Attributes such as required, minlength, maxlength, min, max, pattern and the input type give you free, instant validation. Pair pattern with a clear title, style states with :user-invalid, and always validate again on the server.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -