Explain Codes LogoExplain Codes Logo

Number input type that takes only integers?

html
input-validation
regex
javascript
Nikita BarsukovbyNikita Barsukov·Aug 31, 2024
TLDR

If you need an HTML input field that accepts only integers, use type="number" and step="1".

Example:

<input type="number" step="1">

This essentially makes the input field integer-friendly, blocking the decimals. For more bullet-proof precision, bring in the pattern attribute reinforced with a regular expression.

Positive integer enforcement with pattern attribute

Setting the input type to "text" and then using the pattern attribute with a regex \d+ ensures your field only receives beautiful positive integers.

Here's the code for it:

<input type="text" pattern="\d+" title="Positive integers rule! Anything else drools.">

In this scenario, \d+ regex is your bouncer, allowing only digits to get in. Anything else (decimals, letters, etc.) will be shown the door. Equipped with a useful title attribute, your user receives a hint for the right input.

JavaScript for on-spot input cleanup

Want your integer field to be allergic to non-number characters? Here’s the game-plan:

  1. Set the input type to "text".
  2. Leverage the oninput event and bring in some JavaScript tech to keep the field strictly integers.

HTML:

<input type="text" id="integerOnly" oninput="integerPlugin(this)">

JavaScript:

function integerPlugin(input) { // No character that's not a number will survive this cleansing! 🔥🧹 input.value = input.value.replace(/[^\d]+/g, ''); }

The JavaScript snippet casually exterminates any non-number characters as the user types, providing an immediate validation and error prevention.

Beware of browser variability

Different browsers may interpret the input attributes differently. Though HTML5 validation attributes such as pattern are supported among most modern browsers, it's nonetheless essential to go on a cross-browser testing adventure to ensure the form holds up well on various platforms.

Insights and tips on input validation

Deciphering the step attribute

Don't rely on the step="1" attribute to prevent decimal input in all scenarios. Some browsers are a bit naughty and might still allow the user to type a decimal, although they won't let the form submission go through until it's corrected to a valid integer.

The magic of regular expressions

Regular expressions (Regex) are like your little validation wizards. The \d+ regex in this example makes sure that only digits are on the guest list. No periods or commas allowed!

Lean on native features when you can...

Making use of native HTML5 validation features usually leads to cleaner code. Less scripts mean less load for the browser and lesser potential bugs.

...But don't forget about JavaScript

On the flip side, JavaScript solutions pump up the form's interactivity by providing real-time validation feedback, which greatly enhances user experience. It’s like having a personal waiter telling you what’s wrong with your order in a polite manner before you even finish placing it.