Sub topic 9
Built-in validation with attributes
This chapter assumes you can build a form with labeled inputs and a submit button, and that you know how name and value become submitted data. Here you will let the browser check values before the form is sent, with no JavaScript.
How built-in validation works
Each control has constraints, which are rules set by attributes. When the user submits, the browser checks every control. If one fails, the browser cancels the submission, focuses the first invalid control, and shows a message.
The common attributes
| Attribute | Applies to | Rule |
|---|---|---|
| `required` | Most controls | The value must not be empty. |
| `minlength`, `maxlength` | Text, textarea | Limits on the number of characters. |
| `min`, `max` | Number, date, range | Limits on the value. |
| `step` | Number, date, range | The value must fit the step. |
| `pattern` | Text-like inputs | The value must match a regular expression. |
| `type` | `email`, `url`, and others | The value must have that format. |
The next example combines several of them.
<form action="/order" method="post">
<label for="zip">ZIP code</label>
<input id="zip" name="zip" type="text" inputmode="numeric"
required pattern="[0-9]{5}" title="Enter exactly five digits">
<label for="qty">Quantity</label>
<input id="qty" name="qty" type="number" min="1" max="10" value="1">
<button type="submit">Order</button>
</form>The ZIP field fails if it is empty or is not exactly five digits. The title text is often added to the browser's message, so use it to describe the format. The quantity field fails for values below 1 or above 10.
Gotchas
- Empty values skip most checks. Only
requiredrejects an empty field. A blanktype="email"field, or one with apattern, is valid unless it is alsorequired. patternmust match the whole value. The browser treats it as anchored, so[0-9]{5}rejects123456. Do not add^and$.minlengthchecks only typed input. A value set by a script, or a defaultvalue, is not checked until the user edits it.patternis ignored on types that do not support it. It works ontext,search,url,tel,email, andpassword, but not onnumber.- Radio groups need only one
required. If any radio in a group has it, the group needs a choice. - An unchecked required checkbox blocks submission. This suits "I accept the terms."
Turning validation off
Add novalidate to the form to skip browser checks, for example when you validate in JavaScript. A submit button with formnovalidate skips them for that button only. This suits a "Save draft" button.
<button type="submit">Place order</button>
<button type="submit" formnovalidate>Save draft</button>Exercise
A field has pattern="[A-Z]{3}" but no required. The user leaves it empty and submits. Does the form submit? What changes if the user types abcd?
Show answer
The empty form submits, because only required rejects an empty value. If the user types abcd, validation fails: the value is not three uppercase letters, and the pattern must match the whole value.
Exercise
Write the attributes for a required password field that must be 8 to 64 characters long.
Show answer
<input type="password" name="password" required minlength="8" maxlength="64">