2

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">
Stacker-flow
  • 1,251
  • 3
  • 19
  • 39
J. Doe
  • 21
  • 1
  • 3
  • Use Math.round() on change – thatOneGuy May 13 '16 at 09:00
  • 1
    Possible duplicate of [How can I remove the decimal part from JavaScript number](http://stackoverflow.com/questions/7641818/how-can-i-remove-the-decimal-part-from-javascript-number) – thatOneGuy May 13 '16 at 09:01
  • Possible duplicate of [Easiest way to mask characters in HTML(5) text input](http://stackoverflow.com/questions/10887645/easiest-way-to-mask-characters-in-html5-text-input) – kayess May 13 '16 at 09:01

4 Answers4

1

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" />
Mohammad
  • 21,175
  • 15
  • 55
  • 84
0

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
Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47
-1

Maybe you can add a min attribute to your input html element. like this

<input type="number" min="0">
claudios
  • 6,588
  • 8
  • 47
  • 90
-1
<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.

https://jsfiddle.net/rry32tm1/

z1m.in
  • 1,661
  • 13
  • 19