Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I got a simple input field which has a maxlength="2" attribute. The code looks like this below:

<input id="txtLoginName" maxlength="2">

It works fine on most Android devices. However, on the HTC One M7, it does not work. On this device, it just allows me to enter as many characters as I want.

Any suggestion? I think it should be a device specific issue so far.

Thanks in advance.

like image 454
Long Dao Avatar asked Jun 27 '14 02:06

Long Dao


People also ask

How do you use Maxlength in HTML?

The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.

How do you change input number length in HTML?

The <input> maxlength attribute is used to specify the maximum number of characters enters into the <input> element. Attribute Value: number: It contains single value number which allows the maximum number of character in <input> element. Its default value is 524288.

How do you add a Maxlength in CSS?

Use $("input"). attr("maxlength", 4) if you're using jQuery version < 1.6 and $("input"). prop("maxLength", 4) if you are using jQuery version 1.6+.

How do you set the maximum number of characters allowed in the input field to 40?

Complete HTML/CSS Course 2022 To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.


3 Answers

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));
    }
});
like image 95
Kevin Sebastian Fernandez Avatar answered Oct 16 '22 10:10

Kevin Sebastian Fernandez


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:

  • https://codereview.chromium.org/442433002/
  • https://bugs.chromium.org/p/chromium/issues/detail?id=431747
  • https://caniuse.com/#feat=maxlength
like image 42
Miro Grenda Avatar answered Oct 16 '22 12:10

Miro Grenda


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.

like image 2
Adam Hughes Avatar answered Oct 16 '22 11:10

Adam Hughes