I want to restrict what numbers and letters can be entered into a textbox. Let's say I only want to allow numbers 0-5 and letters a-d (both lower and uppercase). I already tried using a masked text box but it only let me specify numbers only, letters only (both without restriction) or numbers and letters together but in a particular order. Best scenario would be: user tries to enter number 6 and nothing gets entered into the textbox, same for letters outside the range a-f. I think the best event to use would be the Keypress event, but I am at a loss as to how I can achieve the restriction thing.
- 8Don't forget copy/paste...ken2k– ken2k2012-03-07 13:00:43 +00:00Commented Mar 7, 2012 at 13:00
- see this question which will probably help you (Applies to ASP.NET)Shai– Shai2012-03-07 13:01:40 +00:00Commented Mar 7, 2012 at 13:01
- winforms, sl, wpf, wp7, asp.net webforms, or mvc?Daniel A. White– Daniel A. White2012-03-07 13:02:48 +00:00Commented Mar 7, 2012 at 13:02
- This is related to WinForms, sorry I forgot that detailgacanepa– gacanepa2012-03-07 13:07:21 +00:00Commented Mar 7, 2012 at 13:07
6 Answers
Use the KeyPress Event for your textbox.
protected void myTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs) { e.Handled = !IsValidCharacter(e.KeyChar); } private bool IsValidCharacter(char c) { bool isValid = true; // put your logic here to define which characters are valid return isValid; } Comments
// Boolean flag used to determine when a character other than a number is entered. private bool nonNumberEntered = false; // Handle the KeyDown event to determine the type of character entered into the control. private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { // Initialize the flag to false. nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard. if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) { // Determine whether the keystroke is a number from the keypad. if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) { // Determine whether the keystroke is a backspace. if(e.KeyCode != Keys.Back) { // A non-numerical keystroke was pressed. // Set the flag to true and evaluate in KeyPress event. nonNumberEntered = true; } } } //If shift key was pressed, it's not a number. if (Control.ModifierKeys == Keys.Shift) { nonNumberEntered = true; } } // This event occurs after the KeyDown event and can be used to prevent // characters from entering the control. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { // Check for the flag being set in the KeyDown event. if (nonNumberEntered == true) { // Stop the character from being entered into the control since it is non-numerical. e.Handled = true; } } 1 Comment
Override the PreviewKeyDownEvent like this:
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.A || e.KeyCode == Keys.B || ...) e.IsInputKey = true; else e.IsInputKey = false; } This will tell the textBox which keys it will consider as a user input or not.
Comments
Use the KeyDown event and if the e.Key is not in your allowable set, then just e.Handled = true.
An alternative would be accept all input, validate it and then provide useful feedback to the user, for example an error label asking them to enter data within a certain range. I prefer this method as the user knows something went wrong and can fix it. It is used throughout the web on web forms and would be not at all surprising for a user of your app. Pressing a key and getting no response at all might be confusing!
http://en.wikipedia.org/wiki/Principle_of_least_astonishment