javascript - Detect if Shift is pressed - Stack Overflow

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

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?

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?

Share Improve this question edited Oct 26, 2012 at 20:03 j08691 208k32 gold badges269 silver badges280 bronze badges asked Oct 26, 2012 at 20:02 thiago.lenzthiago.lenz 4094 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

The event.shiftKey attribute should tell you that information.

Listen to the keypress instead of keydown event. Then you can reject based on the actual typed character, not the pressed key. So you don't have to to make guesses about the user's keyboard layout. For example, option+5 on a mac does NOT type 5, but you would miss that if you only watch the state of the shift key.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745468388a4629018.html

相关推荐

  • javascript - Detect if Shift is pressed - Stack Overflow

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

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信