How to restrict number of characters that can be entered in HTML5 number input field on iPhone

前端 未结 10 1460
野趣味
野趣味 2020-12-14 02:19

It seems that neither of the \"maxlength\", \"min\" or \"max\" HTML attributes have the desired effect on iPhone for the following markup:

 

        
相关标签:
10条回答
  • 2020-12-14 02:26

    you can use this code:

    <input type="number" onkeypress="limitKeypress(event,this.value,2)"/>
    

    and js code:

    function limitKeypress(event, value, maxLength) {
        if (value != undefined && value.toString().length >= maxLength) {
            event.preventDefault();
        }
    }
    
    0 讨论(0)
  • 2020-12-14 02:28

    Example

    JS

    function limit(element)
    {
        var max_chars = 2;
    
        if(element.value.length > max_chars) {
            element.value = element.value.substr(0, max_chars);
        }
    }
    

    HTML

    <input type="number" onkeydown="limit(this);" onkeyup="limit(this);">
    

    If you are using jQuery you can tidy up the JavaScript a little:

    JS

    var max_chars = 2;
    
    $('#input').keydown( function(e){
        if ($(this).val().length >= max_chars) { 
            $(this).val($(this).val().substr(0, max_chars));
        }
    });
    
    $('#input').keyup( function(e){
        if ($(this).val().length >= max_chars) { 
            $(this).val($(this).val().substr(0, max_chars));
        }
    });
    

    HTML

    <input type="number" id="input">
    
    0 讨论(0)
  • 2020-12-14 02:28

    You can use JavaScript to check how many characters are in the box and if there are too many, remove one: http://www.webcodingtech.com/javascript/limit-input-text.php

    0 讨论(0)
  • 2020-12-14 02:31

    This is Tripex answer optimized for allowing delete key, works when typing and on paste.

    $(document).on("keyup", "#your_element", function(e) {
        var $that = $(this),
        maxlength = $that.attr('maxlength');
        if ($.isNumeric(maxlength)){
            if($that.val().length === maxlength) {
                e.preventDefault();
                // If keyCode is not delete key 
                if (e.keyCode !== 64) {
                    return;
                }
            }
            $that.val($that.val().substr(0, maxlength));
        }
    });
    
    0 讨论(0)
  • 2020-12-14 02:32

    Here a more optimized jQuery version that caches the selectors. It also uses the maxlength attribute that is not supported by input type number.

    // limits the input based on the maxlength attribute, this is by default no supported by input type number
    $("input[type=number]").on('keydown keyup',function(){ 
        var $that = $(this),
        maxlength = $that.attr('maxlength')
        if($.isNumeric(maxlength)){
            $that.val($that.val().substr(0, maxlength));
        };
    });
    
    0 讨论(0)
  • 2020-12-14 02:35

    Another option with jQuery, but onkeypress event... ;)

    $("input[type=number]").on('keypress',function(e) { 
        var $that = $(this),
        maxlength = $that.attr('maxlength')
        if($.isNumeric(maxlength)){
            if($that.val().length == maxlength) { e.preventDefault(); return; }
    
            $that.val($that.val().substr(0, maxlength));
        };
    });
    
    0 讨论(0)
提交回复
热议问题