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:
requiredmeans the field must be filled in. You write it without a value, just the wordrequiredinside the<input>tag. If the field is empty when Sign Up is clicked, the browser does not send the form, and it shows a message like "Please fill out this field" on that field.minlength="3"means at least 3 characters. Typeaband the browser refuses to send the form and tells you the minimum length.- You could leave out
type="text", because plain text is the default kind. It is written here so the kind of field is easy to see. type="password"shows what is typed as dots. This password field is alsorequired.<button type="submit">Sign Up</button>is the send button. The text between its tags is the text on the button.
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:
maxlength="15": at most 15 characters. The browser doesn't let visitors type more than that.min="17"andmax="65": the smallest and largest value for a number field (type="number").pattern="[0-9]+": the value must match a pattern. In this example the pattern is: digits only. Patterns are written in a special language called regex.
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
- Thinking
required="false"turns the rule off.requiredis active as soon as you write it, and the browser does not read its value. To turn the rule off, remove the attribute. - A misspelled attribute name. The browser doesn't recognise
minlenght, so the rule silently does nothing. - A Cancel button that sends the form. A
<button>inside a form without atypeworks as a send button. Givetype="button"to any button that should not send the form. - Using
placeholderas a label. The example text disappears when the visitor types, so still add a<label>. - A field outside the
<form>. That field is not checked and not sent. - Thinking an empty field is checked by
minlengthorpattern. Rules other thanrequiredonly check a field that has something in it. Aminlength="3"field left empty is still sent. If the field must be filled in, addrequired. - Thinking
type="email"makes sure the address is real. The browser only checks the basic shape: a name,@, then a domain name (such as gmail.com).name@gmailanda@bcount as valid,a@is refused. To make sure the address really exists, the server has to send a confirmation email to it.
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.