keypress keyup or Jquery keydown do not respond on smarthphone

2

I have a field validation on my website that works in any pc browser except mobile phones (in any browser) allowing the entry of any character; I have tried in the following ways:

 $(".onlytext").keydown(function(e) {
      var key = e.keyCode || e.which;
    return ((key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key >= 193 && key <= 211) || (key >= 225 && key <= 244) || key == 218 || key == 250 || key == 8 || key == 9 || key == 32) ;

  });



 $(".onlytext").on('keypress',function(e) {
      var key = e.keyCode || e.which;
    return ((key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key >= 193 && key <= 211) || (key >= 225 && key <= 244) || key == 218 || key == 250 || key == 8 || key == 9 || key == 32) ;

  });

The above as I said I have tried it in the 2 ways previously described with keyup, keydown and keypress and it does not work.

Thank you in advance for the information you can give me.

    
asked by U. Angel 20.09.2016 в 16:14
source

3 answers

1

Yes. Keyboard events do not work properly on mobile devices, I'm not sure if at all, but they do not work on Android Chrome.

The keyup and keydown events fire as we expect, but the event object of the callback provides incorrect values when we try to access the properties key , keyCode , charCode , among others . For example, in some versions of Chrome on Android all the keys throw the value 229 when you try to access the property keyCode , except the back key which throws an 8, others throw 0, etc.

On the other hand the event keypress is not even triggered or is not recognized.

As I understand it, this is because the virtual keyboards of the mobile devices are not able to generate a key identifier for the key that has been pressed, unlike the physical keyboards. The standard keyboard provided by Google has no way of generating these codes. In addition, if this were possible, it would be very difficult to control that all the IMEs of mobile devices adapt to said behavior.

The Android keyboard developers team simply recommends this: "Adapt to it."

The discussion is quite long on the subject. Here you can find more information.

    
answered by 21.09.2016 / 04:43
source
0
window.addEventListener('textInput', function(e) {
            const char = e.data; 

                const keyCode = char.charCodeAt(0);
                if (keyCode === 43 || keyCode == 44 || keyCode === 45 || keyCode == 46) {
                    e.preventDefault();
                    return false;
                }

            return true;
        });

    },
    
answered by 11.01.2018 в 12:17
-1

Please try keyup + keypress

$(".onlytext").on('keyup keypress',function(e) {

});

link

    
answered by 16.01.2017 в 09:26