Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Shift is pressed

Tags:

javascript

I have a situation on a phone field. On this field, the user can type the following phone numbers:

(00)9999 9999
(00)99999 9999 (in São Paulo Brasil)
and 1234*12 (Nextel Numbers)

On the field phone I configure an onkeydown event that call this javascript function:

function validateKeyDownPhone(event) {

    var c = event.keyCode
        keychar=String.fromCharCode(c)
        valid = true

    /**
     * Excluding enter, backspace, etc. 
     */
    if (c > 46 ) {          
        if (keychar != null) {  
            var re = new RegExp("^[0-9\*#\(\) ]+$")
            matched = keychar.match(re)
            valid = matched != null && matched.length > 0
        }
    }
    event.returnValue = valid
}

It works fine, but when the I press SHIFT + 5 results in %, for example.

How can I identify if the shift key was pressed?

like image 575
thiago.lenz Avatar asked Dec 28 '25 16:12

thiago.lenz


1 Answers

The event.shiftKey attribute should tell you that information.

like image 193
complex857 Avatar answered Dec 30 '25 04:12

complex857



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!