0

I have input like this

<input max="100" min="0" type="number">

But in this input users can put numbers like 01 02 03 004 itp... And my question is how to prevent this? To only numbers from 0 to 100

0, 1, 2, 3 ... 100

anakin
  • 3
  • 3
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart – Mister Jojo Jun 21 '19 at 14:10
  • 1
    https://stackoverflow.com/questions/8808590/html5-number-input-type-that-takes-only-integers – Pete Jun 21 '19 at 14:10
  • Possible duplicate of [Remove leading zeros from input type=number](https://stackoverflow.com/questions/30626094/remove-leading-zeros-from-input-type-number) – Mathias Jun 21 '19 at 14:46

2 Answers2

1

In most cases JavaScript is the answer:

<input type="text" id="taskinput">
<script>
    const input = document.getElementById('taskinput');

    let lastValue = '';

    input.oninput = () => {
        if (!input.value) {
            lastValue = '';
            return;
        }

        const val = parseInt(input.value);

        if (val > 100 || isNaN(val)) {
            input.value = lastValue;
            return;
        }

        lastValue = val;
        input.value = lastValue;
    }
</script>
Victor
  • 409
  • 3
  • 14
0

You could archive this by adding a onchange eventlistener on the input.

The regex will remove the leading zero.

document.getElementById('number').onchange = function(){
 this.value = this.value.replace(/\b0+[0-9]/g, '');
};
<input id="number" min="0" max="100" step="1" type="number" >
Dontwan
  • 121
  • 6