← Back to HTML & CSS, page 6

html-css-0104 · Easy fresher

How do input types and the inputmode attribute improve a form, and what do they not do?

The type attribute tells the browser what to expect, so it can show a suitable keyboard or picker and run basic checks. inputmode changes only the keyboard. These are conveniences, not security, so the server must check again.

Full answer

The type attribute of an input tells the browser what kind of value to expect. The browser can then show a better keyboard on phones, offer a date picker, and run basic checks.

  • email shows a keyboard with the @ sign and checks that the value looks like an address.
  • tel shows a number pad. It does not check the format, because phone numbers differ by country.
  • url checks for a valid address with a scheme.
  • number allows digits only, gives spinner arrows, and works with min, max and step. It is a poor choice for codes such as a postal code or a card number, because it removes leading zeros and shows arrows that make no sense.
  • date, time and datetime-local show a picker. The value is always in a fixed format such as 2026-12-04, whatever the display.
  • search is a text box with a clear button in some browsers. password hides the characters.

inputmode changes only the keyboard and not the checks. For a postal code or a one-time code, use type="text" with inputmode="numeric". The pattern attribute then checks the format.

Types are a help and not security. Users can type anything into a plain text field, and a script can send anything to the server, so the server must check every value again.

If a script sets the email field to abc, its validity reports a type mismatch. The number field rejects letters and keeps an empty value. The postal code field keeps its leading zeros as text, and its pattern rejects the value 123.

The keyboards themselves depend on the phone, so check them on a real device.

html
<form id="f">
  <input type="email" id="mail" name="mail" value="">
  <input type="number" id="num" name="age" min="1" max="120">
  <input type="text" id="zip" name="zip" inputmode="numeric" pattern="[0-9]{5}" autocomplete="postal-code">
  <input type="date" id="day" name="day">
</form>

The check set values by script and read the validity state. The phone keyboards were not tested. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • type picks the keyboard, picker and basic checks.
  • Do not use number for codes that keep leading zeros.
  • inputmode changes the keyboard without adding checks.
  • pattern and the server do the real checking.
  • Test keyboards on real phones.

Common mistakes

  • Using type="number" for postal codes, phone numbers or card numbers.
  • Assuming type="email" makes a value safe or real. It only checks the shape.
  • Skipping server-side checks because the browser already checked.

Follow-ups

  • What does the step attribute do on a number input?
  • Why does a date input send a fixed format?

Tips

  • Give one example where the type is wrong, such as a postal code as a number.
  • Say that inputmode changes the keyboard only.