7

I am trying to disable all other characters from being entered in text input.

Since to get the $ you have to press the shift-key and the 4-key. I am not sure how you would get the keyCode for somthing like this?

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Olokoo
  • 1,114
  • 3
  • 19
  • 34
  • Possible duplicate of [What are the JavaScript KeyCodes?](http://stackoverflow.com/questions/5603195/what-are-the-javascript-keycodes) – Pooja Kedar Feb 03 '17 at 07:39

5 Answers5

6

There is no onkeydown keycode, as previously said by ngmiceli

Although, onkeypress keycode exists and is equal to 36.

JavaScript Event KeyCode Test Page

Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86
5

Key codes relate only to keys. $ is a character, achieved through two keys, Shift and 4. There is no key code explicitly for $ when using onkeydown

Edit: It was pointed out by Edward that onkeypress uses key combinations, and does have keycode's for combinations. Learn something new every day :)

Here's some code, edited from the onkeydown example provided by the MDN, to detect keypress keycodes.

Here's the fiddle updated to be Firefox-friendly, using help from this S.O. post. The JQuery solution works too, if you swing that way.

Community
  • 1
  • 1
Nick
  • 8,964
  • 4
  • 26
  • 37
  • The fiddle from your edit gives this result in firefox12: 'keyCode for the key pressed: 0' (on any key i press..) – GitaarLAB Aug 08 '12 at 16:30
  • @GitaarLAB How odd. Works in IE and Chrome. I can't imagine Firefox wouldn't support `onkeypress`, though swapping it for `onkeydown` does work in Firefox. – Nick Aug 08 '12 at 16:33
  • Check out [this S.O. thread for info on onkeypress in Firefox](http://stackoverflow.com/questions/4496910/onkeypress-event-not-working-in-firefox) Doubt its the only one too – Nick Aug 08 '12 at 16:35
  • I'm losing faith in firefox to (apart from the security risks it starts to pose).. But on subject: yeah, does not matter what key I press, also does not respond to shift of alt or ctrl.. – GitaarLAB Aug 08 '12 at 16:35
  • @GitaarLAB Added another fiddle to be Firefox-friendly – Nick Aug 08 '12 at 16:40
  • Confirm, works!! Nice one. Alway's fun here on SO: you answered the question that was asked in the title, whereas I provided a different view on how to obtain the same end-result as outlined in the question's text. I like my answer to but I'm gonna vote for your answer to, since users mostly find answers related to the title of the question! Nice job! – GitaarLAB Aug 08 '12 at 16:44
2

Then DONT use this way to solve your problem: since you cannot predict that every keyboard-layout will always have $-sign entered as shift+4.
You can still get keycode 4, and check if shift was pressed, but you could not be sure of this!!

Thus it would be better to simply replace all illegal characters in your fields (before you submit the data). Think: str.replace()
You could also check for your set of illegal characters on the onkeyup event of the input-box, effectively replacing all illegal characters as you type!
Like: onkeyup="this.value.replace(/[your illegal characters]/gi, '')"
That should do what you want.

Please note: you should NEVER trust browser-input, and should still filter this input in your receiving script!!!

Good Luck!!

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
1

There is no key code for $, since it's not a valid key - it has to be accessed via some kind of shifter.

Usually, you would perform a check to see if the shifter was active, and the listen out for the keypress event on the relevant key. Something like this for a QWERTY layout in your instance:

var shiftPressed = false;

$(window).keydown(function(e) {  
    if(e.which == 16) { shiftPressed = true; }
    if(e.which == 52 && shiftPressed) 
    {  
        // Do whatever here...
    }
});

$(window).keyup(function(e) {  
    if(e.which == 16) { shiftPressed = false; }
});
BenM
  • 52,573
  • 26
  • 113
  • 168
1

onkeypress - The Unicode CHARACTER code is: 36

onkeydown - The Unicode KEY code is: 52

Cherryishappy
  • 160
  • 1
  • 6