10

How can I stop user from adding number greater than max value and smaller then min value in html input(type = number)

<input id="ageRange" type="number" min=20 max= 50 maxlength=2></input>

I tried one javascript code, but it makes change after we add the value then it changes the value. Is there any way that I can stop user to put value in range or just there is no flickering in the input cell while we change value programatically?

3
  • 1
    yeah, min and max, like you're already doing. They can fill in a wrong value, and the element will now know it's wrong, and you can use CSS to show that to the user, and make the form that it's in not submit until the user fixes their value. Commented Sep 13, 2014 at 17:41
  • see it wufoo.com/html5/attributes/04-minmaxstep.html Commented Sep 13, 2014 at 17:52
  • What JavaScript code did you try? Commented Sep 13, 2014 at 18:03

5 Answers 5

7

You would need handle input at a low level and perform your own checks. To prevent the user from typing value larger than the maximum, you could handle the keypress event and then check the input character and whether it would make the value too large:

<input id="ageRange" type="number" min=20 max= 50 maxlength=2>
<script>
document.getElementById('ageRange').onkeypress =
  function (e) {
    var ev = e || window.event;
    if(ev.charCode < 48 || ev.charCode > 57) {
      return false; // not a digit
    } else if(this.value * 10 + ev.charCode - 48 > this.max) {
       return false;
    } else {
       return true;
    }
  }
</script>

This does prevent the user from inserting a larger value by pasting.

To prevent the user from deleting characters so that the value becomes too small, you would need to handle the rubout key, which is more problematic due to keyboard differences. But it would be poor usability. Suppose the user typed 30, then realized he meant to type 40. A code that prevents changing the value to too small would prevent removing either digit!

Browsers are expected to have their own user interface for <input type=number>, and this is the reason for using it. The interface is not meant to prevent the user from typing too large values but to detect them and report them so that the user can correct them. Before trying to substantially modify this, consider the potential confusion. If you do add code that prevents out-of-ranfe values, browsers will not show diagnostic messages (e.g. “Number must be less than or equal to 50”), as they do by default if they support <input type=number>, so your JavaScript code should probably issue its own diagnostics.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

var max = 100;
$('input').keyup(function(){

var inputValue = $(this).val();
if(inputValue > max){
    alert('greater!');

    $(this).val('')
}

})

here is jsfiddle: http://jsfiddle.net/h07Lc0Ld/1/

2 Comments

The jsfiddle does not have <input type=number>, and the code clears user input, which is hardly what was meant, or good usability.
you right, you can change inside if statement. and type number does not support by all browsers now.
0

Have a read of the input element documentation.

alternatively:

Have a read of HTML5 Constraint Validation

<input id="age" pattern="[0-9]" value="9" />
<script>
    document.getElementById('age').validity.patternMismatch;
</script>

And then take a read of Regex numeric range matching

Comments

0

I ended up with this to prevent the user from entering letters or a number outside the range 1-10.

<input type="number" id="skill${player.id}" class="inpRatings" step="1" min="1" max="10">

document.getElementsByClassName("inpRatings")[0].oninput =
    function () {
     
      const val = this.value;

      if (parseInt(val)){
        if(val < 1 || val > 10){
          this.value = null;
        }
      } else {
        this.value = null;
      }
    };

The page contains a list of players with a rating. I wanted to only allow a rating between 1 and 10 and to prevent any non integers being entered, including "e" for exponential which is allowed in a number input.

So, the JS gets all the inputs with a class of "inpRatings" and attaches an 'oninput' event to them all. The even function parses the value entered. If it's outside 1-10 it gets set to null. If it's not parsed as an int it also gets set to null.

Comments

-2

Set value to min...

<form>
<input id="ageRange" type="number" min="20" max="50" value="20"></input>
</form>

1 Comment

this does not answer the question, if you type minus -1, the field would still except it, this only when you use the litte up and down arrow button that come with the number input type

Your Answer

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