JQuery error in Android Chrome

I have the following jquery that works fine on desktop browsers when I download it on Android chrome mobile, and when I start typing, the second letter goes before the first letter and the others follow the second letter.

Here's how it works ...

Can someone help me solve this problem?

 $("#textbox").on('input', function(evt) {
          if(this.lengh == 0) return $(this);

          var input = $(this);
          var start = input[0].selectionStart;
          $(this).val($(this).val().replace(/[^a-zA-Z0-9 +:%=\\/-]/gi,""))
          $(this).val(function (_, val) {
            return val.toUpperCase();
          });
          input[0].selectionStart = input[0].selectionEnd = start;
        });
+4
source share
1 answer

The problem is that on Android, selectStart is zero.

Test it with your phone: http://www.vixed.it/st/34947263/

$("#textbox").on('input', function(e) {
    e.preventDefault();
    var input = $(this);
    var start = input[0].selectionStart;
    var inputTextChanged = input.val().replace(/[^a-zA-Z0-9 +:%=\\/-]/gi,"").toUpperCase();
    input.val(inputTextChanged);
    if (start>=1) input[0].selectionEnd = start;
});
0
source

Source: https://habr.com/ru/post/1625504/


All Articles