RestrictInputTextBox Control
The RestrictInputTextBox is a simple control that inherits from the standard TextBox. The RestrictInputTextBox does nothing more than add an onkeypress JavaScript event handler that prevents users from typing certain characters in the TextBox. This can be useful when asking a user to enter a price without the dollar sign, a phone number using only the digits, a password that only contains certain characters, or even something as simple as making sure they only enter a number for certain fields. The Tab and Backspace keys are always allowed, so that you do not need to worry about preventing the user from editing their text or tabbing to the next field.
ValidInput Enumeration Accepted Characters  
ValidInput.AllChars Any Character
ValidInput.Letters [A-Z],[a-z]
ValidInput.AlphaNumericDigits [A-Z],[a-z],[0-9]
ValidInput.AlphaNumericNumbers [A-Z],[a-z],[0-9],.,-
ValidInput.Uppercase [A-Z]
ValidInput.Lowercase [a-z]
ValidInput.Digits [0-9]
ValidInput.Integers [0-9],-
ValidInput.Numbers [0-9],.,-
ValidInput.PositiveNumbers [0-9],.
ValidInput.Hexadecimal [0-9],[A-F],[a-f]
ValidInput.Binary [0-1]
ValidInput.Octal [0-7]
Server Tag  
<NJS:RestrictInputTextBox runat="server" Accepted="AlphaNumericDigits" Specific="_." EnableViewState="false"/>
<NJS:RestrictInputTextBox runat="server" Accepted="AllChars" Invalid=""'" EnableViewState="false"/>
<NJS:RestrictInputTextBox runat="server" Accepted="None" Specific="AEIOUaeiou" EnableViewState="false"/>
<NJS:RestrictInputTextBox runat="server" Accepted="Hexadecimal" Specific="#" Invalid="abcdef" EnableViewState="false"/>
Properties & Methods
RestrictInputTextBoxThe RestrictInputTextBox inherits all of the properties and methods in the standard TextBox, and is used to prevent the user from typing certain characters.
  • Accepted - The predefined group of characters from the ValidInput enumeration (Default="AllChars")
  • Specific - A String containing the characters to be added to the selected predefined group of characters (Default=String.Empty)
  • Invalid - A String containing the characters to be removed from the selected predefined group of characters (Default=String.Empty)
ValidInput As ByteAn enumeration used to specify a predefined group of accepted characters.
  • None - Accepts only Tab and Backspace. This option is intended to be used only if you want to completely customize the list of accepted characters
  • AllChars - Accepts all characters. Intended for use when there are only a few characters you do not want to allow
  • Letters - Accepts [A-Z],[a-z]
  • AlphaNumericDigits - Accepts [A-Z],[a-z],[0-9]
  • AlphaNumericNumbers - Accepts [A-Z],[a-z],[0-9],.,-
  • Uppercase - Accepts [A-Z]
  • Lowercase - Accepts [a-z]
  • Digits - Accepts [0-9]
  • Integers - Accepts [0-9],-
  • Numbers - Accepts [0-9],.,-
  • PositiveNumbers - Accepts [0-9],.
  • Hexadecimal - Accepts [0-9],[A-F],[a-f]
  • Binary - Accepts [0-1]
  • Octal - Accepts [0-7]
Source Code

Because the JavaScript keypress event is very different in most browsers and I do not have access to all of them for testing, please be aware that this could prevent the user from typing certain keys that were not meant to be blocked (usually keys such as backspace, tab, enter, etc.). If you plan on using this class for an important site, I would suggest doing a little testing on more browsers, and feel free to make any adjustments you feel necessary. RestrictInputTextBox does not prevent the user from copying & pasting invalid characters into the textbox, so if undesired characters could cause an error be sure to check the text on the server as well (This can be done using my RestrictInputValidator control). When using RestrictInputTextBox, be sure your users are aware of what characters they can use to avoid frustration or confusion when certain characters cannot be typed. This is especially important when using TextMode="Password", since the user cannot see what they have typed.