Sub topic 3
Built-in constraint attributes
This chapter assumes you can write a basic HTML form with labels and inputs, and that you know how a form submission sends named values to a server. Constraint attributes are HTML attributes that tell the browser which values a control accepts. The browser checks them when the user submits the form, and it blocks submission if any check fails. You write no JavaScript for this.
The attributes at a glance
| Attribute | Applies to | Fails when |
|---|---|---|
| `required` | Most controls | The value is empty |
| `minlength` | Text-like inputs, `textarea` | The value has fewer characters than the minimum |
| `maxlength` | Text-like inputs, `textarea` | The value has more characters than the maximum |
| `min`, `max` | Number, range, and date-like inputs | The value is below the minimum or above the maximum |
| `step` | Number, range, and date-like inputs | The value is not a valid step from the base |
| `pattern` | Text, search, url, tel, email, password | The value does not match the regular expression |
A worked example
This form uses most of the attributes together.
<form id="signup" action="/signup" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" required autocomplete="email">
<label for="username">Username</label>
<input id="username" name="username" required minlength="3" maxlength="20"
pattern="[a-z0-9_]+" title="Lowercase letters, digits, and underscores only">
<label for="age">Age</label>
<input id="age" name="age" type="number" min="18" max="120" step="1">
<button type="submit">Sign up</button>
</form>If the user clicks Sign up with an empty username, the browser stops the submission and shows a message on that field. The message for a pattern failure includes the title text, so write the title as a plain description of the rule.
How each attribute behaves
required
required fails on an empty value. For a checkbox it fails when the box is unchecked. For a select it fails when the selected option has an empty value.
minlength and maxlength
Browsers stop the user from typing past maxlength, so the user rarely sees an error for it. minlength shows an error only after the user has edited the field. A value set by a script, or a pre-filled value the user has not changed, skips the minlength check. An empty value never fails minlength, so pair it with required.
min, max, and step
min and max set an inclusive range. step sets the allowed spacing, measured from min, or from the value attribute if there is no min. With min="18" and step="5", the valid values are 18, 23, 28, and so on. The default step for number is 1, so 3.5 fails unless you set step="0.5" or step="any".
pattern
The browser treats pattern as if it were anchored at both ends. [a-z0-9_]+ therefore means the whole value must match, and you do not write ^ or $. Do not wrap the pattern in slashes either.
Common errors
- Expecting empty fields to fail without
required. Empty optional fields skip every other check. Theagefield above accepts an empty value. - Using
minandmaxon a plain text input. They only apply to input types that have a numeric or date value. Usetype="number", or useminlengthandmaxlengthfor text. - Writing a
patternthat is too strict. A pattern such as[A-Za-z]+rejects names with spaces, hyphens, or accents. Validate shape, not identity. - Forgetting
titleonpattern. Without it, some browsers show only a generic "match the requested format" message. - Trusting these checks for security. A client can send any request it likes, so the server must repeat every rule.
Exercise
A field has type="number" min="10" step="5" and no required. Which values does the browser accept: 10, 12, 15, 17.5, or an empty field?
Show answer
It accepts 10, 15, and the empty field. The valid values are 10, 15, 20, and so on, because step is measured from min. 12 and 17.5 fail the step check. The empty field passes because the control is not required.