Input Types
What the type attribute of input changes: keyboards, validation and value formats for email, number, date, range and color, and when plain text is better.
Input Types
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `type` changes what an input is, not only how it looks. It sets the value format, the on-screen keyboard, the built-in validation, and the widget (a date picker, a color swatch, a slider).
- An unknown `type` falls back to `text`. In a test, `type="banana"` reported `type` as `text` while the attribute kept its odd value.
- `type="number"` is for quantities, not for identifiers. It accepts `e` and returns an empty value when the text is not a valid number, so zip codes, card numbers and phone numbers should be text with `inputmode`.
- `type="email"` trims whitespace and checks for an address shape; `type="tel"` does no validation at all. In a test, `not a phone` was valid in `tel`.
- Dates are exchanged as `YYYY-MM-DD` strings whatever the user sees on screen, and an invalid string is thrown away, leaving the value empty.
One element, many controls
Before HTML5, an <input> was a text box, a checkbox, a radio button or a button, and anything fancier meant JavaScript. The type attribute now selects among about twenty controls, and each one comes with behavior you would otherwise write yourself.
The reason is practical. A text box does not know that the user is typing an email. A phone shows the wrong keyboard, nothing checks the format, and you write the check by hand. type="email" tells the browser the intent, and the browser handles the keyboard, the validation and the autofill.
If a browser does not know a type, it treats it as text. In a test, type="banana" came back from the type property as "text" in headless Chromium, Firefox and WebKit, while getAttribute("type") still returned banana. This fallback is why newer types are safe to use: the worst case is a plain text box.
The common types
text, password, search: plain text. password masks what is typed; search is text with a clear button in some browsers and a semantic hint to assistive technology.
email: a text box that expects an address. The value is trimmed: an input holding " a@b.co " returned "a@b.co" in all three browsers. With multiple, it accepts comma-separated addresses. Validation only checks the shape: something, an @, something. It does not check that the mailbox exists.
url: expects an absolute URL, including a scheme. In a test, example.com without a scheme was :invalid with the message “Please enter a URL.” in Chromium.
tel: shows a phone keypad on mobile, and does nothing else. Phone number formats vary too much to validate by type, so not a phone was valid. Add a pattern if you need a format.
number: a quantity with a spinner. It supports min, max and step. More on its pitfalls below.
range: a slider. With no attributes it runs from 0 to 100 and starts at 50, which a test confirmed.
date, time, datetime-local, month, week: pickers. The value is a standard string, such as 2026-11-22 for a date, however the picker is displayed. In a test, setting "2026-13-45" on a date input left the value as an empty string, and setting "2026-11-22" worked. The date and time types are Baseline (newly available in October 2021 and widely available since April 2024, per web-features 3.47.0). month and week are the exception: the compatibility data shows no desktop Firefox support for them, so provide a fallback or use two fields.
color: a color swatch that opens the system picker. With no value it reported #000000.
file: a file chooser. It has its own lesson.
checkbox, radio, hidden, and the button types: covered in the next few lessons and in buttons and submit behavior.
The number trap
type="number" looks like the right tool whenever a field contains digits. It is the right tool only for quantities, such as how many tickets or what the age limit is.
It has two surprises. First, it lets the user type e, +, - and ., because 1e3 is a valid number. In a test, typing 1e3 produced the value "1e3" and valueAsNumber of 1000. Typing a lone e produced an empty value with validity.badInput set to true, so your code sees nothing while the field shows an e. That held in Chromium, Firefox and WebKit. Second, the value is a string, and the browser keeps the digits the user typed: 007 stayed "007" while valueAsNumber was 7.
Identifiers should be text. Zip codes, card numbers, one-time codes and phone numbers are not quantities, since nobody adds them or uses a spinner on them. Use type="text", add inputmode="numeric" to bring up the number keypad, and add autocomplete. The autocomplete and input modes lesson covers both.
Types and validation
Several types validate by themselves: email and url check their format, and number, range and the date types respect min, max and step. A value that breaks the rule makes the field :invalid and blocks submission. How that works, and how to tune it, is the subject of built-in form validation. The type is a first line of defense and never a replacement for checking on the server, since a script or curl can post anything.
What the video covers
The embed is a clip, 4:47 to 10:46, from Bro Code’s “Learn HTML forms in 10+ minutes!” (September 2023). The chapter list for that range reads password, email, phone, dates and number, and the clip ends where radio buttons begin. I chose it for those chapters. The rest of the video covers form basics, the submit and reset buttons, radios, a dropdown, checkboxes, a textarea and file upload, which other lessons cover. I could not read a transcript, so I do not describe the demos. It is a fast, code-along style with no talk about edge cases, so the number trap, the fallback behavior and the date format are from the lesson’s own tests, run in headless Chromium, Firefox and WebKit. I did not test the on-screen keyboards or date pickers, which need a real device.
Where this fits
This follows forms, labels and submission. The next lesson, textarea, select and datalist, covers the controls that are separate elements rather than input types.
Check yourself
A user types `e` into an `<input type="number">`. What do `value` and `validity.badInput` return?
The value is an empty string and `badInput` is `true`. The field lets the user type `e` because `1e3` is a valid number, but `e` alone is not a number, so the browser hides it from `value`. In a test, the same happened in Chromium, Firefox and WebKit.
You need a field for a UK postcode. Why not `type="number"`?
A postcode is an identifier, not a quantity. It contains letters, and for identifiers with leading zeros a number input is the wrong tool, since the spinner and wheel behavior makes no sense for them. Use `type="text"` with `autocomplete="postal-code"` and a `pattern` if you want a format check.
What is the value of an `<input type="range">` that has no `value` attribute and no `min` or `max`?
`50`. The default range is 0 to 100 and the handle starts in the middle. In a test it reported "50", and a `type="color"` input with no value reported `#000000`.
A browser has no date picker for `type="date"`, or you give it a bad string. What happens?
An unsupported type becomes a plain text field, so users type the date themselves, and a bad value is dropped. A test that set `"2026-13-45"` on a date input left the value empty. That is why a server should always validate dates itself.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.