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:

html
<label for="email">Email (required)</label>
<input id="email" name="email" type="email" required>

The rules you can set

AttributeRuleWorks on
requiredMust not be emptyAlmost all fields
type="email", type="url"Must look like an email or web addressThose types
minlength, maxlengthAt least / at most so many charactersText, password, textarea
min, maxLowest and highest valueNumber, date
stepAllowed steps, such as step="0.5"Number
patternMust match a patternText, tel, password…

A sign-up form with rules

html
<!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 "Create an account" form with four labelled fields: "Email (required)", "Password (required, at least 8 characters)", "Age (16 or over)", and "Class code: 3 letters and 2 numbers, like ABC12", followed by a "Create account" button.

An email field with the required attribute. When it is empty and the form is submitted, the browser shows a small message next to the field: "Please fill out this field." With type="email" and the text "ana", it asks for an @ in the address. With minlength="8" and 5 letters, it asks for at least 8 characters. The exact words depend on the browser and its language, and nothing is sent until every field passes.

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

  1. Select the example and choose Try it.
  2. Press Create account with everything empty. Where does the browser stop first?
  3. Fill in the email, and type a 5-letter password. Submit.
  4. Type abc12 in 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 required attribute also announces it).
  • Trusting browser checks for security. Fix: a server must check again.
  • maxlength that is too short for real names or addresses. Fix: be generous.

Check yourself

  1. Which attribute makes a field mandatory?
  2. How do you allow only numbers from 1 to 10?
  3. Why must a server check the data again?

Answers

  1. required.
  2. type="number" min="1" max="10".
  3. 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?

Chat with us on WhatsApp