HTML Input type="number": A Live Tester
Use input type="number" for real quantities such as an age, a price or a count. min and max set the allowed range, step sets the increment (1 by default, or any for free decimals), and valueAsNumber gives you a number or NaN when the field is empty. For card numbers and postcodes use type="text" with inputmode="numeric" instead.
| Attribute | Example | Effect |
|---|---|---|
| min | min="0" | Lower values fail validation (rangeUnderflow) |
| max | max="10" | Higher values fail validation (rangeOverflow) |
| step | step="0.01" or step="any" | Default 1; values off the step fail (stepMismatch) |
| required | required | An empty field fails (valueMissing) |
| list | list="sizes" | Suggests values from a datalist |
| placeholder | placeholder="e.g. 2" | Hint shown while empty, never a label |
Updated
Number fields look simple, but the rules about steps, empty values and the spinner trip people up. Set the attributes in the tester, type anything into the field, and see the raw value, the parsed number and each validity flag update as you type.
Number input tester
<label for="qty">Quantity</label>
<input type="number" id="qty" name="qty" min="0" max="10" step="1">Which field for which number
| Data | Field | Why |
|---|---|---|
| Quantity, age, guests | type="number" min max | A real amount; the spinner and range checks help |
| Price | type="number" step="0.01" | Cents need a decimal step |
| Card number | type="text" inputmode="numeric" | An identifier, not an amount |
| Postal or ZIP code | type="text" inputmode="numeric" | Leading zeros and letters in some countries |
| One-time code | type="text" inputmode="numeric" autocomplete="one-time-code" | Digits only, no spinner |
| Phone number | type="tel" | Has its own input type |
Number field examples
Price field with a decimal step and invalid styling
Type 4.999 or a negative number and the outline turns red, because the value breaks the step or the minimum.
<style>
input { font: inherit; padding: 6px 8px; border: 2px solid #94a3b8; border-radius: 8px }
input:invalid { border-color: #dc2626 }
</style>
<label for="price">Price (USD)</label><br>
<input type="number" id="price" min="0" step="0.01" value="19.99">Hide the spinner buttons
appearance: textfield removes the arrows; the WebKit pseudo-elements cover browsers that still draw them. Arrow keys keep working.
<style>
.plain { appearance: textfield; -moz-appearance: textfield }
.plain::-webkit-inner-spin-button,
.plain::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0 }
</style>
<label for="n">Guests</label><br>
<input class="plain" type="number" id="n" min="1" max="12" value="2">Digits-only field without a spinner
inputmode hints at a numeric keyboard, the pattern checks for five digits, and leading zeros stay intact.
<label for="zip">ZIP code</label><br>
<input type="text" id="zip" inputmode="numeric" pattern="[0-9]{5}" autocomplete="postal-code">Read the number safely in JavaScript
valueAsNumber avoids string parsing, and the validity check catches out-of-range and off-step values.
const input = document.querySelector("#qty");
input.addEventListener("input", () => {
const n = input.valueAsNumber;
if (Number.isNaN(n)) return; // empty or not a number
if (!input.checkValidity()) {
console.log(input.validationMessage); // browser's own message
return;
}
console.log("Valid quantity", n);
});
How validation works
The browser checks the field against min, max and step, and sets a flag on the validity object for each rule it breaks. The form will not submit while a flag is set, and :invalid lets you style the field.
When the field holds text the browser cannot turn into a number, validity.badInput becomes true. Check that flag, or valueAsNumber for NaN, before trusting what you read; the tester shows exactly what your browser reports.
Accessibility and mobile
A number input is exposed to assistive technology as a spinbutton. If stepping up and down is not useful for the value, a text field with inputmode="numeric" avoids accidental changes from the scroll wheel or arrow keys.
Ask whether the value is an amount you could add to or subtract from. If it is, use a number field; if it is a code that happens to contain digits, use text.
How it works
- Enter min, max and step values. Leave a box empty to drop that attribute.
- Type into the test field or use the arrow keys and spinner.
- Read value, valueAsNumber and the validity flags in the panel beside it.
- Copy the generated HTML once the field behaves the way you want.
Tips
- Always pair the field with a visible label element; a placeholder disappears as soon as someone types.
- Read valueAsNumber instead of parsing value yourself, and check for NaN before using it.
- Validate again on the server. Browser checks help people, but anyone can submit a form without them.
- For money, step="0.01" accepts cents; for free decimals use step="any".
- Leave type="number" off fields like phone numbers, card numbers and postcodes, where a spinner makes no sense.
Frequently asked questions
Why does my number input reject 1.5?
The default step is 1, so only whole numbers are valid unless the step base is not an integer. Add step="0.5", step="0.01" or step="any" to accept decimals.
What does valueAsNumber return for an empty field?
NaN. The value property returns an empty string in the same case. You can also set valueAsNumber to write a number back into the field.
How do I remove the arrows from a number input?
Set appearance: textfield on the input. The up and down arrow keys still change the value, because appearance only changes the look. WebKit and Blink also draw spin buttons through pseudo-elements, which the example below hides as well.
Should I use type=number or inputmode=numeric?
Use type="number" when the value is a real quantity and a spinner helps. Use type="text" with inputmode="numeric" and a pattern for digit strings such as card numbers or postal codes, which are identifiers rather than amounts.
Is an empty number field valid?
Yes, unless it has the required attribute. A number input counts as valid when it is empty or holds a single valid number.
Related: HTML and CSS cheat sheet, Regex cheat sheet, HTML accessibility checker, JavaScript event listeners, HTML validator.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- HTML Links: Anchors, Buttons, Images and New Tabs
- HTML Popover API: Live Playground and Reference
- HTML Table Borders with CSS
- How to Convert HTML to PDF
- HTTP 304 Not Modified, Simulated Request by Request
- HTTP 405 Method Not Allowed: What It Means and How to Fix It
- HTTP 429 Too Many Requests: Wait, Then Retry the Right Way
- HTTP Status Codes for Website Owners
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App