Use HTML's built-in validation attributes: required, type, pattern, min, max, minlength and maxlength. The browser checks them when the visitor submits, blocks the form if one fails, and shows a message, with no JavaScript.
This is a convenience for honest visitors. It is not security, because anyone can post to your endpoint without using your form.
The attributes
| Attribute | What it checks | Example |
|---|---|---|
| required | The field is not empty, or the box is checked | input type="text" name="name" required |
| type="email" | The value has the shape of an email address | input type="email" name="email" |
| type="url" | The value is a full address including https:// | input type="url" name="website" |
| type="number" with min, max, step | A number within a range | input type="number" name="guests" min="1" max="10" |
| minlength, maxlength | How many characters were typed | textarea name="message" minlength="20" |
| pattern | The whole value matches a regular expression | input name="zip" pattern="[0-9]{5}" title="Five digits" |
Things worth knowing
- pattern must match the whole value, not part of it. Add a title to explain the expected format: browsers include it in the error message.
- An empty optional field always passes. pattern and type are only checked once something is typed, so combine them with required when the field is needed.
- type="email" accepts short addresses such as name@host with no dot. Add a pattern if you want to insist on one.
- For a group of radio buttons, put required on one of them and the group must have a choice.
- type="tel" does no checking at all, because phone formats vary. It only changes the keyboard on phones.
- The novalidate attribute on the form tag turns all of this off. Check nobody added it.
Styling errors with CSS
The :invalid and :valid selectors match fields by their current state. The catch is that an empty required field is invalid from the moment the page loads, so a red border appears before anyone has typed.
Current browsers support :user-invalid, which only matches after the visitor has interacted with the field:
input:user-invalid { border-color: #b00020; }
What HTML validation cannot do
- Compare two fields, such as a repeated email address.
- Check anything against a server, such as whether an address is already registered.
- Change the wording of the browser's messages, apart from the title on a pattern.
- Stop a bot or a curious person posting straight to the form's address.
The receiving side still has to cope
Whatever receives the form must handle missing and oversized values, because browser checks are optional for anyone posting directly. A form backend sets hard limits for you. FormTango accepts up to 100 fields, 5,000 characters per value and 100 KB of text per submission, and filters spam on every form.
Run your markup through the form checker to catch missing names and other mistakes, or start from the HTML form generator.
Common questions
- Can I validate an email address with HTML only?
- Yes. Use input type="email" with required. The browser checks the shape of the address before sending the form. It cannot check that the mailbox exists.
- Is HTML form validation secure?
- No. It helps real visitors fix mistakes, but it runs in the browser and can be skipped. Anything that matters must also be checked where the form is received.
- Why is my pattern attribute not working?
- The pattern has to match the entire value, and it is skipped when the field is empty. Check the expression against a full example and add required if the field is mandatory.
Written by the FormTango team. Facts about other services were read from their own pages on November 22, 2026. Spotted something out of date? Tell us and we will fix it.