Allow only numbers into a input text box
You can achieve this by pure JavaScript
. Create this function that you can reuse in your script.
function allowNumbersOnly(e) {
var code = (e.which) ? e.which : e.keyCode;
if (code > 31 && (code < 48 || code > 57)) {
e.preventDefault();
}
}
You may preferably call this onkeypress
event handler.
<input type="text" id="onlyNumbers" onkeypress="allowNumbersOnly(event)" />
function allowNumbersOnly(e) {
var code = (e.which) ? e.which : e.keyCode;
if (code > 31 && (code < 48 || code > 57)) {
e.preventDefault();
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
Try editing in me:
<input type="text" id="onlyNumbers" onkeypress="allowNumbersOnly(event)" />
</body>
</html>
However, I would recommend the unobtrusive style of writing JS using because it is good to keep the HTML semantic and away from pollution. You can execute the function on event handler that we would attach to this text box using vanilla JavaScript or jQuery.
function allowNumbersOnly(e) {
var code = (e.which) ? e.which : e.keyCode;
if (code > 31 && (code < 48 || code > 57)) {
e.preventDefault();
}
}
// using classic addEventListener method:
document.getElementById('onlyNumbers').addEventListener('keypress', function(e){ allowNumbersOnly(e);
}, false);
//using jQuery
$(function(){
$('#onlyNumbers2').keypress(function(e) {
allowNumbersOnly(e);
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<div>
Using addEventListener: <input type="text" id="onlyNumbers" />
</div>
<div>
Using jQuery: <input type="text" id="onlyNumbers2" />
</div>
</body>
</html>
To restrict every character you can just simply use e.preventDefault()
.
Besides, you can also use return false
instead but preventDefault()
is better in this case and return false
should be chosen wisely. It is good to know the difference between both of them.
user3474775
Updated on June 01, 2021Comments
-
user3474775 almost 3 years
I have a number input text box and I want to allow the user to edit but do not want to allow the user to enter any other text except numbers. I want them only to be able to use the arrows on the number input box.
<input type = "number" min="0" max="10" step="0.5" input id="rating" name = "rating" class = "login-input" placeholder = "Rating 1-5:" value="0">
-
user3474775 about 10 yearsHow would I implement this javascript?
-
user3474775 about 10 yearsNever mind, that was a silly question. Placing in the bottom between the <script> tags works fine. Thanks!
-
user3474775 about 10 yearsHow can I prevent the user from backspacing my initial value? This prevents letters and numbers only.
-
wavemode about 10 yearsJust run it on your page, probably with <script>*javascript code*</script>
-
josephting about 10 yearsThis should do.
document.getElementById('rating').onfocus = function() { return this.blur(); }
-
AlpharettaTechy about 2 yearsI would have thought jquery has a library by this time, but they are so screwy, due to various possible types of objects that can be passed. This worked for me beautifully