admin管理员组

文章数量:1220937

I have written a JS function that only allow numbers to be entered. A copy of that function is below:

function NumbersOnly(e) {
    var evt = e || window.event;
    if (evt) {
        var keyCode = evt.charCode || evt.keyCode;
        //Allow tab, backspace and numbers to be pressed, otherwise return false for everything.
        //(keyCode>=96 && keyCode<=105) are the numpad numbers        
        if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105) || keyCode === 9 || keyCode === 8) {


        }
        else {

            evt.returnValue = false;
        }
    }
}

This function works fine with all the numbers but my problem happens when the shift key is held down and one of the number keys is pressed. The value returned is one of the characters above the numbers. So for example if I hold down shift and press 7, '&' is returned but the keyCode is still 55!! I would have expected that to be different.

So my question is how do I check if the shift key is being held down. I've tried the following check but this didn't work:

    if (keyCode === 16) {
        evt.returnValue = false;
    }
    else {

        if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105) || keyCode === 9 || keyCode === 8) {


        }
        else {

            evt.returnValue = false;
        }
    }

I'm using ASP.NET 4.0.

Any help would be gratefully received.

Thanks in advance.

I have written a JS function that only allow numbers to be entered. A copy of that function is below:

function NumbersOnly(e) {
    var evt = e || window.event;
    if (evt) {
        var keyCode = evt.charCode || evt.keyCode;
        //Allow tab, backspace and numbers to be pressed, otherwise return false for everything.
        //(keyCode>=96 && keyCode<=105) are the numpad numbers        
        if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105) || keyCode === 9 || keyCode === 8) {


        }
        else {

            evt.returnValue = false;
        }
    }
}

This function works fine with all the numbers but my problem happens when the shift key is held down and one of the number keys is pressed. The value returned is one of the characters above the numbers. So for example if I hold down shift and press 7, '&' is returned but the keyCode is still 55!! I would have expected that to be different.

So my question is how do I check if the shift key is being held down. I've tried the following check but this didn't work:

    if (keyCode === 16) {
        evt.returnValue = false;
    }
    else {

        if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105) || keyCode === 9 || keyCode === 8) {


        }
        else {

            evt.returnValue = false;
        }
    }

I'm using ASP.NET 4.0.

Any help would be gratefully received.

Thanks in advance.

Share Improve this question edited May 15, 2012 at 11:11 user123444555621 153k27 gold badges117 silver badges126 bronze badges asked May 15, 2012 at 10:52 SunSun 4,71816 gold badges72 silver badges115 bronze badges 1
  • If you give the evt in console.log, you'll see shiftKey attribute, if shift key is pressed, it'll become true – Ranganadh Paramkusam Commented May 15, 2012 at 10:55
Add a comment  | 

3 Answers 3

Reset to default 16

You can check if shift key is pressed using :

if(evt.shiftKey) {
 ...  //returns true if shift key is pressed

Use event.key instead of charCode. No more magic numbers!

function onEvent(event) {
    const key = event.key; // "a", "1", "Shift", etc.
    if (isFinite(key)) { // Is number
        // Do work
    }
};

Mozilla Docs

Supported Browsers

use keypress for holding any key isntead of keydown

本文标签: keypressIs the shiftkey held down in JavaScriptStack Overflow