Click here to Skip to main content
Licence 
First Posted 11 Apr 2005
Views 218,137
Bookmarked 49 times

VB.NET TextBox Validation Control

By | 29 Apr 2005 | Article
A TextBox control witch validates user input with the use of a regular expression. If the user input is invalid, both foreground and background colors may be set to preset colors giving the user a visual clue. The control will also send an event to the hosting program when a validation error occurs.

The TextBoxValidate Demo

Introduction

The TextBoxValidator control is a control that is inherited from the .NET Framework TextBox. This control will ensure that the information entered into the text box is valid for that field which it represents. The validation of the data is accomplished by sending the text value through the Regular Expression Match class. If the property Validation which holds the regular expression pattern is changed at run time, the the control will revalidate the data in the text box if it is not empty.

Using the code

To use the TextBoxValidate demo program, make sure both TextBoxValidateHost.exe and TextBoxValidate.dll are both in the same directory, and double click on the TextBoxValidateHost program. To install the control into your environment, open the Toolbox. Then select the tab you want the control to appear in, right click, and select “Add\Remove Items…”. When the “Customize Toolbox” window appears, select the “Browse…” button. When the “Open” dialog box appears, navigate to where you have placed the TextBoxValidate.dll and click the Open button. Then back in the “Customize Toolbox” window, click on the OK button. Now when you want to use the TextBoxValidator control, just drop it on a form and set the properties.

Toolbox icon

The icon displayed in the Toolbox window.

TextBoxValidate Properties

The properties added for the TextBoxValidator control.

The following four properties control the validation part of the control: ErrorBackColor and ErrorForeColor control the background and foreground colors of the text box when a validation error has occurred. Required is the property that will allow a field to be left blank (False) or must have a valid value (True) to be able to move on. The last property Validation is where the Regular Expression is placed to control the values allowed in the text box. You must be careful when entering a value for Validation so that what you want to validate is correctly defined by the regular expression. For example, if you want to validate a text field for a US phone number and selected the one Microsoft gives in the Regular Expression Editor, “((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}”, you will find that the expression will not only match (555) 555-1234 but also (555) 555-12345 and others. I used “^((\(\d{3}\) ?)\d{3}-\d{4})$|^(\d{3}-\d{3}-\d{4})$” in the demo program which matched a US phone number and nothing else, so please be careful. Also, if at run time the regular expression is modified, the control will validate the data in the text box if it contains any data.

Regular Expression Editor Showing Microsofts US Phone Number

Regular Expression Editor: You may type any regular expression in the Validation Expression text box shown above.

Look in the Microsoft documentation and the Web for information on regular expressions. The documents “Regular Expression Language Elements”, “Regular Expressions as a Language”, “.NET Framework Regular Expressions” and others can be found in the Microsoft documentation.

Revision History

  • April 12, 2005: Article first published.
  • April 23, 2005: Modified the code so that if the Validation pattern is changed at run time, it validates the data. A suggestion made by Oleg A. Lukin.

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

About the Author

Fernando Soto

Systems Engineer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey22:28 12 Mar '12  
Generaltime validation in window application Pinmemberpooran singh niranjan20:36 17 Jun '10  
GeneralRe: time validation in window application PinmemberFernando Soto2:31 18 Jun '10  
GeneralMy vote of 1 PinmemberSdumo23:57 1 Jul '09  
GeneralNumeric with percentage validation in vb.net textbox Pinmemberveeramani narayanasamy0:48 17 Feb '09  
QuestionToolbox icon? PinmemberMichael Ehrt23:39 11 Sep '05  
AnswerRe: Toolbox icon? PinmemberMichael Ehrt1:02 12 Sep '05  
GeneralRe: Toolbox icon? PinmemberFernando Soto4:28 12 Sep '05  
GeneralRe: Toolbox icon? PinmemberFernando Soto4:31 12 Sep '05  
GeneralRe: Toolbox icon? PinmemberMichael Ehrt4:44 12 Sep '05  
GeneralRe: Toolbox icon? PinmemberFernando Soto5:35 12 Sep '05  
AnswerRe: Toolbox icon? PinmemberFernando Soto4:08 12 Sep '05  
GeneralDear Fernando: about .net version supported Pinsussdundask0:45 11 Sep '05  
GeneralRe: Dear Fernando: about .net version supported PinmemberFernando Soto6:27 11 Sep '05  
GeneralRe: Dear Fernando: about .net version supported Pinmemberdundask6:33 12 Sep '05  
GeneralRe: Dear Fernando: about .net version supported PinmemberFernando Soto6:47 12 Sep '05  
GeneralValidation pattern PinmemberOleg A.Lukin7:46 22 Apr '05  
GeneralRe: Validation pattern PinmemberFernando Soto18:39 22 Apr '05  
This is a good suggestion and I will update the control to check the validation of the data in the text box after the property "Validation" has been updated with a new pattern.
 
Thanks

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 29 Apr 2005
Article Copyright 2005 by Fernando Soto
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid