• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check which key is pressed

 
Narasimha Raju Naidu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all

i have a text field and i want to keep a condition on this field and that condition is "i must and should take only characters and blank spaces". it must and should check after pressing the key.


regards
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what the JavaScript event handlers onKeyDown, onKeyUp and onKeyPress are for. This page shows them in action in the "Event Keycode Tester".
[ April 08, 2007: Message edited by: Ulf Dittmer ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Narasimha Raju Naidu:
condition is "i must and should take only characters and blank spaces"


What else is there? Everything you can type into a text field is a character.

Assuming you meant that you want to limit to a particular type of character, such as alphabetic characters, you can achieve this by: establish a keydown handler on the text field, check the key code of the entered character, and stop the event's default handling if the character is one that you wish to block.

The details of doing so are browser-dependent so I;d suggest using a library such as Prototype that allows you to handle events in a browser-independent manner.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a little example that I whipped up using Prototype that blocks spaces from being entered into the text field:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic