Loading lessons...
Form Validation
Form Validation
Validation checks that what the visitor typed is good before the form is sent. HTML can do a lot of this by itself!
required
Makes a field mandatory. The browser refuses to submit until it's filled.
<input type="text" name="name" required>
minlength and maxlength
Control how many characters are allowed.
<input type="password" name="pw" minlength="8" maxlength="64">
min and max
For numbers, dates, ranges: the allowed lowest and highest values.
<input type="number" name="age" min="1" max="120">
pattern
A "regular expression": a pattern the text must match. This example requires 5 digits:
<input type="text" name="zip" pattern="[0-9]{5}" title="5 digit zip code">
title explains what's expected if the pattern fails.
step
The allowed jump between values (for numbers and ranges).
<input type="number" name="rating" min="0" max="5" step="0.5">
inputmode
Tells the phone which keyboard to show (like numeric or email), even for text inputs.
<input type="text" name="card" inputmode="numeric">
placeholder
Grey hint text that disappears when typing.
<input type="text" name="code" placeholder="e.g. ABC-123">
autocomplete
Lets the browser fill fields from saved info.
<input type="email" name="email" autocomplete="email">
Common values: name, email, username, current-password, new-password, tel, url.
readonly
Shows the value but the visitor can't change it. It still gets sent.
<input type="text" name="code" value="FIXED" readonly>
disabled
Grayed out and NOT sent with the form.
<input type="text" name="temp" disabled>
multiple
Lets the field accept several values (emails, files, options).
The Constraint Validation API
Browsers also give JavaScript tools (like setCustomValidity()) for custom checks. HTML handles the common cases: JS handles the fancy ones.
TL;DR
required,pattern,min/max,minlength/maxlength,stepvalidate input.placeholderhints,autocompletefills.readonlyshows-but-locks;disabledturns off and hides from submission.multipleallows several values.inputmodepicks the phone keyboard.