HTML5 Input Types in Practice: Email, Date, Number, and When to Use Them
HTML5 input types in practice: email, tel, url, number, date, time, range, color and file, what each gives mobile users, and when number is wrong.
type attribute on an input decides three things at once: which keyboard a phone shows, what built-in validation runs, and what value format your code receives. email, tel, url and the date and time types are safe defaults that improve mobile input for free. number is the one to be careful with: it is meant for quantities you might increment, not for anything that merely contains digits, such as card numbers, phone numbers, PINs or postal codes.
A checkout form has twelve fields, and on a phone every one of them opens the full alphabetic keyboard. The user switches to the number pad to type a phone number, switches back for the email, hunts for the @ key, then scrolls a date dropdown with 100 years in it. Each field takes seconds longer than it should. Meanwhile, the card number field uses type="number", strips leading zeros and changes value when someone scrolls the page over it.
HTML5 input types are the values of an <input> element’s type attribute beyond the original text, password, checkbox and radio. HTML5 added email, tel, url, search, number, range, color, date, time, datetime-local, month and week. Browsers that do not recognize a type fall back to text, so using them never breaks a form. It only makes the form better where support exists.
What does each HTML5 input type give you?
| Type | Mobile keyboard | Built-in validation | Value you receive |
|---|---|---|---|
email |
Letters with @ and . keys | Must look like an address | Trimmed string |
tel |
Phone dial pad | None (formats vary worldwide) | String |
url |
Letters with / and .com keys | Must be an absolute URL with a scheme | String |
number |
Numeric, varies by OS | Must be a number; min, max, step |
String, plus valueAsNumber |
date |
Native date picker | Valid date; min, max |
YYYY-MM-DD, plus valueAsDate |
time |
Native time picker | Valid time; step for seconds |
HH:MM in 24-hour form |
datetime-local |
Date and time picker | Valid date and time | YYYY-MM-DDTHH:MM, no time zone |
range |
A slider, no keyboard | Always valid, clamped to range | Numeric string |
color |
Native color picker | Always valid | Lowercase #rrggbb |
search |
Keyboard with a Search or Go key | None | String; Escape clears it in many browsers |
The “value you receive” column matters most. Date inputs always submit 2023-04-11, regardless of whether the user sees 11/04/2023 or 4/11/2023 on screen. The display follows the user’s locale; the value is always ISO 8601. That alone removes an entire family of date parsing bugs.
Email, tel and url: always use them
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<label for="phone">Mobile number</label>
<input id="phone" name="phone" type="tel" autocomplete="tel">
<label for="site">Website</label>
<input id="site" name="site" type="url" placeholder="https://example.com">
There is no downside to these three. A few details are worth knowing:
- email validation is deliberately loose. It checks for a basic
local@domainshape, not whether the domain exists. Keep server-side validation and confirmation emails. Addmultipleto accept a comma-separated list. - tel does no validation at all, because phone formats differ across countries. It exists for the keyboard. Add a
patternif you need one specific format, and accept spaces, dashes and a leading plus. - url rejects
example.comwithout a scheme. Many users type exactly that, so either addhttps://as a visible prefix or usetype="text"withinputmode="url"and normalize on the server.
When should you use type=number?
type="number" is designed for numeric values a person might increase or decrease: a quantity in a cart, a number of guests, an age, a price filter. It brings spin buttons, arrow-key stepping and numeric validation. It is the wrong choice for strings that happen to be digits, and the problems are concrete:
- Leading zeros vanish. A PIN of
0042or a postal code of01234can come back as42or1234once your code treats it as a number. - Scrolling changes the value. In some desktop browsers, using the mouse wheel over a focused number input increments it, so a user scrolling the page silently edits the field.
maxlengthis ignored. It does not apply to number inputs at all.- Odd characters are allowed.
e,+,-and.can be typed, because1e5is a valid number. - Invalid input looks empty. If the content is not a valid number,
input.valuereturns an empty string, so your code cannot tell “empty” from “invalid” without checkingvalidity.badInput. - Screen reader support is uneven for spin button announcements.
For card numbers, OTPs, postal codes, account numbers and ID numbers, use a text input with a numeric keyboard instead:
<label for="otp">Verification code</label>
<input id="otp" name="otp" type="text" inputmode="numeric"
pattern="[0-9]{6}" autocomplete="one-time-code" maxlength="6">
inputmode="numeric" shows the digit keypad on phones, pattern validates the format, leading zeros survive, and maxlength works. A quick test: if adding 1 to the value makes no sense, it is not a number input.
Are date and time inputs ready to use?
<label for="checkin">Check-in</label>
<input id="checkin" name="checkin" type="date" min="2023-04-12" max="2024-04-11" required>
<label for="slot">Preferred time</label>
<input id="slot" name="slot" type="time" min="09:00" max="18:00" step="1800">
Native date and time inputs now work in Chrome, Edge, Firefox and Safari on both desktop and mobile; Safari added them on macOS in version 14.1. The mobile pickers in particular are excellent, accessible and localized, and far better than most JavaScript date widgets. step="1800" on a time input means 30-minute steps.
They are not right for every date:
- Dates of birth are tedious to reach in a calendar that opens on today. Three separate fields for day, month and year, or a single text field with a clear format, is often faster. The UK government design system recommends exactly this for memorable dates.
monthandweekare still not supported in Firefox or desktop Safari, where they fall back to plain text. Avoid them unless you only target Chromium.- Time zones are not handled.
datetime-localreturns a wall-clock time with no zone, so store the user’s zone alongside it. - Styling of the picker popup is limited. If the brand requires a custom calendar, you are building a widget, and its accessibility becomes your responsibility.
Browsers also support input.showPicker(), which opens the native picker from JavaScript, for example when a user clicks a custom calendar icon next to the field.
Range, color and file
Range is fine for approximate values, like volume or a price filter, but users cannot see or type the exact value. Pair it with an <output> element that displays the current value, or offer a number input next to it for precision.
<label for="budget">Max budget</label>
<input id="budget" name="budget" type="range" min="500" max="5000" step="500" value="2000"
oninput="budgetOut.value = this.value">
<output id="budgetOut" for="budget">2000</output>
Color gives you the OS color picker and always returns a six-digit lowercase hex value. It has no alpha channel.
File inputs accept accept="image/*" or a list of extensions to filter the picker, and capture="environment" or "user" to open the rear or front camera directly on mobile. accept is a hint, not a security control; always check type and size on the server.
How do you choose an input type quickly?
| Field | Use |
|---|---|
| Email address | type="email" |
| Phone number | type="tel" |
| Quantity, guests, age | type="number" with min and max |
| Card number, OTP, PIN, postal code, Aadhaar or PAN-style IDs | type="text" with inputmode="numeric" and pattern |
| Price with decimals | type="text" with inputmode="decimal", or number with step="0.01" |
| Appointment or booking date | type="date" |
| Date of birth | Separate day, month and year fields |
| Approximate setting | type="range" with an output |
Frequently Asked Questions
What happens if a browser does not support an input type?
It falls back to a plain text input. The field still works and submits, it just loses the special keyboard, picker or validation, so using newer input types never breaks a form.
Should I use type=number for phone numbers or credit cards?
No. Use type=tel for phone numbers, and type=text with inputmode=numeric and a pattern for card numbers, OTPs and postal codes. Number inputs can drop leading zeros, ignore maxlength and change value when the user scrolls.
What format does input type=date return?
Always YYYY-MM-DD, such as 2023-04-11, regardless of how the date is displayed to the user. The visible format follows the user’s locale, but the submitted value is ISO 8601.
Is input type=date supported in all browsers?
Yes. Chrome, Edge, Firefox and Safari support date, time and datetime-local on desktop and mobile. The month and week types are still missing in Firefox and desktop Safari.
Let the keyboard do the work
Picking the right type is the cheapest form improvement there is: one attribute, and every phone user gets a better keyboard and every browser gets built-in checks. Use the specific types by default, reserve number for real quantities, and reach for inputmode when you need digits without numeric behavior. Good input types still need good labels and structure, which build on the element choices from semantic HTML explained. For the full reference, see the MDN input element reference and web.dev’s forms course. Related reading on this site: native form validation and autocomplete and inputmode.
Originally posted on 11 April 2023 and post last modified on 11 April 2023