Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

how to check which key is pressed

 
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
 
Rancher
Posts: 43016
76
  • 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 ]
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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:

 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic