maxlength of input tag with type=text using html5 in android webview not working

后端 未结 3 1262
没有蜡笔的小新
没有蜡笔的小新 2021-01-28 13:12

i am able to enter more than specified maxlength of input tag with type=text using html5 in android webview. when lost focus/ blur, value will be trimmed to maxlength.

f

相关标签:
3条回答
  • 2021-01-28 13:44

    Thanks @paddybasi it worked for me. Just one small correction. keydown event doesn't seem to be working in android. So we need to change the event to "textInput".

    $('input[maxlength],textarea[maxlength]').on('textInput', function (event) {
            var $this = $(this);
            if ($this.val().length >= parseInt($this.attr('maxlength'), 10)) {
                event.preventDefault();
            }
        });

    0 讨论(0)
  • 2021-01-28 13:49

    A bit late to the party, but as neliojrr mentioned you can correct this using javascript/jquery. However, I would be very tempted to make this much more generic:

    $('input[maxlength]').on('keydown', function(event) {
        var $this = $(this);
        if ($this.val().length > parseInt($this.attr('maxlength'), 10)) {
            event.preventDefault();
        }
    });
    
    0 讨论(0)
  • 2021-01-28 13:56

    This problem probably is a bug in Android 4.1 version as you can see here 35264. You can solve that with some Javascript:

    <input type="text" maxlength="3" id="hahaha" onkeypress="if(this.value.length >= this.getAttribute('maxlength') return false;" />
    

    or JQuery:

    $(function() {
        max_length = Number($("#hahaha").attr("maxlength"));
        $("#hahaha").attr("onkeypress", "if(this.value.length >= max_length) return false;");
    });
    
    0 讨论(0)
提交回复
热议问题