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.

Attributes of a number field
AttributeExampleEffect
minmin="0"Lower values fail validation (rangeUnderflow)
maxmax="10"Higher values fail validation (rangeOverflow)
stepstep="0.01" or step="any"Default 1; values off the step fail (stepMismatch)
requiredrequiredAn empty field fails (valueMissing)
listlist="sizes"Suggests values from a datalist
placeholderplaceholder="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

type=number compared with a numeric text field
DataFieldWhy
Quantity, age, gueststype="number" min maxA real amount; the spinner and range checks help
Pricetype="number" step="0.01"Cents need a decimal step
Card numbertype="text" inputmode="numeric"An identifier, not an amount
Postal or ZIP codetype="text" inputmode="numeric"Leading zeros and letters in some countries
One-time codetype="text" inputmode="numeric" autocomplete="one-time-code"Digits only, no spinner
Phone numbertype="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

  1. Enter min, max and step values. Leave a box empty to drop that attribute.
  2. Type into the test field or use the arrow keys and spinner.
  3. Read value, valueAsNumber and the validity flags in the panel beside it.
  4. Copy the generated HTML once the field behaves the way you want.

Tips

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.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap