I think the easy way to do this would be:

<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />

But the problem comes up when you paste some text then HTML5's number type input may be a better choice:

<input type="number" />

BTW you can find better suggestions if you search the SO like this.

Answer from Morteza Tourani on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Element › keypress_event
Element: keypress event - Web APIs | MDN
The keypress event is fired when a letter, number, punctuation, or symbol key is pressed, or else when the Enter key is pressed — including when the Enter key is pressed in combination with the Shift key or Ctrl key. Otherwise, when a modifier key such as the Alt, Shift, Ctrl, Meta, Esc, ...
🌐
W3Schools
w3schools.com › jsref › event_onkeypress.asp
onkeypress Event
To detect if the user presses a key, always use the onkeydown event. It works for all keys. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to ...
Discussions

jquery - Trying to get numbers from keypress document, javascript - Stack Overflow
Numbers are 48 through 57, so... $(document).keypress(function (e) { var key = e.keyCode || e.charCode; if (key >= 48 && key <= 57) { alert('You pressed ' + (key - 48)); } }); ... It doesn't work in Firefox, because for keypress, you need to use charCode in Firefox (keyCode is always zero in a keypress in Firefox). ... Keypress events ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Number validate at keypress - Stack Overflow
I validate the phone number using below code its working fine but i allow char at first time while user entering the values. how i can solve it. . . . $('.Number').keypress(function () { $('.N... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
javascript - How do I check if the key pressed on a form field is a digit (0 - 9)? - Stack Overflow
I'm using the onkeyup event on a form field in JavaScript, and I'm wanting to check if the key the key pressed is a numeric digit - i.e. 0 - 9, so I can then do something with the input. More on stackoverflow.com
🌐 stackoverflow.com
javascript - KeyboardEvent: how to get numeric key code while 'Shift' is pressed? - Stack Overflow
In my application, I need to identify if the user pressed any of the number keys (either main keyboard or numpad) as well as if shift/ctrl/alt keys are pressed at the same time. I need this because... More on stackoverflow.com
🌐 stackoverflow.com
🌐
JavaScript.info
javascript.info › tutorial › browser: document, events, interfaces › ui events
Keyboard: keydown and keyup
... Letter keys have codes "Key<letter>": "KeyA", "KeyB" etc. Digit keys have codes: "Digit<number>": "Digit0", "Digit1" etc. Special keys are coded by their names: "Enter", "Backspace", "Tab" etc.
🌐
GitHub
gist.github.com › 6789046
Only Numbers JavaScript · GitHub
with the previous solution the user is allowed to enter the decimal dot several times, this version has a fix for that (typescript): `const handleInputKeydown = (event: KeyboardEvent) => { // it's an special key or a combination like ctrl+c if ((event.key.length > 1) || event.ctrlKey) { return } // it's a digit if (event.key.match(/\d/)) { return } // it's a dot and is not already into the value if (event.key === '.' && ((event.target as HTMLInputElement).value.indexOf('.') === -1)) { return } event.preventDefault()
🌐
C# Corner
c-sharpcorner.com › blogs › numeric-keypress-validation-in-textbox-using-javascript1
Numeric KeyPress Validation in TextBox using JavaScript
May 19, 2020 - Right-click on the project and select Add -> Web Form and name it as Home.aspx. Paste the following JavaScript function in the head section of the Home.aspx page. ... <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="NumericKeyPressValidation.Home" %> ... Numeric Value: <input type="text" id="text1" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" />
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › javascript-keycode-list-keypress-event-key-codes
JavaScript Keycode List – Keypress Event Key Codes for Enter, Space, Backspace, and More
January 8, 2021 - JavaScript keyboard events help you capture user interactions with the keyboard. Like many other JavaScript events, the KeyboardEvent interface provides all the required properties and methods for handling every keystroke a user makes using the keyb...
🌐
Logfetch
logfetch.com › js-event-key-number
How to Check If Key Press (event.key) is a Number in JavaScript - LogFetch
July 29, 2020 - const getCode = (e) => { e = e || window.event; return e.key; }; const handleKeyPress = (e) => { const key = getCode(e); if (isFinite(key)) { console.log(`Number ${key} was pressed!`); } }; document.addEventListener("keypress", handleKeyPress); How to Validate a UUID with Regular Expressions in JavaScript
🌐
Toptal
toptal.com › developers › keycode
JavaScript Key Code Event Tool | Toptal®
KeyCode.Info allows users to press any key and instantly get the JavaScript Key or Key Code KeyboardEvent. Check out the Tool and Event List.
🌐
CodePen
codepen.io › yewkeong › pen › LNEQby
Allow numbers only on keypress with jQuery
<div class="form-group"> <label class="col-md-4 control-label">Digits only:</label> <div class="col-md-4"> <input type="text" name="digitsOnly" class="form-control" id="digitsOnly"> </div> </div> ! CSS Options · Format CSS · View Compiled ...
🌐
ASPSnippets
aspsnippets.com › Articles › 948 › Numeric-KeyPress-Validation-Allow-only-numbers-validation-on-KeyPress-in-TextBox-using-JavaScript
Numeric KeyPress Validation Allow only numbers validation on KeyPress in TextBox using JavaScript
January 29, 2019 - In this article I will explain how to perform Numeric validation in TextBox on KeyPress using JavaScript in such a way that the TextBox will accept only numbers i.e. numeric values or digits in TextBox. <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css"> body · { font-size: 9pt; font-family: Arial; } </style> </head> <body> Numeric Value: <input type="text" id="text1" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" /> <span id="error" style="color: Red; display: none">* Input digits (0 - 9)</span> <script type="text/javascript"> var specialKeys = new Array(); specialKeys.push(8); //Backspace ·
🌐
JavaScript Kit
javascriptkit.com › javatutors › javascriptkey3.shtml
Form validation using the keyboard events
Click here for comprehensive JavaScript tutorials, and over 400+ free scripts!
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › KeyboardEvent › key
KeyboardEvent: key property - Web APIs | MDN
When key 2 is pressed, another keydown event is fired for this new key press, and the key property value for the event is set to be the string @ for the U.S keyboard type and " for the UK keyboard type, because of the active modifier shift key. The beforeinput and input events are fired next because a character key has been produced.
🌐
jQuery
api.jquery.com › keypress
keypress event | jQuery API Documentation
While browsers use differing properties to store this information, jQuery normalizes the .which property so you can reliably use it to retrieve the character code. Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For ...