HTML Forms Best Practices: Inputs, Validation and UX
A form is the main way a user gives you data. Learn the input types, the native validation, the labels, and the error handling.
By Velloreth
The label
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
Every input needs a label.
A placeholder is not a label, because it disappears when the user types.
The for attribute associates the label with the input.
A wrapping label works too.
<label>
Email address
<input name="email" type="email">
</label>
The input type
| Type | Keyboard | Validation |
|---|---|---|
email | an email keyboard | the format |
tel | a numeric keypad | none |
url | a URL keyboard | the format |
number | a numeric input | a range |
date | a date picker | a valid date |
password | a text keyboard | none |
search | a search keyboard | none |
file | a file picker | a type and a size |
The type gives a keyboard, a validation and a semantic.
type="number" is not for a phone number or a card number, because it allows a scroll and a scientific notation. Use type="text" with inputmode="numeric".
The native validation
<input type="email" required minlength="5" maxlength="100">
<input type="number" min="1" max="10" step="1">
<input type="text" pattern="[A-Za-z]{3}">
It is free, accessible and works without JavaScript.
It should be the first layer.
Always validate on the server, because a client check is a convenience and not a security control.
The autocomplete
<input name="email" type="email" autocomplete="email">
<input name="password" type="password" autocomplete="new-password">
<input name="code" type="text" autocomplete="one-time-code">
It tells the browser what the field expects.
new-password offers a generated password.
one-time-code fills a code from an SMS.
It saves a lot of typing on a mobile device.
The error
<label for="email">Email address</label>
<input id="email" type="email" aria-describedby="email-error" aria-invalid="true">
<p id="email-error" role="alert">Enter an email address in the format name@example.com</p>
Next to the field, in text.
aria-describedby associates the message with the input.
aria-invalid marks the field.
role="alert" announces the message.
The message says what to do, not only that something is wrong.
Move the focus to the first invalid field on submit.
The structure
<form action="/subscribe" method="post">
<fieldset>
<legend>Your details</legend>
<!-- the fields -->
</fieldset>
<button type="submit">Subscribe</button>
</form>
A fieldset and a legend group the related fields.
A real button with type="submit".
The button says what it does: “Subscribe”, not “Submit”.
The security
A CSRF token for a state-changing form.
<input type="hidden" name="csrf_token" value="...">
A server-side validation for every field.
A rate limit on the endpoint.
A file upload needs a type check, a size limit and a storage outside the web root.
The UX
One column is easier to scan than two.
A visible label above the field.
A hint for a format the user may not know.
A disabled submit is an anti-pattern, because the user cannot tell why.
A progress indicator for a multi-step form.
A confirmation after the submit.
Common mistakes
A placeholder as a label. The field has no name.
No autocomplete. Extra typing.
type="number" for a phone. A scroll changes the value.
A client-only validation. A security hole.
An error that is not announced. A screen reader misses it.
An error that says “invalid”. It does not say what to do.
A disabled submit. The user is stuck.
No name attribute. The field is not submitted.
A label that is not associated. A screen reader misses it.
No server validation. A bad value is stored.
A quotable summary
Every input needs a label, because a placeholder disappears when the user types, and the input type gives a keyboard, a validation and a semantic. Native validation is free and accessible and should be the first layer, with a server-side check as the real control, and an error must be announced, associated with the field, and say what to do. autocomplete saves the user a lot of typing, and a real button with a clear label completes the form.
Related reading
For the related topics, see form-accessibility-guide and form-validation-patterns. For the broader context, csrf-explained and aria-labels-explained.