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 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
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" />
Maybe you can add a min attribute to your input html element. like this
<input type="number" min="0">
<input type="text" pattern="([d]+)">
You can use modern HTML5 attribute pattern for input validation on client side. Input with decimal value would be invalid.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.