Allow only numbers in input using JavaScript

The following example shows how to enter only numbers in input using JavaScript.We can filter the input values with the following 'onlyNumbers' and 'numberValidation' user defined JavaScript functions.

<!DOCTYPE html>
  <h1>Allow only Numeric Input</h1>
  <input type='text' onkeypress="return onlyNumbers(event)" onkeyup="return numberValidation(event)">
  function onlyNumbers(e)
    var c=e.which?e.which:e.keyCode; 
      return false;
  function numberValidation(e){
      e.target.value = e.target.value.replace(/[^\d]/g,'');
      return false;
Try it Yourself

Live Demo