Start with constraints in the markup

Use a proper input type, an explicit label, and required only for fields the recipient needs. Browser constraints improve input feedback. The server still decides which submitted values to accept.

Expose an error after interaction

This field marks up the label and hint independently. The user-invalid variant highlights invalid fields after a submit attempt or when the browser determines the visitor has interacted with the field.

HTML
<label class="block" for="email">Email address</label>
<input id="email" name="email" type="email" required autocomplete="email"
  aria-describedby="email-hint"
  class="block w-full rounded-lg border p-3 user-invalid:border-red-600">
<p id="email-hint" class="mt-1 text-sm text-slate-600">
  We’ll use this address to reply.
</p>

Keep feedback readable

If an async request fails, show a status message beside the submit button, keep the entered values, and allow another attempt. Check what happens with a keyboard and a screen reader.

Related resources

Create a formBrowse blog