I have an a text input with a maximum length:
<input type="text" name="name" maxlength="50">
This has been working fine on all the desktop browsers I've tried, but the maximum length does not seem to be enforced on mobile browsers.
Is there any way to get mobile browsers to enforce maxlength
? I am open to using JavaScript and/or jQuery in the solution.
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));
}
});
jsFiddle here: http://jsfiddle.net/fttk2/1/
This problem is because predictive text is enabled on your keyboard, disable it and try again
var max = 1
input.addEventListener('keyup', function (event) {
event.target.value = event.target.value.substring(0, max)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With