Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

JavaScript Code to Restrict Keyboard Entries

Binary Number:

You can restrict form textbox input to only the characters you desire. This example shows how to restrict textbox input characters to only the characters 0 and 1. The character code for 0 is 48, the character code for 1 is 49. The code shown below restricts input to those two characters.

var unicode = e.charCode || e.keyCode;
if(unicode != 48 && unicode != 49) return false;

The character code input from the user's keyboard is stored in the variable unicode and the if statement tests if that code is not equal to 0 and is not equal to 1, if true, it return a false. A return of false means exit this section of code without doing anything else. This effectively restricts the input.

We do want to permit certain other key functions besides 0 and 1. For example we want to allow the character code for backspace (8), tab (9), and delete (46) so that the user can edit their input. To do that we nest the previous ifstatement in another if statement that does not allow those keyCodes to be tested by the first if statement, as shown below.

if(unicode != 8 && unicode != 9 && unicode != 46)
  if(unicode != 48 && unicode != 49) return false;

All this key input restricting prevents the form from responding to the normal Tab function that allows the user to tab between form elements, which is something that you probably want to preserve, so before letting program flow get to the statements above, I use an if statement to catch the character code for tab (13), and use it to send the focus to the next element in the form. The code for this is shown below.

if(unicode == 13)

Shown below is the entire code for a function to restrict the input from a form text box to only 0 and 1.

function restrictKey(e)
   var unicode = e.charCode || e.keyCode;
   // cause enter key to tab
   if(unicode == 13)

   // allow tab and backspace key
   if(unicode != 8 && unicode != 9 && unicode != 46)
     if(unicode != 48 && unicode != 49) return false;

function showEntry(form)

<form action="javascript:return false;">
Binary Number: <input type="text" id="bindigits" size="12" maxLength="12" style="text-align:right" onkeypress="return restrictKey(event)" /><br />
<input type="button" id="result" value="Show" onclick="showEntry(this.form)" />

You should know enough now to be able to adjust the code for your own purposes. To aid in that, below is a table of some keys and their related character codes.

Page Up33
Page Down34
Left arrow37
Up arrow38
Right arrow39
Down arrow40

More Java Script Code:
• Allowing Multiple Selections from Drop-down Select List
• Calculators for Your Website : Decimal to Hexidecimal Converter
• Using the Java Script Array Object
• How to Disable the Browser Back Button
• Java Script Random Password Generator
• JavaScript Code for Binary to Hexadecimal - Hexadecimal to Binary Conversion
• Java Script to Get Selected Item from Drop-down Select List
• Easy Slide Show Code with Mouseover Pause
• Regular Expression Basics : Match a Set of Characters
• Easy Java Script Timer Code

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268