The html 5 maxlength is not working samsung devices. How can I restrict user to enter more than maximum characters in the text box using javascript.
Asked
Active
Viewed 1,135 times
0
-
Which browser are you using ? Native browser or Chrome ? Are you sure of your code ? Can we have your html code ? I'd like a confirmation because I tried it on Samsung S4 and Samsung S6 and all works perfectly. If you're right, I would be very surprising to see a such mistake in a device. – Alexandre Tranchant Oct 29 '15 at 07:39
1 Answers
2
You can check on keyup event if length is valid...
old_value = "";
document.querySelector(yourinput).addEventListener("keyup", function(e){
if (this.value.length > 20){
//set the old value
this.value == old_value;
}
else{
old_value = this.value;
}
})
This is untested and just an idea how to handle this.
OR (I don't think this will work, but good to know)
document.querySelector(yourinput).maxLength = "20";
//ie: document.querySelector(yourinput).setAttribute("maxLength", 20);
-
If maxlength attribute is not understood by samsung device, I do not think that your the second way will do the job. But the first method will do it. You can use jquery, handle all input elements with this attribute an add your listener. – Alexandre Tranchant Oct 29 '15 at 07:34
-
1Because of this it is just the second option. -> NO need for JQUERY, you can easily select multiple elements. And the op doesn't tagged jquery, so nope. – Oct 29 '15 at 07:38
-