Skip to the content

Lesson 11 of 296

Form validation in HTML

What you will learn

  • What validation is and why it matters
  • The HTML attributes that check input without JavaScript
  • How to show errors clearly
  • Why the server must always check the data again
On this page

What is validation?

Validation means checking that the data a user typed is correct before using it. For example: the email has an @, the password is long enough, the required fields are not empty.

HTML can do many checks without any JavaScript. If a check fails, the browser stops the form from sending and shows a message next to the field.

Validation attributes

AttributeRuleExample
requiredMust not be empty<input name="name" required>
type="email"Must look like an email<input type="email">
type="url"Must look like a URL<input type="url">
minlengthAt least this many charactersminlength="8"
maxlengthAt most this many characters. The user cannot type more.maxlength="50"
minSmallest number or earliest datemin="1"
maxBiggest number or latest datemax="10"
stepWhich numbers are allowedstep="0.01" allows pennies
patternMust match a rule written as a regular expressionsee below

Examples

<label for="age">Age</label>
<input type="number" id="age" name="age" min="18" max="120" required>

<label for="pw">Password (at least 8 characters)</label>
<input type="password" id="pw" name="password" minlength="8" required>

<label for="qty">Quantity</label>
<input type="number" id="qty" name="qty" min="1" max="10" value="1">

pattern

pattern lets you write your own rule. It uses a regular expression, a short code for describing text patterns. You will learn regular expressions in Part 5. For now, one example:

<label for="code">Discount code (5 capital letters)</label>
<input type="text" id="code" name="code" pattern="[A-Z]{5}" title="Five capital letters, for example SAVE5">

[A-Z]{5} means "exactly five capital letters". The title text is shown to the user when the rule fails, to explain what is expected.

Tell the user the rules before they type

Write the rule in the label or in a hint under the field. Do not make people guess and fail.

<label for="pw">Password</label>
<p id="pw-hint">At least 8 characters.</p>
<input type="password" id="pw" name="password" minlength="8" required aria-describedby="pw-hint">

aria-describedby links the hint to the field, so screen readers read the hint after the label. You will learn more about aria attributes in Lesson 13.

Styling valid and invalid fields

CSS can style fields based on their state:

input:invalid { border-color: red; }
input:valid { border-color: green; }
input:user-invalid { border-color: red; }

:invalid applies straight away, so an empty required field is red before the user has even typed. :user-invalid only applies after the user has interacted with the field, which is friendlier. You will learn CSS in Part 3.

Turning off browser validation

Sometimes you want to show your own error messages with JavaScript instead. Add novalidate to the form:

<form action="/signup" method="POST" novalidate>

The attributes still describe the rules, and JavaScript can still check them, but the browser will not show its own pop-up messages.

The most important rule: always validate on the server

Front end validation is only for the user's comfort. It gives quick feedback. It is not security.

Anyone can:

  • open dev tools and delete the required attribute
  • send a request straight to your server with a tool, skipping your form completely

So the back end must check every value again, every time. This is one of the most important ideas in web development, and interviewers love to ask about it.

Front end validationBack end validation
PurposeFast, friendly feedbackProtect data and security
Can the user skip it?YesNo
Required?RecommendedAlways

Good error messages

  • Say what is wrong and how to fix it: "Enter an email address like name@example.com", not "Invalid input".
  • Show the message next to the field, not only at the top of the page.
  • Do not rely on colour alone. A red border is invisible to colour-blind users. Add text, and an icon if you like.
  • Keep what the user typed. Never empty the whole form because one field was wrong.

Common mistakes

  • Trusting front end validation for security.
  • Hiding the rules until the user fails.
  • Error messages shown only as a colour.
  • Clearing the form after an error.
  • Setting maxlength too short for real data, such as long names or email addresses.

How to say it in an interview

"HTML gives built-in validation with attributes like required, type, minlength, min, max and pattern, so the browser blocks a bad submission without JavaScript. But client-side validation is only for user experience, because anyone can bypass it. The server must validate every value again. I also show clear error messages next to each field, in text, not only in colour."

Key words

WordMeaning
ValidationChecking that input data is correct
Client-sideRuns in the browser
Server-sideRuns on the server
requiredAn attribute that makes a field compulsory
patternAn attribute with a custom rule (a regular expression)
novalidateTurns off the browser's own validation messages

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.