Skip to content Skip to sidebar Skip to footer

Enforcing The Maxlength Attribute On Mobile Browsers

I have an a text input with a maximum length: This has been working fine on all the desktop browsers I've tried, but the maxim

Solution 1:

var max = 1input.addEventListener('keyup', function(event) {
    event.target.value = event.target.value.substring(0, max)
})

Solution 2:

This problem is because predictive text is enabled on your keyboard, disable it and try again

Solution 3:

I'd suggest something a bit more simple. The aboves didn't work for me unless there was a space added for the field to pick up on it... or if I hit submit right after, it still passed the information.

if($(".input") > 20){$(".input") = input.slice(0,20)};

Solution 4:

Universal jQuery way to enforce the stated maxlength of a form field. (I needed it for sessionStorage, localStorage, and param overrides.) Code optimized for readability.

$('input').on('change', function () {
  var max = $(this).attr('maxlength'),
      val = $(this).val(),
      trimmed;

  if (max && val) {
    trimmed = val.substr(0, max);
    $(this).val(trimmed);
  }
});

Solution 5:

var maxLength = 10;
var field = $('#myinput');
field.keydown( function(e)
{
    if ( $(this).val().length >= maxLength ) e.preventDefault();
});

Post a Comment for "Enforcing The Maxlength Attribute On Mobile Browsers"