aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes how to check which key is pressed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how to check which key is pressed" Watch "how to check which key is pressed" New topic
Author

how to check which key is pressed

Narasimha Raju Naidu
Ranch Hand

Joined: Mar 23, 2007
Posts: 37
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
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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 ]

Ping & DNS - updated with new look and Ping home screen widget
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Here's a little example that I whipped up using Prototype that blocks spaces from being entered into the text field:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check which key is pressed
 
Similar Threads
Methods add and delete
How to deny the Enter key press action
Conditional validation in Struts2
Why we cannot assign the persistent relationship field in ejbCreate() ?
Some questions about data.java in Assignment