Click here to Skip to main content
Click here to Skip to main content
Go to top

Controlling Text Field Input on Key Press

, 31 Oct 2007
Rate this:
Please Sign up or sign in to vote.
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

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

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

Share

About the Author

The ANZAC
Other
Australia Australia
I am a Hobby programmer who primarily uses vb.net. I study Psychology at the University of Wollongong, Australia.
 
I am currently using Visual Studio 2005.

Comments and Discussions

 
QuestionIssue In Implementing Decimal Pinmembermshariq16-Sep-10 2:52 
Generalthank you! Pinmemberxiaolinjuan12-Feb-08 17:02 
GeneralRe: thank you! Pinmember The ANZAC 12-Feb-08 17:46 
QuestionHow to add copy/paste/cut Pinmemberjohneecc4-Oct-07 7:06 
AnswerRe: How to add copy/paste/cut PinmemberThe ANZAC6-Oct-07 13:39 
GeneralGood One PinmemberGunarathinam23-Aug-07 19:54 
GeneralRe: Good One PinmemberThe ANZAC23-Aug-07 21:59 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 31 Oct 2007
Article Copyright 2007 by The ANZAC
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid