Sub topic 6
Choosing the right input type
This chapter assumes you can write a basic <form>, know that a control's name and value become submitted data, and know how to attach a <label> with for and id. It covers how the type attribute of <input> changes what the browser shows, what it checks, and what it sends.
What the type attribute controls
The type attribute on <input> selects a variant of the control. If you omit it, or use a value the browser doesn't recognize, the browser uses type="text". That fallback makes the specialized types safe to use. An old browser shows a plain text box, and your server still receives a string.
A type can change four things:
- The widget. A date picker, a color chooser, a slider, or a masked password field.
- The on-screen keyboard on touch devices.
- Built-in validation. An
emailinput rejects text that isn't shaped like an address. - The value format. Some types require a specific string format for
value, whatever the user sees.
A type never replaces server-side validation. Anyone can send a request without your form, so treat the type as a convenience for honest users.
Text-like types
These types all hold a single line of text and submit it as a string.
| Type | Use it for | Built-in validation | Notes |
|---|---|---|---|
| `text` | Names, free text | None | The default. |
| `email` | Email addresses | Must look like `name@host` | Add `multiple` to accept a comma-separated list. |
| `url` | Web addresses | Must start with a scheme such as `https:` | `example.com` alone is rejected. |
| `tel` | Phone numbers | None | Phone formats vary too much. Opens a phone keypad. |
| `search` | Search boxes | None | May show a clear button. Semantics only. |
| `password` | Secrets | None | Masks the characters. |
The browser trims line breaks from these values. For email, it also strips leading and trailing spaces before validating and submitting.
tel does not validate anything. If you need a pattern, add pattern yourself. Be careful, because phone formats differ between countries.
password hides characters on screen only. A POST body still carries the password in plain text unless the connection uses HTTPS. Pair it with the right autocomplete value (current-password or new-password) so password managers work.
Numbers: number, range, and when not to use them
type="number" shows a spinner and a numeric keypad. It supports min, max, and step. The default step is 1, so a value of 2.5 is invalid unless you set step="0.5" or step="any".
type="range" is a slider. It always has a value (the default is the midpoint), so the user can't leave it empty. Use it only when the exact number matters less than the rough position, such as a volume control. Show the current value next to it.
<label for="qty">Quantity</label>
<input id="qty" name="qty" type="number" min="1" max="10" step="1" value="1">
<label for="vol">Volume</label>
<input id="vol" name="vol" type="range" min="0" max="100" step="5" value="50">Use number only for values that are quantities you could add, subtract, or compare. Identifiers that happen to be made of digits are text. These break with number:
- ZIP and postal codes lose leading zeros in some code paths, and many countries use letters.
- Credit card numbers are too long for reliable numeric handling, and the spinner is useless for them.
- Phone numbers can contain
+, spaces, and parentheses.
For digit-only identifiers, use type="text" with inputmode="numeric" and a pattern. The inputmode attribute requests a keyboard layout without changing the control's type or validation:
<label for="zip">ZIP code</label>
<input id="zip" name="zip" type="text" inputmode="numeric"
pattern="[0-9]{5}" autocomplete="postal-code" required
title="Enter exactly five digits">Dates and times
The date and time types give the user a native picker. The submitted format is fixed, whatever locale the picker displays.
| Type | Submitted format | Example |
|---|---|---|
| `date` | `YYYY-MM-DD` | `2026-11-30` |
| `time` | `HH:MM` (seconds optional) | `08:19` |
| `datetime-local` | `YYYY-MM-DDTHH:MM` | `2026-11-30T08:19` |
| `month` | `YYYY-MM` | `2026-11` |
| `week` | `YYYY-Www` | `2026-W49` |
datetime-local carries no time zone. The server receives a wall-clock time and must know which zone it means. If you need an exact instant, convert on the client or store the user's zone separately.
min and max work on these types, using the same string format. The picker greys out dates outside the range, and validation rejects typed values outside it.
<label for="start">Start date</label>
<input id="start" name="start" type="date" min="2026-12-01" max="2027-12-31" required>Picker appearance differs between browsers, and you can't style it freely. If you need a custom calendar, you'll build one with JavaScript, but you then take on the accessibility work the native control gives you for free.
Other specialized types
colorshows a color chooser. Its value is always a lowercase#rrggbbstring, and it can't be empty. The default is#000000.hiddenisn't shown. It sends a fixed or script-set value, such as a record ID. The user can edit it in developer tools, so never trust it.fileselects files. It needsenctype="multipart/form-data"on the form and gets its own chapter.checkboxandradioare choice controls, covered in the next chapter.
Reading values in JavaScript
The value property is always a string. Some types add typed properties that save you from parsing:
<form id="order">
<label for="count">Count</label>
<input id="count" name="count" type="number" min="1" value="3">
<label for="when">Delivery date</label>
<input id="when" name="when" type="date" value="2026-12-01">
</form>
<script>
const form = document.querySelector('#order');
const count = form.elements.count;
const when = form.elements.when;
console.log(typeof count.value); // "string"
console.log(count.valueAsNumber); // 3
console.log(when.valueAsDate); // a Date at midnight UTC
</script>valueAsNumber returns NaN for an empty or invalid field. valueAsDate returns a Date at midnight UTC, so formatting it in a local zone can show the previous day. Keep the string value when you only need to send the date on.
A subtle point: invalid input reads as empty
If a user types abc into a number input, many browsers block most such characters. When bad input does get through, value is an empty string, even though the box displays text. The control then reports validity.badInput as true. Don't assume an empty value means the user typed nothing. Check validity.badInput too.
Deciding which type to use
Common errors
- Using
numberfor identifiers. Leading zeros, spinners, and scroll-wheel changes cause bugs. Usetextwithinputmode. - Expecting
telorsearchto validate. They don't. Addpatternwhen you need a format. - Sending a date in the wrong format. Setting
value="30/11/2026"on adateinput leaves the field empty. UseYYYY-MM-DD. - Treating
hiddenas secure. Anything the browser holds, the user can change. - Skipping the label. Every type needs one. Placeholder text is not a label, because it disappears on input and some assistive technology ignores it.
- Using
rangewhen the value must be deliberate. A slider always submits something, even if the user never touched it.
Exercise
A form asks for a five-digit ZIP code and a delivery date. A teammate used type="number" for the ZIP code and type="text" for the date, with the date typed as 30/11/2026. Which types and attributes would you use instead, and what format will the server receive for the date?
Show answer
Use type="text" with inputmode="numeric", pattern="[0-9]{5}", and autocomplete="postal-code" for the ZIP code. That keeps leading zeros and gives a numeric keypad without treating the ZIP as a quantity. Use type="date" for the delivery date. The server receives YYYY-MM-DD, for example 2026-11-30, whatever locale the picker displays.
Exercise
A user types the letters "abc" into a type="number" input and the script reads input.value. What does it get, and how can the script tell this apart from an empty field?
Show answer
value is an empty string, the same as for an empty field. The script can check input.validity.badInput, which is true when the control holds text the browser can't convert to a number.