Jelly Bean WebView not working well with HTML maxlength attribute for text box

后端 未结 6 1605
我在风中等你
我在风中等你 2020-12-16 00:37

Jelly Bean doesn\'t seem to like the maxlength attribute of HTML for text input. It certainly restricts the number of input characters but the moment you attemp

6条回答
  •  时光取名叫无心
    2020-12-16 00:55

    I have also experienced the same problem in my app

    for now I have handled it with js, which removes all maxlength attributes from input text and textarea and stops user from inputing more than the required text. Here it is assumed that all input text and textarea have unique id.

    Code is also available at jsfiddle

        $(document).ready(function () {
    
            var ver = window.navigator.appVersion;
                ver = ver.toLowerCase();
    
            if ( ver.indexOf("android 4.1") >= 0 ){            
    
                var idMaxLengthMap = {};
    
                //loop through all input-text and textarea element
                $.each($(':text, textarea, :password'), function () {
                    var id = $(this).attr('id'),
                        maxlength = $(this).attr('maxlength');
    
                    //element should have id and maxlength attribute
                    if ((typeof id !== 'undefined') && (typeof maxlength !== 'undefined')) {
                        idMaxLengthMap[id] = maxlength;
    
                        //remove maxlength attribute from element
                        $(this).removeAttr('maxlength');
    
                        //replace maxlength attribute with onkeypress event
                        $(this).attr('onkeypress','if(this.value.length >= maxlength ) return false;');
                    }
                });
    
                //bind onchange & onkeyup events
                //This events prevents user from pasting text with length more then maxlength
                $(':text, textarea, :password').bind('change keyup', function () {
                    var id = $(this).attr('id'),
                        maxlength = '';
                    if (typeof id !== 'undefined' && idMaxLengthMap.hasOwnProperty(id)) {
                        maxlength = idMaxLengthMap[id];
                        if ($(this).val().length > maxlength) {
    
                            //remove extra text which is more then maxlength
                            $(this).val($(this).val().slice(0, maxlength));
                        }
                    }
                });
            }
        });​
    

    The bug for this issue was already opened at 35264

提交回复
热议问题