Allow Only Numbers in Textbox Javascript

Allow Only Numbers in Textbox Javascript

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>
Allow Only Numbers in Textbox Javascript

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store