Click here to Skip to main content
15,881,833 members
Articles / Programming Languages / Visual Basic
Article

Controlling Text Field Input on Key Press

Rate me:
Please Sign up or sign in to vote.
2.71/5 (21 votes)
31 Oct 20072 min read 79.9K   337   41   7
Shows the basics of controlling input on a controls key press event

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

VB
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

VB
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

VB
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

VB
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

VB
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Australia Australia
I am a Hobby programmer who works in the insurance industry as a broker. I also innovate within my role through building custom software to streamline processes.

Comments and Discussions

 
QuestionIssue In Implementing Decimal Pin
mshariq16-Sep-10 2:52
mshariq16-Sep-10 2:52 
Generalthank you! Pin
xiaolinjuan12-Feb-08 17:02
xiaolinjuan12-Feb-08 17:02 
GeneralRe: thank you! Pin
The ANZAC12-Feb-08 17:46
The ANZAC12-Feb-08 17:46 
QuestionHow to add copy/paste/cut Pin
johneecc4-Oct-07 7:06
johneecc4-Oct-07 7:06 
AnswerRe: How to add copy/paste/cut Pin
The ANZAC6-Oct-07 13:39
The ANZAC6-Oct-07 13:39 
GeneralGood One Pin
Gunarathinam23-Aug-07 19:54
Gunarathinam23-Aug-07 19:54 
GeneralRe: Good One Pin
The ANZAC23-Aug-07 21:59
The ANZAC23-Aug-07 21:59 
ya, this was basically the build up to my other atrictle which you can check out from the link below in my signature, i just did this to give beginners an idea of how they could implement custom forms of validation as oppose to the standard ons that i have used.

Please check out my articles:
The ANZAC's articles

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.