Keycode is always zero in Chrome for Android

后端 未结 10 2095
栀梦
栀梦 2020-12-05 13:17

I need to detect the keycode for a custom search box on my website, but the keycode always returns as zero on Chrome for Android (except for backspace, which returns 8). Has

10条回答
  •  醉酒成梦
    2020-12-05 13:55

    I have faced the same issue with Android Devices of SonyXperia and HTC. I have developing hybrid application where i am validating Amount text field by reading event.keyCode() of the entered char on keydown event from text field, so that i can allow only numbers and dot(.) char to be entered. I tried but it doesn't worked, returning always 0 in these devices. Later i came with other solution with keyup event and char matching through Regular Expression:

    $("#AmountField").keyup(function (e) {
    
        var regex = /^[0-9\.]$/;
        var str = $(this).val();
        var subStr = str.substr(str.length - 1);
        if(!regex.test(subStr)) {
    
            if(str.length >0){
                $(this).val(str.substr(0, (str.length - 1)));
            }else{ 
                $(this).val();
            }
    
        }
    
    });
    

    Hope this can help for the people who facing this issue. Good Luck.

提交回复
热议问题