Skip to content
Velloreth
Web Development 3 min read

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

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

TypeKeyboardValidation
emailan email keyboardthe format
tela numeric keypadnone
urla URL keyboardthe format
numbera numeric inputa range
datea date pickera valid date
passworda text keyboardnone
searcha search keyboardnone
filea file pickera 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.

For the related topics, see form-accessibility-guide and form-validation-patterns. For the broader context, csrf-explained and aria-labels-explained.

Frequently asked questions

Why is a placeholder not a label?
Because it disappears as soon as the user types, so the field has no visible name, and a screen reader may not announce it. A label is always visible and is programmatically associated with the input. A placeholder is a hint, not a name.
What does the input type do?
It selects the keyboard on a mobile device, it enables a native validation, and it gives the field a semantic meaning. type=email shows an email keyboard and validates the format, type=tel shows a numeric keypad, and type=number shows a numeric input.
Should I use native validation or JavaScript?
Use native validation as the first layer, because it is free, it is accessible, and it works without JavaScript. Add JavaScript for a rule the native validation cannot express, and always validate on the server, because a client check is a convenience and not a security control.
What is autocomplete for?
It tells the browser what the field expects, so it can fill it from the user's saved data. autocomplete=email fills the email, autocomplete=one-time-code fills a code from an SMS, and autocomplete=new-password offers a generated password. It saves a lot of typing on a mobile device.
How should an error be shown?
Next to the field, in text, with a programmatic association so a screen reader announces it, and with the field marked as invalid. The message should say what to do, not only that something is wrong. Move the focus to the first invalid field on submit.
What is the difference between required and aria-required?
required is the native attribute, and it is announced by a screen reader and enforced by the browser. aria-required is an ARIA attribute for a custom control that cannot use the native one. Use required for a native input, and aria-required only for a custom widget.