Limit number of characters in input type number

后端 未结 13 1698
逝去的感伤
逝去的感伤 2020-12-18 08:08

Im trying to limit to X number the characters in a input (type of number). ive tried a lot of options and none seems to work. I dont want to use the option tel as it needs t

相关标签:
13条回答
  • 2020-12-18 08:35

    The HTML5 number type input has min and max attributes.

    If you wanted to limit the number of characters to 1 you could set a min of 0 and a max of 9.

    You can also set the step attribute, which is 1 by default, but would come in use if you wanted the ability to select decimals or other rounded numbers.

    <input type="number" maxlength="1" max="9" min="1" size="1" />
    

    Here's the full demo

    0 讨论(0)
  • 2020-12-18 08:39

    please review this code

    <script language="javascript" type="text/javascript">
    function limitText(limitField, limitCount, limitNum) {
        if (limitField.value.length > limitNum) {
            limitField.value = limitField.value.substring(0, limitNum);
        } else {
            limitCount.value = limitNum - limitField.value.length;
        }
    }
    </script>
    
    <form name="myform">
    <input name="limitedtextfield" type="text" onKeyDown="limitText(this.form.limitedtextfield,this.form.countdown,15);" 
    onKeyUp="limitText(this.form.limitedtextfield,this.form.countdown,15);" maxlength="15"><br>
    <font size="1">(Maximum characters: 15)<br>
    You have <input readonly type="text" name="countdown" size="3" value="15"> characters left.</font>
    </form>
    
    0 讨论(0)
  • 2020-12-18 08:41

    Note: charCode is non-standard and deprecated, whereas keyCode is simply deprecated.

    Check this code

    JavaScript

    <script>
    function check(e,value)
    {
        //Check Charater
        var unicode=e.charCode? e.charCode : e.keyCode;
        if (value.indexOf(".") != -1)if( unicode == 46 )return false;
        if (unicode!=8)if((unicode<48||unicode>57)&&unicode!=46)return false;
    }
    function checkLength()
    {
        var fieldLength = document.getElementById('txtF').value.length;
        //Suppose u want 4 number of character
        if(fieldLength <= 4){
            return true;
        }
        else
        {
            var str = document.getElementById('txtF').value;
            str = str.substring(0, str.length - 1);
            document.getElementById('txtF').value = str;
        }
    }
    

    and HTML input with number type below

    onInput //Is fired also if value change from the side arrows of field in Chrome browser
    
    <input id="txtF" type="number" onKeyPress="return check(event,value)" onInput="checkLength()" />
    

    Fiddle Demo

    Update -- Little bit generic code example

    Change above function into this one

    function checkLength(len,ele){
      var fieldLength = ele.value.length;
      if(fieldLength <= len){
        return true;
      }
      else
      {
        var str = ele.value;
        str = str.substring(0, str.length - 1);
        ele.value = str;
      }
    }
    

    In HTML use like this

    <!-- length 4 -->    
    <input id="txtF" type="number" onKeyPress="return check(event,value)" onInput="checkLength(4,this)" />
    <!-- length 5 -->    
    <input  type="number" onKeyPress="return check(event,value)" onInput="checkLength(5,this)" />
    <!-- length 2 -->    
    <input  type="number" onKeyPress="return check(event,value)" onInput="checkLength(2,this)" />
    

    Demo

    0 讨论(0)
  • 2020-12-18 08:42

    You can easily do it like this,

    <input type="text" pattern="\d*" maxlength="4">
    
    0 讨论(0)
  • 2020-12-18 08:43

    Use the maxlength property like this.

    <input type="text" maxlength="5"/>
    
    0 讨论(0)
  • 2020-12-18 08:44

    You can try thiss jQuery code :

    In HTML

    <input type="number" id="number" />
    

    In JS

    $(document).ready(function () {
        var element = document.getElementById('number');
    
        $("#number").keydown(function (event) {
            // Allow only backspace and delete
    
            if($(this).val().length <= 9 || event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 )
            {
                if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9) {
                    // let it happen, don't do anything
                } else {
                    // Ensure that it is a number and stop the keypress
                    if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                        event.preventDefault();
                    }
                }
            }else{
    
                event.preventDefault();
            }          
    
        });
    
    
    });
    

    I have not any idea wheather its working on IOS N Android but its work on all browser.

    DEMO

    0 讨论(0)
提交回复
热议问题