---
title: "Why does input type=number let people type letters, and what should I use instead?"
description: "Number inputs accept e, plus, minus and dots, and handle codes like PINs badly. Learn when to use a text input with inputmode and pattern instead, and when number is still right."
url: https://www.danny.engineering/answer/html/why-does-input-typenumber-let-people-type-letters-and-what-should-i-use-instead
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["HTML"]
---

# Why does input type=number let people type letters, and what should I use instead?

## The short answer


`type="number"` is meant for quantities, not for any value made of digits. Browsers let people type `e`, `+`, `-` and `.` because those are valid in numbers like `1e3` or `-2.5`, and some browsers accept any character and simply report an empty value. For things like PINs, card numbers, postcodes and verification codes, use a text input with `inputmode` and `pattern`.


```html
<label for="code">Verification code</label>
<input
  id="code"
  name="code"
  type="text"
  inputmode="numeric"
  pattern="[0-9]{6}"
  autocomplete="one-time-code"
  required
/>
```


## Why it happens


A number input parses whatever is typed as a floating point number. The characters `e`, `E`, `+`, `-` and `.` are all part of that syntax, so the browser cannot block them while someone is still typing. Chromium browsers block most other letters, but Firefox lets people type anything and only checks the value afterwards.


When the text does not parse, the browser does not give you the bad text. The `value` property becomes an empty string and `validity.badInput` becomes `true`. That is why a form can seem to "lose" what someone typed.


Number inputs also do things you rarely want for codes:

- Leading zeros can be lost once the value is treated as a number, so `007` turns into `7`.
- Long values such as card numbers go past the safe integer range and lose precision.
- Spin buttons and, in some browsers, the arrow keys or scroll wheel change the value by accident.
- Screen readers announce it as a spin button, which is confusing for a postcode.

A good rule: if adding one to the value makes no sense, it is not a number.


## How to fix it


### Use inputmode for digit-only values


`inputmode="numeric"` tells mobile browsers to show a digit keypad while keeping the field a normal text input. The value stays a string, so leading zeros and long values are safe.


```html
<label for="pin">PIN</label>
<input id="pin" type="text" inputmode="numeric" pattern="[0-9]{4}" />
```


Use `inputmode="decimal"` when people need a decimal point too, for example a price field.


```html
<label for="amount">Amount</label>
<input id="amount" type="text" inputmode="decimal" pattern="[0-9]+([.,][0-9]{1,2})?" />
```


### Add pattern for validation


`pattern` is a regular expression that must match the whole value. You do not need `^` or `$`, because the browser anchors it for you. When it does not match, the field is invalid, the form will not submit, and the `:invalid` CSS pseudo-class applies. Pair it with a visible hint or a `title` that explains the expected format.


You can also check it in JavaScript before sending data:


```javascript
const input = document.querySelector('#code');

if (!input.checkValidity()) {
  input.reportValidity();
}
```


### Keep type=number for real quantities


For values people count or measure, such as a quantity in a basket, an age or a number of guests, `type="number"` is still a good choice. Set `min`, `max` and `step` so the browser can validate the range.


```html
<label for="qty">Quantity</label>
<input id="qty" type="number" min="1" max="20" step="1" value="1" />
```


Read the value with `valueAsNumber`, which gives you a number or `NaN`, and check `validity.badInput` if you need to tell "empty" apart from "typed something invalid".


```javascript
const qty = document.querySelector('#qty');

if (qty.validity.badInput) {
  console.log('Not a number');
} else {
  console.log(qty.valueAsNumber);
}
```


### Always validate on the server


Browser validation is a convenience for users. Anyone can send any value to your server, so check the format again there.


## Common pitfalls

- Using `maxlength` on a number input. It is ignored for `type="number"`; use `max` or switch to a text input.
- Stripping letters with a keydown handler. It breaks paste, autofill and assistive technology. Let people type, then validate.
- Using `pattern="\d*"` and forgetting that an empty value still matches. Add `required` if the field must be filled in.
- Relying on `pattern` alone to get a number keypad. Modern mobile browsers use `inputmode` for that.
- Forgetting `autocomplete="one-time-code"` on verification code fields, which lets phones offer the code from a text message.

## Related reading

- [MDN: input type="number"](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/number)
- [MDN: inputmode](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/inputmode)
- [MDN: pattern attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/pattern)
