1

jQuery Code

only_float is text box class Name.

$(".only_float").live("down",function(event){
          console.log(event.keyCode);
    });

When I press 1 from Numeric key on following mentioned image then it will Display 49 for keyCode.

enter image description here

When I press 1 from Numeric key on following mentioned image then it will Display 97 for keyCode.. enter image description here

My Question is Why different keyCode for Same Numeric Value?

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
  • Because JQuery doesn't care about numeric value, it cares about the physical key being pressed. (I could be wrong here of course) – therealrootuser Aug 30 '14 at 05:40
  • @mattingly890 Can you explain what you mean by `physical key` ? – Sadikhasan Aug 30 '14 at 05:41
  • @mplungjan My actual question is not for keyCode but I want to validate float number so I can? – Sadikhasan Aug 30 '14 at 05:46
  • By physical key, I mean the *actual* key that was pressed on your keyboard; from the hardware perspective, every key is different, regardless of the plastic sticker that happens to be on top. Can you clarify what you mean by "validate float number"? – therealrootuser Aug 30 '14 at 05:50
  • I want to allow only float number in text box then how I can validate like `23.44`(valid) and `23.44a`(not valid) – Sadikhasan Aug 30 '14 at 05:52
  • I wouldn't work directly with keyCodes then, I would subscribe to an event that would fire whenever the text changes, then validate the text in the text box. I'm sure there is something on number validation in jquery on Google, just search around, if you can't find what you need, then ask a new question. Best wishes. – therealrootuser Aug 30 '14 at 06:17

1 Answers1

1

When a keyboard event is triggered in JavaScript, the event.keyCode contains the keycode of the key that was pressed. It just so happens that there are two different constants for the numerical keypad and the standard location numbers.

From the MDN reference, the following constants are defined:

DOM_VK_1 = 0x31 (49) and DOM_VK_NUMPAD1 = 0x61 (97)

Thus, the keyCode may very well be different, depending on which physical key was pressed, whether the regular 1 or the numeric keypad 1.

therealrootuser
  • 10,215
  • 7
  • 31
  • 46