Dev.to WebDev 🛠 Dev 👁 2 📖 5 min read

Forms Fail in the Details: Validation and Error Messages That Help

Forms are where products collect the things they need most: an email address, a payment, a booking, a message. They're also where many people give up. And a lot of that isn't due to length. It's due to the way forms hand

Forms are where products collect the things they need most: an email address, a payment, a booking, a message. They're also where many people give up. And a lot of that isn't due to length. It's due to the way forms handle mistakes.

You know the experience: you fill in everything, press submit, and the page reloads with a red banner saying "Invalid input". Which input? Invalid how? Good form design treats errors as a conversation, not a verdict.

Prevent first, then correct

The best error message is one nobody ever needs to see. Before writing any messages, reduce the chance of mistakes:

  • Use the right input type. Email, telephone, number and date fields bring up the right keyboard on mobile and offer free basic checks.
  • Use autocomplete attributes. Browsers can fill names, addresses and payment details when fields are labelled correctly. It's one of the highest-value changes you can make.
  • Explain expectations up front. If a password has rules, show them before the user types, not only after they fail.
  • Accept reasonable variations. Phone numbers with spaces or dashes, postcodes in lower case, emails with a trailing space. Clean the input instead of rejecting it.
  • Ask for less. Every field is a chance to make an error. Remove ones you don't use.

Labels, not just placeholders

A placeholder that disappears when you start typing leaves people unable to check what a field was for. Use a visible label above the field, and use placeholder text only for examples, if at all. This also matters for accessibility, since screen readers rely on proper label associations.

When to validate

Timing is where many forms go wrong.

  • Validating on every keystroke shows an error before the person has finished typing "name@exa". It feels like being nagged.
  • Validating only on submit means finding out about all the problems at once, at the end.
  • A good middle path is to validate a field when the user leaves it (on blur), then update live as they fix it, and remove the error as soon as it's resolved.

Some checks are exceptions. A password strength hint or character counter is helpful as you type, because it guides rather than criticises.

What a helpful message looks like

A useful error message answers three things: what's wrong, where, and how to fix it.

Instead of Try
Invalid input Enter an email address like [email protected]
Error 422 We couldn't save this. Check the highlighted fields and try again.
Password too weak Use at least 12 characters. A short sentence works well.
Required Enter your company name
Something went wrong We couldn't send your message. Your text is saved, so try again in a minute.

A few rules of thumb:

  • Write in plain language. No codes or technical terms.
  • Be specific and polite. "Please enter a date in the future" instead of "Bad date".
  • Blame the situation, not the person. "We couldn't find that email" rather than "You entered the wrong email".
  • Say it next to the field. Place the message directly below or beside the problem, not only at the top.
  • Keep it short.

Don't rely on colour alone

Red text and borders are fine, but they're not enough. People with colour-vision differences may not see them, and a red border says nothing about what's wrong. Combine colour with:

  • An icon or a short prefix
  • Message text that stands on its own
  • Proper accessibility markup, such as linking the message to its field and announcing errors to assistive technology

On submit, if there are errors, move focus to the first problem field, or to a summary at the top that links to each one. Otherwise keyboard and screen-reader users may not know the form failed.

Never throw away what people typed

This is the cardinal sin of forms. If something fails, whether validation, a timeout, or a server error, keep everything the user entered. Clearing a long message because of one bad field is infuriating.

Also be careful with:

  • Password fields, which are often cleared on error. Consider offering a show/hide toggle so people can check what they typed.
  • Session timeouts that lose a half-completed form.
  • Back-button behaviour, which should restore entered data where possible.

Server errors are your problem, not theirs

Client-side validation is a convenience. The server must always validate too, since anything in the browser can be bypassed. But when the server rejects something, the user should still get a message they can act on.

That means your API needs to return structured, field-level errors, and your front end needs to map them to the right fields. A generic "400 Bad Request" in the console might be fine for developers but it leaves the user stranded.

For real outages, be honest and calm: say it's not their fault, say whether their data was saved, and say what to do next.

Make submission safe

  • Disable the button only briefly, or show a clear loading state, to prevent double submissions. Make sure the server also handles duplicates safely.
  • Confirm success clearly. Tell people what happened and what to expect next, such as "We'll reply within the time we promise" (only if you really will).
  • Avoid CAPTCHAs where you can. If you need bot protection, consider lighter approaches first, and test the experience with real people.

A thirty-minute form audit

Open your most important form and try the following:

  1. Submit it empty. Is every message clear and next to its field?
  2. Enter plausible but messy data (spaces, capital letters). Does it cope?
  3. Fill it in using only a keyboard.
  4. Try it on a phone. Does the right keyboard appear?
  5. Cause a server error, if you can. What does the user see, and is their input kept?
  6. Check with a screen reader or at least the browser's accessibility tools.

Most teams find three or four small problems within the first half hour, and each fix helps every user.

The short version

  • Prevent errors with the right input types, autocomplete, clear labels and forgiving input.
  • Validate when a field is left, and clear errors as soon as they're fixed.
  • Good messages say what's wrong, where and how to fix it, in plain language.
  • Don't rely on colour alone, and manage focus on failure.
  • Never lose what the user typed, and handle server errors gracefully.

When did anyone last submit your main form with deliberately wrong data?

We're nothink, a small team that designs and builds products for startups — more at nothink.site.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.