<html> <head> <title>example</title> </head> <body> <div> <input> </div> <script> var input = document.querySelector('input'); input.onkeypress = function (e) { return !(/\D/.test(String.fromCharCode(e.charCode))); } </script> </body> </html>