Forms: Validation — HTML

Validation attributes put rules on a form's fields. required means a field must be filled in. minlength and maxlength set the text length, min and max limit…

You click Sign Up, and the form isn't sent. Instead, a small message appears on the email field: the address has no "@" yet. The browser has just done validation: it checked what was typed into the form before sending it. For example: is the field filled in, is the text long enough, is the format right?

HTML has attributes for validation. Each attribute puts one rule on a field, for example "must be filled in". When a rule is broken, the browser does not send the form, and it shows a message on the field that is wrong. All of this happens without JavaScript (the language that makes a page react).

Required fields and a minimum length

<form>
  <label for="username">Username</label>
  <input type="text" id="username" name="username" required minlength="3">

  <label for="password">Password</label>
  <input type="password" id="password" name="password" required>

  <button type="submit">Sign Up</button>
</form>

Only two attributes are new here: required and minlength. You already know the rest:

All the fields and the button are inside the same <form>. The browser only checks and sends the fields inside that form.

Other validation attributes

<input type="email" name="email" required placeholder="[email protected]">

This field must be filled in, and its value must look like an email address (with an @). That format check comes from type="email". placeholder is not a rule. It is grey example text inside the field, and it disappears as soon as the visitor starts typing.

Other attributes you will often use:

For long text that can have several lines, use <textarea>. The validation attributes are the same: <textarea name="message" required minlength="10"></textarea>.

The browser is only the first layer

All of these checks run in the visitor's browser. Someone who knows how can remove the attributes with DevTools (the browser's built-in tool for inspecting a page). They can also send data directly to the server (the computer that receives the form's data), without using the form. So HTML validation is only the first layer of checking: it helps visitors fill in the form correctly, but it can be skipped. The second layer is the server, which still has to check every rule again before it saves the data.

⚠️ Rules that don't work the way you think

TL;DR: Validation attributes put rules on a form's fields. required means a field must be filled in. minlength and maxlength set the text length, min and max limit numbers, type="email" checks the email format, and pattern matches a pattern. When a rule is broken, the browser does not send the form, and it shows a message. The fields and the button must be inside the <form>. The browser's checks are only the first layer and can be skipped, so the server still has to check everything again.

⚡ Must be filled in

Write:

<form>
  <label for="username">Username</label>
  <input type="text" id="username" name="username" required minlength="3">
  <button type="submit">Sign Up</button>
</form>
Guess first: the field is still empty and Sign Up is clicked. What happens?

The form isn't sent. The browser stops the sending and shows a message on that field, without JavaScript.

Meaning: required means the field must be filled in, and you write it without a value. minlength="3" means at least 3 characters. The fields and the button must be inside the same <form>.

⚡ An email format

Write:

<input type="email" name="email" required placeholder="[email protected]">

Meaning: type="email" checks that the value looks like an email address (with an @). placeholder is not a rule, only example text that disappears when the visitor types.

⚡ Turning a rule off

Guess first: does required="false" turn the rule off?

No. required is active as soon as you write it, and the browser does not read its value. To turn the rule off, remove the attribute.

Meaning: An attribute without a value, like required, is active because it is there, not because of its value.

⚡ The browser is only the first layer

Meaning: All these checks run in the browser, and the attributes can be removed with DevTools (the browser's built-in tool for inspecting a page). The server still has to check again before it saves the data.

What you will learn