Programming is like sex, one mistake and you have to support it for the rest of your life. --Michael Sinz
Welcome to my blog and project site for Microsoft.NET development.

I've been a full time .NET developer for ten years, but I didn't start my professional life as a programmer ... more
Share/Print this page:

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

By steve on January 11, 2007.
Updated on January 22, 2012.
Viewed 27,803 times (0 times today).
Article TypesLanguagesSoftware DevelopmentTechnologies
SnippetJavascriptClient sideWeb Browsers

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.

Back to Top

User Comments (4)

Posted 2008 Apr 02 12:35 PM. reply
Not a very robust solution.

1) Copy any text.

2) Right-click on text field.

3) Paste.

4) Text field now contains the pasted text.


Try this instead:

onFocus="blur();"

RandomUser
Replied 2008 Sep 09 15:25 PM by Craig. reply
I was able to press Ctrl-V and insert characters from the clipboard. The Delete key seemed to work well to delete the characters too.
Posted 2009 Aug 06 10:00 AM. reply
I can copy and paste text into your box just fine.

ray
Posted 2010 Oct 21 01:24 AM. reply
Good. It worked :)
Thanks..

Venkat
Post Your Comment
  You may post without logging in or login here.
Display Name: Required.
Email: Required. Will not be shown. Used for identicon.
Comment:
Allowed tags: <quote></quote>, <code></code>, <b></b>, <i></i>, <u></u>, <red></red>
 
   Please type text as shown in the image at left.