maxlength attribute of input in html does not work on HTC One M7

冷暖自知 提交于 2019-12-04 04:37:54

Try this one:

var $input = $('input')
$input.keyup(function(e) {
    var max = 5;
    if ($input.val().length > max) {
        $input.val($input.val().substr(0, max));
    }
});

I've noticed this issue on a couple of projects. I think certain versions of Android have bad support for maxlength.

My solution was to use a jQuery keydown function that checks the amount of characters and prevents anymore from being entered if the max has been hit.

$('#inputWithMaxLength').keydown(function (e) {

    var inputLength = jQuery(this).val().length;

    if(inputLength >= 10) {
        e.preventDefault();
        return false;
    }
}

I'm sure you could change this so that it searched for any input with attributes of maxlength and created a jQuery backup on the fly. I'm not sure if that would be rather slow though.

What I found about this topic:

The issue is not specific to Android but is to Android keyboards. maxlength works on google keyboard.maxlength does not work on Samsung keyboard. => see https://github.com/ionic-team/ionic/issues/11578#issuecomment-323403990

and even more important - it's not a bug, it's a feature:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!