0

Good morning,

I facing a issue on the IE 10 where my keypress still can enter '%' but the FF and Chrome no such issue. I found out that the key 37 is the [ left arrow ] which match with '%' in ASCII. My sample code as below:

$('#refId').bind("keypress", function(event) {
            // allow  letters, numbers and keypad numbers ONLY
            var key = event.charCode;
            if((key >= 48 && key <= 57) ||
                (key >= 65 && key <= 90) ||
                (key >= 97 && key <= 122)){
                return true;
            }

            //allow backspace, tab, left arrows, right arrow, delete
            key = event.keyCode;
            if(key == 8 || 
                    key == 9 ||
                    key == 37 ||
                    key == 39 ||
                    key == 46){
                return true;
            }

            return false; 
    });

Can give me some idea how to fix this?

Thanks. -fsloke

HenryLoke
  • 67
  • 1
  • 12

1 Answers1

0

Use var key = event.which; instead and join the if-statements.

The event.which property normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input.

- https://api.jquery.com/event.which/

$('#refId').on("keydown", function(event) {
    // allow letters, numbers and keypad numbers ONLY
    var key = event.which;
    if((key >= 48 && key <= 57) ||
        (key >= 65 && key <= 90) ||
        (key >= 97 && key <= 122) ||
        key == 8 || 
        key == 9 ||
        key == 37 ||
        key == 39 ||
        key == 46) {
        return true;
    }

    return false; 
});
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260