How to restrict decimal numbers from a <input="text">
show only integer numbers. The field is only show like 11 not 11.00
<input type="text">
Without using
<input type="number">
How to restrict decimal numbers from a <input="text">
show only integer numbers. The field is only show like 11 not 11.00
<input type="text">
Without using
<input type="number">
You can use match()
method to checking format of input value.
var input = document.getElementById("number");
var lastValue = "";
input.addEventListener("keydown", valueCheck);
input.addEventListener("keyup", valueCheck);
function valueCheck(){
if (input.value.match(/^[0-9]*$/))
lastValue = input.value;
else
input.value = lastValue;
}
<label>Type your value</label>
<input type="text" id="number" />
You can use parseInt()
to remove decimal part without rounding (11.9
=> 11
, 11.1
=> 11
and etc):
<input type="text" id="num">
var n = 11.13;
document.getElementById('num').value = parseInt(n); // 11
or just +
operator:
var n = 11.13;
document.getElementById('num').value = +n; // 11
Maybe you can add a min attribute to your input html element. like this
<input type="number" min="0">