09 / 17

How to restrict a field to accept only numbers?

Restricting Input to Numbers in HTML

In HTML, you can restrict an input field to accept only numbers by using the type="number" attribute. This ensures that the browser only allows numeric input and shows a numeric keyboard on mobile devices.

Ways to Restrict Input to Numbers
  1. 1

    Use <input type="number"> to accept only numeric values.

  2. 2

    Add min and max attributes to set allowed numeric ranges.

  3. 3

    Use the step attribute to control increments (e.g., step="1" for integers).

  4. 4

    Alternatively, use pattern="[0-9]+" with <input type="text"> for custom numeric-only validation.

Example of Numeric Input