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

6

13

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 attempt to type beyond the allowed number of character, the text box fails. Now you won't be able to type in any other text boxes and nor will you be able to delete the already input character, in that text box.

If you haven't already faced this, then try it yourself on a simple HTML and check. Please tell me if you have any clue for solving this.

Mayramays answered 1/8, 2012 at 7:34 Comment(1)
P.S.: this happens in android's webviewMayramays
A
19

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

Arsenopyrite answered 1/8, 2012 at 21:20 Comment(1)
I have added rahul's suggestion in above code. Also if you want that this code should run only for Jelly bean you can put this code in below if block ` var ver = window.navigator.appVersion; ver = ver.toLowerCase(); if ( ver.indexOf("android 4.1") >= 0 ) { //required code here }`Arsenopyrite
M
5

if your case is very simple then a simple addition in your html line can work too, like so:

​<input type="text" class="abc" onkeypress="if(this.value.length > 9) return false;"/>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
Mayramays answered 2/8, 2012 at 5:18 Comment(2)
this is just an example, assuming you need max 10 characters in a field, maybe in a phone no. fieldMayramays
this doesn't work at all on mobile phones neither IOSSediment
G
2

Provide the permission as WRITE_EXTERNAL_STORAGE" in android Manifest file.

Now in my android 4.1.2 is working with acceptance of input type number 7.

Guayaquil answered 21/6, 2013 at 10:50 Comment(0)
A
1

I opened the following android issue: 35823. If you are experiencing this issue please star it so that it can get some attention and resolved.

Algarroba answered 1/8, 2012 at 15:15 Comment(0)
N
1

This certainly is a developer's nightmare bug :) Until you realize the root cause and feel the "ahaaa..." moment. FYI: Even though all the input fields in the DOM freeze because of this bug, I was still able to manipulate and change values of the input fields using Javascript.

Neuter answered 12/2, 2013 at 0:13 Comment(1)
yes through js, you can do that anytime. It's just that the DOM stops listening to touch events from user.Mayramays
I
1

Handle the keypress separately for input fields where you need to restrict the maxlength by adding a separate class and listen keypress for that class.

HTML

<textarea ng-model="model " rows="3" maxlength="100" cols="70" class="custom_txtarea ng-pristine ng-valid charlength" placeholder="Achievements"></textarea>

Javascript

$(".charlength").keypress(function(event) {
          if(event.which >= 32 || event.which == 13) {
          var maxLength = event.currentTarget.maxLength;
           var length = event.currentTarget.value.length;
           if(length >= maxLength) {                     
               event.preventDefault();
           }
       }
   });
Inscrutable answered 20/8, 2014 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.