HTML
Text Input
Numeric Input
Web Development
Coding Rules

HTML input that allows only numbers

Codemia Knowledge Hub ·

The simplest way to accept only numbers in an HTML input is type="number", optionally with min, max and step. If you need a text field (for example to allow formatting), add pattern="[0-9]*", and use a JavaScript keypress handler if you want to block non-digit keys as the user types. Fields like age, phone number or zip code are typical cases.

type="number"

HTML5 added input types that validate data automatically, and type="number" is the most direct one for numeric input:

html
<input type="number" id="age" name="age">

Limiting the range and step

These attributes work with type="number":

  • min: the minimum allowed value.
  • max: the maximum allowed value.
  • step: the allowed number intervals.
  • value: the default value.
html
<input type="number" id="age" name="age" min="0" max="130" step="1" value="25">

pattern on a text input

When you want type="text", for instance to allow spaces or hyphens in phone numbers, restrict the value with a regular expression in the pattern attribute:

html
<input type="text" id="phone" name="phone" pattern="[0-9]*" title="Please enter a number">

The browser checks the value against the pattern, here any number of digits, and treats a non-matching value as invalid. The title attribute supplies the message shown when the input is invalid.

Blocking non-digit keys with JavaScript

JavaScript can stop non-numeric characters from being entered at all:

javascript
1document.getElementById('numeric-input').addEventListener('keypress', function(event) {
2    if (!/[0-9]/.test(event.key)) {
3        event.preventDefault();
4    }
5});

The keypress listener checks each key and cancels anything that is not a digit.

Numeric keyboards on mobile

On mobile devices, type="number" also brings up the numeric keypad. Consider behavior across platforms when designing forms:

html
<input type="tel" id="mobile-number" name="mobile" pattern="[0-9]*">

type="tel" is meant for telephone numbers, but it also shows a numeric-friendly keyboard and still supports validation with pattern.

Which method to use

Attribute/MethodUse CaseBenefits
type="number"Numeric values onlyValidates input, can define range and steps
patternAlternative formats using type="text"Custom numeric formats, uses RegEx
JavaScriptDynamic input restrictionsReal-time control, greater flexibility
type="tel"Mobile-friendly numeric inputTriggers numeric keypad on mobile devices

The choice depends on whether you need formatting, how much control you want, platform behavior and whether validation must happen as the user types. Test on different devices and browsers for consistent behavior and accessibility.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms