Lesson 39 of 51 · Module 6, lesson 5 of 6 · 4 min
Built-in validation
Let the browser check answers before a form is sent, with required fields, limits and patterns.
What you'll learn
- How to make a field required
- How to set limits on length and numbers
- How to match a pattern, and how to help people who get it wrong
Why this matters
People make mistakes when they fill in forms: an empty box, a missing @, a password that is too short. The browser can catch these mistakes before the form is sent, and tell the person what to fix — with no JavaScript. You only need a few attributes.
Required fields
Add required (a boolean attribute) to a field that must not be empty. Also tell people in the label — not only with a colour or a star:
<label for="email">Email (required)</label>
<input id="email" name="email" type="email" required>
The rules you can set
| Attribute | Rule | Works on |
|---|---|---|
required | Must not be empty | Almost all fields |
type="email", type="url" | Must look like an email or web address | Those types |
minlength, maxlength | At least / at most so many characters | Text, password, textarea |
min, max | Lowest and highest value | Number, date |
step | Allowed steps, such as step="0.5" | Number |
pattern | Must match a pattern | Text, tel, password… |
A sign-up form with rules
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create an account</title>
</head>
<body>
<h1>Create an account</h1>
<form>
<p>
<label for="email">Email (required)</label>
<input id="email" name="email" type="email" required>
</p>
<p>
<label for="password">Password (required, at least 8 characters)</label>
<input id="password" name="password" type="password" minlength="8" required>
</p>
<p>
<label for="age">Age (16 or over)</label>
<input id="age" name="age" type="number" min="16" max="120">
</p>
<p>
<label for="code">Class code: 3 letters and 2 numbers, like ABC12</label>
<input id="code" name="code" type="text" pattern="[A-Z]{3}[0-9]{2}">
</p>
<p><button type="submit">Create account</button></p>
</form>
</body>
</html>
What you'll see: four labelled fields. Press Create account with an empty email box, and the browser points at it with a message.
The browser checks the fields in order. It stops at the first problem, moves the cursor there, and shows a message in the visitor's own language. Nothing is sent until every field passes.
Patterns
pattern uses a small language called a regular expression. You do not need to learn it now. Two examples cover most beginner needs:
[A-Z]{3}[0-9]{2}— three capital letters, then two digits.[0-9]{5}— exactly five digits.
The browser's message for a pattern is vague ("Please match the requested format"), so always write the rule in the label, as in the example.
The browser is not enough
These checks help honest visitors. They do not protect your data: anyone can switch them off in the developer tools. When a real server receives a form, it must check everything again. (You will do that when you learn a server language.)
Try it yourself
- Select the example and choose Try it.
- Press Create account with everything empty. Where does the browser stop first?
- Fill in the email, and type a 5-letter password. Submit.
- Type
abc12in the class code. It fails — the pattern needs capital letters.
Common mistakes
- The rule only in the pattern, not in the label. The visitor cannot guess it. Fix: write the rule in the label.
- "Required" shown only by colour or a star. Screen reader users and people who do not see colours well miss it. Fix: the word "required" in the label (the
requiredattribute also announces it). - Trusting browser checks for security. Fix: a server must check again.
maxlengththat is too short for real names or addresses. Fix: be generous.
Check yourself
- Which attribute makes a field mandatory?
- How do you allow only numbers from 1 to 10?
- Why must a server check the data again?
Answers
required.type="number" min="1" max="10".- Because browser checks can be switched off or skipped; they only help honest visitors.
Next: your form checks its data. The last question: where does that data actually go?


