Sub topic 6
Choosing the right input type
This chapter assumes you can write a basic <form>, label a text input, and know that a control's name becomes the key in the submitted data. Here you will learn how the type attribute changes what the browser shows, what it validates, and what it sends.
What `type` changes
The type attribute on <input> selects a control. An unknown or missing value falls back to text, so a typo such as type="emial" silently gives you a plain text box.
A specialized type can change three things:
- The on-screen keyboard on touch devices.
- The built-in validation, so the browser can reject bad values before submission.
- The widget, such as a date picker or a number stepper.
The submitted value is always a string. A type="number" input still sends "42", not the number 42.
Common types
| Type | Use it for | Notes |
|---|---|---|
| `text` | Free-form short text | The default. |
| `email` | Email addresses | Validates the basic `user@host` shape. |
| `url` | Web addresses | Requires a scheme such as `https:`. |
| `tel` | Phone numbers | No format check. Add `pattern` if you need one. |
| `password` | Secrets | Masks the characters on screen only. |
| `number` | Quantities | Supports `min`, `max`, and `step`. |
| `date` | Calendar dates | Submits `YYYY-MM-DD`. |
| `search` | Search boxes | Some browsers add a clear button. |
A form with several types
<form action="/register" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<label for="site">Website</label>
<input id="site" name="site" type="url">
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" autocomplete="tel">
<label for="qty">Quantity</label>
<input id="qty" name="qty" type="number" min="1" max="10" value="1">
<label for="birthday">Birthday</label>
<input id="birthday" name="birthday" type="date">
<button type="submit">Register</button>
</form>If you enter not-an-email and press the button, the browser blocks submission and shows a message on the email field. No JavaScript is involved.
Numbers are not always numbers
Use type="number" for values you count or measure, such as a quantity. Do not use it for identifiers such as ZIP codes, card numbers, or phone numbers. Number inputs strip leading zeros in some cases, show steppers that make no sense for identifiers, and respond to the mouse wheel.
For digit-only identifiers, use a text input with a keyboard hint and a pattern:
<label for="zip">ZIP code</label>
<input id="zip" name="zip" type="text" inputmode="numeric"
required pattern="[0-9]{5}" title="Enter exactly five digits">inputmode only changes the keyboard. It does not validate anything. The pattern attribute does the validation.
Common errors
- Trusting the browser alone. Client-side checks help users but can be bypassed. The server must validate every value again.
- Expecting
telto validate. It accepts any text because phone formats vary by country. - Assuming
datelooks the same everywhere. The displayed format follows the user's locale, but the submitted value is alwaysYYYY-MM-DD. - Using
passwordfor security. It hides characters from onlookers. It does not encrypt anything, so you still need HTTPS.
Exercise
A form asks for a five-digit ZIP code. A teammate used type="number". Give two reasons to change it, and say what to use instead.
Show answer
Number inputs can drop leading zeros (such as in the ZIP code 02134) and show steppers and wheel behavior that make no sense for an identifier. Use type="text" with inputmode="numeric" to get the numeric keyboard, plus pattern="[0-9]{5}" and required for validation.