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:
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.
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:
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:
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:
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/Method | Use Case | Benefits |
type="number" | Numeric values only | Validates input, can define range and steps |
pattern | Alternative formats using type="text" | Custom numeric formats, uses RegEx |
| JavaScript | Dynamic input restrictions | Real-time control, greater flexibility |
type="tel" | Mobile-friendly numeric input | Triggers 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.
Related reading
- HTML.ActionLink method
- Html.fromHtml deprecated in Android N
- Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
- HTTP GET request in JavaScript?
- I want to use JavaScript async/await
- I was going through MDN docs on Promises And came up with an example which i am not able to understand.Can Anyone explain the flow to me
- Identify Return Key action in React Native
- If Javascript is single threaded, how things like a clock work?
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.