0

I'm using in a form an input of type number, like this:

<input class="form-control" id="Parti_NEstudante" name="Parti_NEstudante" type="number" min="0" max="9999999999" step="1" oninput="(validity.valid)||(value='');">

When i insert the value if i introduce more than 10 digits all the input is erased, due to the max i implemented..

My question is, instead of erasing my input, is it possible to just block more digits? just like the behavior of "maxlength" of input type "text".

3
  • stackoverflow.com/questions/18510845/… Chrome is just behaving correctly. you can add Jquery to save the result though. Commented Feb 2, 2018 at 18:14
  • Hum i see. Not worth the trouble then maybe Commented Feb 2, 2018 at 18:26
  • if you make the first attempt and post it, I can help you finish it up. Cheers. Commented Feb 2, 2018 at 18:27

1 Answer 1

1

You can achieve this by this way.

<input id="Parti_NEstudante" type="number" min="0" max="9999999999">

<script>
    var input = document.getElementById("Parti_NEstudante");

    input.addEventListener("input", function() {
        if (this.value.length > this.max.length) {
            this.value = this.value.slice(0, this.max.length)
        }
    });
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, although that didn't work. See this jsfiddle, i tested what you said. jsfiddle.net/t656N/2686
If you paste whole code to HTML window, it works. jsfiddle.net/psL9tLg5/…
Like this? jsfiddle.net/t656N/2687 Still doesn't. I tried on my code, also doesn't work
Ohh, the problem was my "oninput" definition. Thanks so much. Works exactly like i wanted.

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.