Validate a numeric input without blocking typing

Let people type, paste and edit normally, then validate a clearly defined decimal format with accessible feedback and server-side checks.

Live example

Numbers, without the guesswork.

Try typing or pasting a non-negative decimal. Invalid text stays yours to correct.

Use a dot for decimals, without a sign or thousands separator. Up to 32 characters.

Your result will appear here.

Examples: 12, 12.5 and .5 are accepted; letters and multiple decimal points are not. Enable JavaScript to check a value.

Validate the value, not the key

A keypress filter misses paste and other input methods, and can interfere with editing shortcuts. Let people enter their value, then explain what needs correcting. The demo validates locally; it does not send or save the input.

First decide what “numeric” means. A quantity, decimal amount and reference number have different rules. This example accepts a non-negative decimal using a full stop, with up to two decimal places. It deliberately rejects commas, exponents and negative values.

Markup and validation

<label for="amount">Amount</label>
<input id="amount" name="amount" type="text" inputmode="decimal"
  maxlength="20" aria-describedby="amount-help amount-message">
<p id="amount-help">Use a full stop and up to two decimal places, for example 12.50.</p>
<button type="button" id="check-amount">Check amount</button>
<p id="amount-message" role="status"></p>
function isDecimalAmount(value) {
  return /^\d{1,12}(?:\.\d{1,2})?$/.test(value.trim());
}

const input = document.querySelector('#amount');
const message = document.querySelector('#amount-message');

document.querySelector('#check-amount').addEventListener('click', () => {
  const valid = isDecimalAmount(input.value);
  input.setAttribute('aria-invalid', String(!valid));
  message.textContent = valid
    ? 'That amount has a valid format.'
    : 'Enter a non-negative number with up to two decimal places, such as 12.50.';
});

input.addEventListener('input', () => {
  input.removeAttribute('aria-invalid');
  message.textContent = '';
});

Put the JavaScript in a deferred file. Invalid text stays in the field so it can be corrected. The mobile keyboard hint does not enforce validity, and different devices can still present different keys.

Choose the right field type

For a countable quantity, a native number input may be enough:

<label for="quantity">Quantity</label>
<input id="quantity" name="quantity" type="number" min="1" max="100" step="1" required>

Do not use numeric conversion for phone numbers, postcodes or identifiers where leading zeroes matter. For money, validate and convert to the payment system's integer minor units on the server; do not treat a JavaScript floating-point value as authoritative. Client-side checks improve feedback, but the receiving server must independently validate the submitted value and business limits.

References: number inputs and inputmode.

Original version13 April 2014

Kept here for reference and earlier links. The updated guide above is the recommended starting point; older code may depend on retired services or different software versions.

Javascript is a key way to help users validate forms and inputs, ensuring they can only enter data that is valid.

A common requirement is an element/input that should only accept numerical values.

The following Javascript does just that, only allowing numbers to be entered, if you type any other value, it simply doesn't get accepted/show.

$(function(){
  $('.number_only').keypress(function(event) {
    if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
      event.preventDefault();
    }
  });
})

Try typing a few things in the below input, you will notice only numbers get accepted:

Keep exploring

A couple more useful notes.