Sub topic 4
Input types that validate for you
This chapter assumes you know how form controls are named and submitted, and that you have seen required, min, and max on an input. Here you will see how the type attribute adds its own checks, and which validity flag each check sets.
What a type adds
Each type value tells the browser what kind of data to expect. For several types, the browser then checks the value without any extra attributes or script.
| Type | Built-in check | Validity flag when it fails |
|---|---|---|
| `email` | Value looks like `name@host` | `typeMismatch` |
| `url` | Value is an absolute URL with a scheme such as `https:` | `typeMismatch` |
| `number` | Value parses as a number, and respects `min`, `max`, and `step` | `badInput`, `rangeUnderflow`, `rangeOverflow`, `stepMismatch` |
| `date`, `time`, `month`, `week` | Value is a real date or time, and respects `min`, `max`, and `step` | `badInput`, `rangeUnderflow`, `rangeOverflow`, `stepMismatch` |
| `range` | Value is held between `min` and `max` by the control itself | rarely invalid |
A form that uses them
<form id="booking" action="/book" method="post">
<label for="contact">Contact email</label>
<input id="contact" name="contact" type="email" required>
<label for="site">Website</label>
<input id="site" name="site" type="url" placeholder="https://example.com">
<label for="guests">Guests</label>
<input id="guests" name="guests" type="number" min="1" max="8" step="1" required>
<label for="arrival">Arrival date</label>
<input id="arrival" name="arrival" type="date" min="2026-12-01" required>
<button type="submit">Book</button>
</form>The browser blocks submission if contact is ana@ or if site is example.com without a scheme. It also rejects guests of 0, 9, or 2.5, and an arrival before 1 December 2026.
You can add multiple to an email input to accept a comma-separated list. The browser checks each address separately.
See which check failed
Each failure sets a specific flag on the control's validity object. Reading the flags lets you give a precise message instead of a generic one.
const form = document.getElementById('booking');
form.addEventListener('submit', (event) => {
for (const field of form.elements) {
if (!field.willValidate || field.validity.valid) continue;
const { typeMismatch, badInput, rangeUnderflow, rangeOverflow, stepMismatch } = field.validity;
const reasons = Object.entries({
typeMismatch,
badInput,
rangeUnderflow,
rangeOverflow,
stepMismatch,
})
.filter(([, failed]) => failed)
.map(([name]) => name);
console.log(`${field.name}: ${reasons.join(', ') || 'other'}`);
}
});Where typed validation falls short
- Empty is valid. Every type check is skipped for an empty value. Add
requiredif the field must be filled. emailis lenient. The browser acceptsa@bbecause a host without a dot is legal. If you need a real domain, add apatternor check on the server.urlonly checks the shape. It does not check that the site exists.javascript:alert(1)can also pass as an absolute URL, so never put a submitted URL into a link without checking its scheme.numberis for quantities. Do not use it for postal codes, phone numbers, or card numbers. It strips leading zeros in some code paths, shows spinner buttons, and accepts forms like1e3. Usetype="text"withinputmode="numeric"and apatterninstead.stepcounts frommin. Withmin="1"andstep="2", valid values are 1, 3, 5. If you set nomin, the base is thevalueattribute or zero.- Unparseable number input. In most browsers a number field with text like
12abcreportsbadInput, and itsvaluereads as an empty string. Sovalue === ''alone does not tell you the field is untouched. teldoes not validate. Phone formats vary too much. Usepatternif you need a rule.
Exercise
A field is <input type="number" min="0" max="100" step="5">. A user types 47. Which validity flag is set, and which value would the user need to type instead to pass?
Show answer
stepMismatch is set, because 47 is not a multiple of 5 counting from the min of 0. Typing 45 or 50 passes.
Exercise
Why can <input type="email"> with no required attribute still be submitted with a blank value?
Show answer
The browser skips type checks on an empty value. Only required makes an empty value invalid.