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
🌐
C# Corner
c-sharpcorner.com › blogs › numeric-keypress-validation-in-textbox-using-javascript1
Numeric KeyPress Validation in TextBox using JavaScript
May 19, 2020 - In this blog we will learn to perform perform Numeric validation in TextBox on KeyPress using JavaScript which will accept only numeric digits in TextBox.
🌐
Talkerscode
talkerscode.com › howto › only-number-validation-in-javascript-onkeypress.php
Only Number Validation In JavaScript Onkeypress
July 1, 2023 - Here we validated when we press any inputs on keyboard. <!DOCTYPE html> <html> <head> <title>Only Numbers OnKeyPress</title> </head> <body> <h3>THIS INPUT FIELD ALLOWS NUMBERS ONLY</h3> <input type="text" onkeypress="javascript:return isNum(event)" placeholder="Enter Phone Number" name="ph"> <script> function isNum(evt){ var charCode=(evt.which)?evt.which:event.keyCode if(charCode>31 && (charCode<48 || charCode>57)){ return false; } else{ return true; } } </script> </body> </html>
🌐
ASPSnippets
aspsnippets.com › Articles › 2688 › Perform-Numeric-validation-Numbers-using-OnKeyPress-in-JavaScript
Perform Numeric validation Numbers using OnKeyPress in JavaScript
January 29, 2019 - explained with an example, how ... JavaScript. When User types in the TextBox, the text in the TextBox will be validated using OnKeyPress event handler in JavaScript and if the inputted character is not Numeric .....
🌐
Wikitechy
wikitechy.com › tutorials › javascript › html-text-input-allows-only-numeric-input
[Solved - 100% Working Code] - HTML text input allows only numeric input - javascript tutorial - Wikitechy
Here we can use JS Validation. It will work for numeric keypad and normal number key's · function isNumberKey(evt){ var charCode = (evt.which) ? evt.which : event.keyCode if (charCode < 31 || (charCode >= 48 && charCode <= 57 ) || (charCode >= 96 && charCode <= 105 )) return true; return false; } <input name="someid" type="number" onkeypress="return isNumberKey(event)"/%>Copy Code · JavaScript: Using keys and other non-typeable keys to combined for a complete JavaScript.
🌐
JavaScript Kit
javascriptkit.com › javatutors › javascriptkey3.shtml
Form validation using the keyboard events
Click here for comprehensive JavaScript tutorials, and over 400+ free scripts!
🌐
GitHub
gist.github.com › nikola-wd › 0b18148c6bbd8723143bc60f9a0f8aa9
[Validation - allow only numbers onKeyPress] #javascript #validation · GitHub
[Validation - allow only numbers onKeyPress] #javascript #validation - input_number_validation_filter.js
Find elsewhere
🌐
Code2night
code2night.com › Blog › MyBlog › Numeric-only-validation-in-javascript
Numeric only validation in javascript | Code2night.com
August 14, 2022 - Below is a code in HTML and JavaScript to number validate a text field. <form> <div class="form-group row"> <label for="inputPassword" class="col-sm-2 col-form-label">Enter Amount</label> <div class="col-sm-10"> <input type="text" class="form-control" id="inputNumber" placeholder="Enter Amount" value="" name="inputNumber" onkeypress="return isNumber(event)" /> </div> </div> </form> Here is how to validate the input only to accept numbers this will only take numeric values like "112233" .
🌐
EncodedNA
encodedna.com › 2012 › 12 › JavaScript-accept-only-numbers-textbox.htm
Accept only numbers with decimal in a textbox using JavaScript
Similar example: 👉 How to force users to enter only numbers in a textbox using jQuery (A cross browser solution) ... &ltbody> &ltdiv> Enter only numbers with a Decimal value: &ltinput type='text' id='tbNumbers' value='' onkeypress='javascript: return isNumber(event)' autocomplete='off' /> ...
🌐
Roy Tutorials
roytuts.com › home › javascript › allow only numeric values or digits in a textbox using javascript or jquery
Allow Only Numeric Values or Digits in a TextBox using JavaScript or jQuery - Roy Tutorials
September 25, 2025 - Only digits allowed.'; return false; } else { document.getElementById('errorMsg').style.display = 'none'; return true; } } </script> </head> <body> <div> <h2>Allow only digits using JavaScript or jQuery</h2> </div> <div id="content"> <label>JavaScript Example</label><br/> <input type="text" name="number1" id="number1" onkeypress="return checkOnlyDigits(event)"/> <div id="errorMsg"></div> </div> </body> </html> Here in the above code you have only one input box and on onkeypress JavaScript function I have implemented the validation logic.
🌐
CodeProject
codeproject.com › Articles › 667548 › Validation-of-Numeric-Values-On-keyPress-Event-USi
Validation of Numeric Values On keyPress Event USing Javascript- CodeProject
February 27, 2010 - It is better to use javascript functions for validating numeric values rather than Validation Controls. We can restrict a textbox to allow only numeric values using javascript functions.
🌐
YouTube
youtube.com › technical rajni
Allow only numbers validation on KeyPress in TextBox with Javascript - YouTube
Please do like share and comment if you like the video please do hit like and if you have any query please write it comment box NestJs Tutorial https://www.y...
Published   June 7, 2023
Views   673
🌐
CodePen
codepen.io › yewkeong › pen › LNEQby
Allow numbers only on keypress with jQuery
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.