---
title: "How do I show custom form validation messages with plain HTML?"
description: "Use required, pattern and other attributes for built-in checks, setCustomValidity() for your own wording, and the :user-invalid selector to show errors only after someone has interacted."
url: https://www.danny.engineering/answer/html/how-do-i-show-custom-form-validation-messages-with-plain-html
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["HTML"]
---

# How do I show custom form validation messages with plain HTML?

## The short answer


Let HTML attributes such as `required`, `pattern` and `type="email"` do the checking. To change the wording of the error, call `setCustomValidity()` with your message when the field is invalid, and with an empty string when it is valid. Style errors with `:user-invalid` so fields do not look broken before anyone has typed.


```html
<label for="postcode">Postcode</label>
<input id="postcode" name="postcode" required
  pattern="[A-Za-z]{1,2}[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}"
  autocomplete="postal-code">
```


```javascript
const postcode = document.querySelector("#postcode");

function checkPostcode() {
  postcode.setCustomValidity("");
  if (postcode.validity.valueMissing) {
    postcode.setCustomValidity("Enter your postcode.");
  } else if (postcode.validity.patternMismatch) {
    postcode.setCustomValidity("Enter a UK postcode, like SW1A 1AA.");
  }
}

postcode.addEventListener("input", checkPostcode);
checkPostcode();
```


## Why it happens


Browsers run constraint validation on every form control. Each control has a `validity` object with flags such as `valueMissing`, `typeMismatch`, `patternMismatch` and `tooShort`. When the form is submitted and any flag is true, the browser blocks the submit, focuses the first invalid field and shows its own message bubble.


Those default messages are generic and depend on the browser and language. `setCustomValidity()` replaces the message, but it also does something people miss: any non-empty string marks the field as invalid, whatever its value. That is why you must reset it to an empty string before checking again, or the field can never become valid.


## How to fix it


### Use the built-in attributes first


Many rules need no JavaScript at all.


```html
<input type="email" name="email" required>
<input type="text" name="username" required minlength="3" maxlength="20">
<input type="number" name="guests" min="1" max="10">
```


The `pattern` attribute must match the whole value; you do not need to add start and end anchors. Modern browsers compile it with the regular expression `v` flag, so some characters inside a character class, such as a literal hyphen, need escaping: write `[\w\-]` rather than `[\w-]`.


### Set your own message with setCustomValidity


Check the specific `validity` flags and set a message that tells people how to fix the problem, as in the short answer. Run the check once on load, so an empty required field already has your message on the first submit, and again on every `input` event.


You can also use it for rules HTML cannot express, such as two password fields matching:


```javascript
const password = document.querySelector("#password");
const confirm = document.querySelector("#confirm");

function checkMatch() {
  confirm.setCustomValidity(
    confirm.value === password.value ? "" : "Passwords do not match."
  );
}

password.addEventListener("input", checkMatch);
confirm.addEventListener("input", checkMatch);
```


### Style errors with :user-invalid


The `:invalid` selector matches as soon as the page loads, so every empty required field looks wrong before anyone touches it. `:user-invalid` only matches after the user has interacted with the field or tried to submit. It has been Baseline since late 2023 and works in all current major browsers.


```css
input:user-invalid {
  border-color: #c0392b;
  outline-color: #c0392b;
}

input:user-valid {
  border-color: #2e7d32;
}
```


### Show messages inline instead of in a bubble


If you want messages next to each field, add `novalidate` to the form. This turns off the browser bubbles but keeps the validation API, so you can read `validationMessage` yourself.


```html
<form id="signup" novalidate>
  <label for="email">Email</label>
  <input id="email" type="email" required aria-describedby="email-error">
  <p id="email-error" class="error" aria-live="polite"></p>
  <button>Sign up</button>
</form>
```


```javascript
const form = document.querySelector("#signup");

form.addEventListener("submit", (event) => {
  for (const field of form.elements) {
    const error = document.getElementById(`${field.id}-error`);
    if (error) error.textContent = field.validationMessage;
  }
  if (!form.checkValidity()) {
    event.preventDefault();
    form.querySelector(":invalid")?.focus();
  }
});
```


## Common pitfalls

- Calling `setCustomValidity("Some message")` and never clearing it. The field stays invalid forever.
- Styling `:invalid`, which shows errors on page load. Use `:user-invalid` instead.
- Treating browser validation as security. Anyone can bypass it, so validate again on the server.
- Expecting `type="email"` to check that an address exists. It only checks the rough shape.
- Forgetting the `v` flag rules in `pattern`, which can make the pattern silently stop working.
- Messages that only say "Invalid". Tell people what to type instead.

## Related reading

- [MDN: Client-side form validation](https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation)
- [MDN: setCustomValidity()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setCustomValidity)
- [MDN: :user-invalid](https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid)
