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

3

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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" >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.