Introduction
Often when we create forms — especially those involving databases — it is important, useful or necessary to control what characters can be used as input for our text fields. You frequently need to allow numbers only, letters only, decimal format only, etc. This article will show you the basics of controlling this input on any controls key press event.
Background
I often found it annoying that there was no option on Microsoft textboxes to say what form of input we want. So, I set about developing a set of code snippets that I could use to validate the input of text boxes on my form before the user makes any attempt to commit data to a database.
Using the Code
The code is simple. Just copy and paste it into the key press event of any given control, although it is specifically designed for those with alphanumeric fields such as combo boxes and textboxes.
Numeric Input
If Char.IsNumber(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(
Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If
Alphabetical Input
If Char.IsLetter(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(
Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If
Alphanumeric Input
If Char.IsLetterOrDigit(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(ChrW(
Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If
Decimal Input
If Char.IsNumber(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) Or e.KeyChar =
CChar(".") or e.KeyChar = CChar(ChrW(Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If
Above, we can see how an exception is formed for the decimal place. This can be done with any of the special characters or any other letter or number. Another interesting point is that when dealing with multiple textboxes (or other) that require the same form of validation, these snippets still apply. For Instance:
Numeric Input on Multiple Textboxes
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.KeyPressEventArgs) Handles TexBox1.KeyPress, _
TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress
If Char.IsNumber(e.KeyChar) = False Then
If e.KeyChar = CChar(ChrW(Keys.Back)) or e.KeyChar = CChar(
ChrW(Keys.Space)) Then
e.Handled = False
Else
e.Handled = True
End If
End If
End If
Note the number of keypress
events handled with this statement. Textboxes 1 through 4 are all validated in real time through one Sub
.
Points of Interest
One thing that was particularly annoying about using the IsNumber
and IsLetter
functions was that they not only block out all special characters, but also the backspace, delete, space and copy and past functions. That's why it is necessary to create an exception for the backspace and space, so that it works as well as any letters and/or numbers. Because I have not included exceptions for copy, cut and paste, these will not work. The number validation also allows use of the numpad, so that numbers entered from anywhere on the keyboard will work.
The ZIP file above contains the source for this project. There is nothing more in the project than a form with 8 textboxes and the code shown above. However, there are several code snippet files that will add the above to your code snippets under Common Code Patterns -> Validation.
History
- 4 July, 2007 — Original version posted
- 31 Oct, 2007 — Source updated