Form Validation - HTML Tutorial
People make mistakes when filling in forms: they skip a field, mistype an email address or enter a date in the past. Validation means checking what's been entered before the form is sent, and helping the visitor fix any problems.
Modern browsers can do a lot of this for you, with no JavaScript at all. You just add a few attributes to your fields.
Required Fields
Add the required attribute to any field that must be filled in:
<form action="" method="post">
<label for="v-name">Name (required)</label>
<input type="text" id="v-name" name="name" required>
<button type="submit">Send</button>
</form>
Try clicking Send with the field empty. The browser stops the form from being sent, moves the cursor to the field and shows a message asking you to fill it in.
Tell visitors which fields are required before they start. Writing "(required)" in the label works well. If you use a symbol such as an asterisk instead, explain what it means at the top of the form.
Checking the Format
Some input types check their format automatically. A field with type="email" won't accept a value without an @ and a domain, and type="url" expects a full web address. This only applies when something has been typed: add required as well if the field can't be left empty.
Length and Range Limits
minlengthandmaxlengthset the minimum and maximum number of characters for text fields and text areas.minandmaxset the lowest and highest allowed values for numbers and dates.stepsets the allowed increments for numbers. For example,step="0.01"allows prices with cents.
<label for="v-guests">Number of guests (1 to 12)</label>
<input type="number" id="v-guests" name="guests" min="1" max="12" required>
<label for="v-message">Message (at least 10 characters)</label>
<textarea id="v-message" name="message" minlength="10" maxlength="500" rows="4"></textarea>
Including the limits in the label tells visitors what's expected up front, so they're less likely to hit an error at all.
Custom Patterns
The pattern attribute checks a text field against a regular expression, a compact way of describing a text format. For example, this field accepts exactly four digits:
<label for="v-postcode">Postcode (4 digits)</label>
<input type="text" id="v-postcode" name="postcode" inputmode="numeric" pattern="[0-9]{4}">
[0-9] means "any digit", and {4} means "exactly four of them". Regular expressions can get complicated, so for now, stick to simple patterns like this one. Because the browser's error message for a pattern is quite general, always describe the expected format in the label.
Hints for Each Field
Sometimes a field needs more explanation than fits in a label. Put the hint in its own element, give it an id and connect it to the field with the aria-describedby attribute. Screen readers then read the hint after the label:
<label for="v-password">Password</label>
<input type="password" id="v-password" name="password"
minlength="12" required aria-describedby="password-hint">
<p id="password-hint" class="hint">Use at least 12 characters.</p>
Styling Valid and Invalid Fields
CSS has pseudo-classes that match fields based on their validation state:
:validand:invalidmatch fields whose current value passes or fails validation.:user-validand:user-invalidwork the same way, but only after the visitor has interacted with the field.:requiredand:optionalmatch fields with or without therequiredattribute.
:user-invalid is usually the best choice for error styles. With plain :invalid, an empty required field shows as an error the moment the page loads, before the visitor has even started.
input:user-invalid,
textarea:user-invalid {
border: 2px solid #c00;
background-color: #fff5f5;
}
input:user-valid,
textarea:user-valid {
border: 2px solid #2e8b57;
}
Remember not to rely on color alone. The browser's own error message already explains the problem in words, so these styles are an extra visual cue.
Turning Validation Off for Testing
Adding novalidate to the form element switches off the browser's checks, which can be handy for testing how a server handles bad data:
<form action="" method="post" novalidate>
Browser Validation Isn't Security
Caution
Browser validation is there to help honest visitors fix mistakes. It isn't a security feature. Anyone can switch it off, edit the page in Developer Tools or send data to your server without using your form at all. A server that receives form data must always check it again itself, however thorough your HTML validation is.
That's the end of the Forms chapter! You can now build forms that are clear, accessible and helpful. The last chapter covers the habits that will make all of your pages better.