HTML number input min and max not working properly

前端 未结 12 1596
野的像风
野的像风 2020-12-05 22:31

I have type=number input field and I have set min and max values for it:



        
相关标签:
12条回答
  • 2020-12-05 22:49

    Despite the HTML5 enforcement of min and max on the up/down arrows of type=number control, to really make those values useful you still have to use Javascript.

    Just save this function somewhere and call it on keyup for the input.

    function enforceMinMax(el){
      if(el.value != ""){
        if(parseInt(el.value) < parseInt(el.min)){
          el.value = el.min;
        }
        if(parseInt(el.value) > parseInt(el.max)){
          el.value = el.max;
        }
      }
    }
    

    Like so...

    <input type="number" min="0" max="23" value="14" onkeyup=enforceMinMax(this)>
    
    0 讨论(0)
  • 2020-12-05 22:49

    This work perfect for geographic coordinates when you have general function document EventListener "keydown" in my example i use bootstrap class.

    <input type="text" name="X_pos" id="X_pos" class="form-control form-control-line" onkeydown="event.stopPropagation(); return(parseInt(event.key) >= 0 && parseInt(event.key) <= 9 && this.value+''+event.key <= 179 && this.value+''+event.key >= (-179)) || this.value.slice(-1) == '.' && parseInt(event.key) >= 0 && parseInt(event.key) <= 9 || event.keyCode == 8 || event.keyCode == 190 && String(this.value+''+event.key).match(/\./g).length <=1 || event.keyCode == 109 && String(this.value+''+event.key).length == 1 || event.keyCode == 189 && String(this.value+''+event.key).length == 1" style="width:100%;" placeholder="X" autocomplete="off">
    

    If you want you can create a function with this code but i preferred this method.

    0 讨论(0)
  • 2020-12-05 22:50
    $(document).ready(function(){
        $('input[type="number"]').on('keyup',function(){
            v = parseInt($(this).val());
            min = parseInt($(this).attr('min'));
            max = parseInt($(this).attr('max'));
    
            /*if (v < min){
                $(this).val(min);
            } else */if (v > max){
                $(this).val(max);
            }
        })
    })
    

    Here is my contribution. Note that the v < min is commented out because I'm using Bootstrap which kindly points out to the user that the range is outside the 1-100 but wierdly doesn't highlight > max!

    0 讨论(0)
  • 2020-12-05 22:50

    if you still looking for the answer you can use input type="number".
    min max work if it set in that order:
    1-name
    2-maxlength
    3-size
    4-min
    5-max
    just copy it

    <input  name="X" maxlength="3" size="2" min="1" max="100" type="number" />
    

    when you enter the numbers/letters manually (using the keyboard), and submit a little message will appear in case of letters "please enter a number" in case of a number out of tha range "please select a value that is no more/less than .."

    0 讨论(0)
  • 2020-12-05 22:53

    One event listener, No data- attribute.

    You can simply prevent it by using following script:

    $(document).on('keyup', 'input[name=quantity]', function() {
      var _this = $(this);
      var min = parseInt(_this.attr('min')) || 1; // if min attribute is not defined, 1 is default
      var max = parseInt(_this.attr('max')) || 100; // if max attribute is not defined, 100 is default
      var val = parseInt(_this.val()) || (min - 1); // if input char is not a number the value will be (min - 1) so first condition will be true
      if (val < min)
        _this.val(min);
      if (val > max)
        _this.val(max);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="number" class="form-control" name="quantity" max="250" min="1" value="">

    The only problem is: You can't type - to get negative numbers if your min is lower than 0

    0 讨论(0)
  • 2020-12-05 22:56

    Forget the keydown or keyup: it won't let you enter like 15 or 20 if the min was set to 10! Use the change event since this is where the input value goes in your business logic (ViewModel):

    private _enforceMinMax = (input:HTMLInputElement) => {
        console.log("input", input);
        const v = parseFloat(input.value);
        if(input.hasAttribute("min")) {
            const min = parseFloat(input.min);
            if(v < min) {
                input.value = min+"";
            }
        }
        if(input.hasAttribute("max")) {
            const max = parseFloat(input.max);
            if(v > max) {
                input.value = max+"";
            }
        }
    }
    
    private _distanceChange = (event) => {
        this._enforceMinMax(event.target);
        ...
    
    0 讨论(0)
提交回复
热议问题