How can I limit possible inputs in a HTML5 “number” element?

前端 未结 26 2465
感动是毒
感动是毒 2020-11-22 05:12

For element, maxlength is not working. How can I restrict the maxlength for that number element?

相关标签:
26条回答
  • 2020-11-22 05:13

    Lets say you wanted the maximum allowed value to be 1000 - either typed or with the spinner.

    You restrict the spinner values using: type="number" min="0" max="1000"

    and restrict what is typed by the keyboard with javascript: onkeyup="if(parseInt(this.value)>1000){ this.value =1000; return false; }"

    <input type="number" min="0" max="1000" onkeyup="if(parseInt(this.value)>1000){ this.value =1000; return false; }">
    
    0 讨论(0)
  • 2020-11-22 05:14

    I use a simple solution for all inputs (with jQuery):

    $(document).on('input', ':input[type="number"][maxlength]', function () {
        if (this.value.length > this.maxLength) {
            this.value = this.value.slice(0, this.maxLength); 
        }
    });
    

    The code select all input type="number" element where maxlength has defined.

    0 讨论(0)
  • 2020-11-22 05:15

    HTML Input

     <input class="minutesInput" type="number" min="10" max="120" value="" />
    

    jQuery

     $(".minutesInput").on('keyup keypress blur change', function(e) {
    
        if($(this).val() > 120){
          $(this).val('120');
          return false;
        }
    
      });
    
    0 讨论(0)
  • 2020-11-22 05:16

    I had this problem before and I solved it using a combination of html5 number type and jQuery.

    <input maxlength="2" min="0" max="59" name="minutes" value="0" type="number"/>
    

    script:

    $("input[name='minutes']").on('keyup keypress blur change', function(e) {
        //return false if not 0-9
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
           return false;
        }else{
            //limit length but allow backspace so that you can still delete the numbers.
            if( $(this).val().length >= parseInt($(this).attr('maxlength')) && (e.which != 8 && e.which != 0)){
                return false;
            }
        }
    });
    

    I don't know if the events are a bit overkill but it solved my problem. JSfiddle

    0 讨论(0)
  • 2020-11-22 05:16

    Ugh. It's like someone gave up half way through implementing it and thought no one would notice.

    For whatever reason, the answers above don't use the min and max attributes. This jQuery finishes it up:

        $('input[type="number"]').on('input change keyup paste', function () {
          if (this.min) this.value = Math.max(parseInt(this.min), parseInt(this.value) || 0);
          if (this.max) this.value = Math.min(parseInt(this.max), parseInt(this.value) || 0);
        });
    

    It would probably also work as a named function "oninput" w/o jQuery if your one of those "jQuery-is-the-devil" types.

    0 讨论(0)
  • 2020-11-22 05:17

    Max length will not work with <input type="number" the best way i know is to use oninput event to limit the maxlength. Please see the below code for simple implementation.

    <input name="somename"
        oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"
        type = "number"
        maxlength = "6"
     />
    
    0 讨论(0)
提交回复
热议问题