Cambia Research - Supporting the Microsoft .NET Developer Community Supporting the Microsoft .NET Developer Community  

     | Home  | Articles  | Categories  | Coders  | Search  | Submit  | Contact Us    
Increasingly, people seem to misinterpret complexity as sophistication... --Niklaus Wirth

Share Your Knowledge! -- Create and submit your articles the easy way with WebWriter.

Updated:02:16 AM CT Jan 11, 2007
Posted:01:54 AM CT Jan 11, 2007

How Do I Suppress a Keystroke in a Browser Input Box Using Javascript?

Author: Steve Lautenschlager

JavascriptBrowsersClient SideWeb

 Summary

Frequently, you want to suppress the key press event in Javascript so that
the character will not show up in a text box. For example, you want to limit
the number of characters that can be entered or you want to allow only numbers.
In these cases, you want to suppress the adding of the character to the text box.

The solution is simple. Return false from the onKeyPress event.

Example: Supressing Key Press in Javascript

<HTML>
   <BODY>
      <INPUT id="txtChar" onkeypress="return false;" 
            type="text" name="txtChar">
   </BODY>
</HTML>
If you try to enter text in the following text box you won't be able to. This works for both Internet Explorer and Netscape.

Sample Input Box:

 The Confusion Begins

Well, that was easy. But if you're like me, the more you try to do, the more confused you'll get.

The problem is that a key press generates three different events. When you press a key, the KeyDown event fires. This event is immediately followed by the KeyPress event. If you release the key, the KeyUp event will fire.

When you press a key and hold it you will immediately get the KeyDown and KeyPress events. If you hold the key down long enough, it will start to repeat and for each repeat the KeyDown and KeyPress events will fire repeatedly.

But don't make the mistake I did of thinking that you can suppress the adding of a character to a text box in the KeyDown event. You can set the value of the text box and return false (to hopefully cancel the event, don't forget to say a prayer, too). In some browsers this will actually cancel adding the character to the textbox, but in others it will not.

The best way to suppress (across browsers--the ones I've tested) the character from being added to the text box is to return false from the onKeyPress event handler, NOT the onKeyDown or onKeyUp events.

Add New Comment
How Do I Suppress a Keystroke in a Browser Input Box Using Javascript?
RandomUser02 Apr 08, 12:35Reply 
re: How Do I Suppress a Keystroke in a Browser Input Box Using Javascript?
Craig09 Sep 08, 15:25Reply 
re: How Do I Suppress a Keystroke in a Browser Input Box Using Javascript?
harish20 Oct 08, 10:17Reply 
re: How Do I Suppress a Keystroke in a Browser Input Box Using Javascript?
harish20 Oct 08, 10:19Reply 
CR Comments by Cambia Research
advertisement
 
Steve Lautenschlager (steve)
Steve is the founder and creator of Cambia Research. Developing and maintaining the site combines his passions for technology, writing and education.
Steve holds a Ph.D. in particle physics from Duke University, has worked at CERN, the European center for particle physics (where the web was born) and in Microsoft's web division with microsoft.com, msnbc.com and other web properties. Steve is a web consultant specializing in Microsoft.NET technologies. Read more here.


 
Copyright © Cambia Research 2002-2007. All Rights Reserved. steve [ at ] cambiaresearch.com