Member-only story
Allow Only Numbers in Textbox Javascript
Hi friends, today in this tutorial you will learn how to allow only numbers in textbox javascript. Also, there are other ways to do this but it will be done with the help of the javascript event handler function onkeypress() by passing an event parameter.
Also read, Google places autocomplete example using maps JavaScript API
Let us take an example below to allow only numbers in textbox javascript
<html>
<head>
<title>Allow only number in Textbox</title>
</head> <body>
<h3>Allow Only Number In Textbox Javascript</h3>
<label for="fname">Quantity:</label><br>
<input type="text" onkeypress="allowNumbers(event)">
</body>
<script type="text/javascript">
function allowNumbers(e)
{
var code = (e.which) ? e.which : e.keyCode;
if (code > 31 && (code < 48 || code > 57))
{
e.preventDefault();
}
}
</script>
</html>
Explanation of the above example
The ASCII table is shown below for your reference.
Conclusion:- I hope the above illustration will help you to understand the overview. If there is any doubt then please leave a comment below.
Originally published at https://pbphpsolutions.com on June 15, 2022.