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

VB.NET TextBox Validation Control

Rate me:
Please Sign up or sign in to vote.
4.28/5 (34 votes)
29 Apr 2005CPOL3 min read 326.1K   6.5K   49   21
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer Unemployed
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionerror on vb2010 Pin
Kixo6921-Sep-12 6:44
Kixo6921-Sep-12 6:44 
AnswerRe: error on vb2010 Pin
Fernando Soto21-Sep-12 8:51
Fernando Soto21-Sep-12 8:51 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey12-Mar-12 22:28
professionalManoj Kumar Choubey12-Mar-12 22:28 
GeneralRe: My vote of 5 Pin
mazharkhan12310-Jun-14 2:54
mazharkhan12310-Jun-14 2:54 
Generaltime validation in window application Pin
pooran singh niranjan17-Jun-10 20:36
pooran singh niranjan17-Jun-10 20:36 
GeneralRe: time validation in window application Pin
Fernando Soto18-Jun-10 2:31
Fernando Soto18-Jun-10 2:31 
GeneralMy vote of 1 Pin
Sdumo1-Jul-09 23:57
Sdumo1-Jul-09 23:57 
GeneralNumeric with percentage validation in vb.net textbox Pin
veeramani narayanasamy17-Feb-09 0:48
veeramani narayanasamy17-Feb-09 0:48 
QuestionToolbox icon? Pin
Michael Ehrt11-Sep-05 23:39
Michael Ehrt11-Sep-05 23:39 
AnswerRe: Toolbox icon? Pin
Michael Ehrt12-Sep-05 1:02
Michael Ehrt12-Sep-05 1:02 
GeneralRe: Toolbox icon? Pin
Fernando Soto12-Sep-05 4:28
Fernando Soto12-Sep-05 4:28 
GeneralRe: Toolbox icon? Pin
Fernando Soto12-Sep-05 4:31
Fernando Soto12-Sep-05 4:31 
GeneralRe: Toolbox icon? Pin
Michael Ehrt12-Sep-05 4:44
Michael Ehrt12-Sep-05 4:44 
GeneralRe: Toolbox icon? Pin
Fernando Soto12-Sep-05 5:35
Fernando Soto12-Sep-05 5:35 
AnswerRe: Toolbox icon? Pin
Fernando Soto12-Sep-05 4:08
Fernando Soto12-Sep-05 4:08 
GeneralDear Fernando: about .net version supported Pin
dundask11-Sep-05 0:45
dundask11-Sep-05 0:45 
GeneralRe: Dear Fernando: about .net version supported Pin
Fernando Soto11-Sep-05 6:27
Fernando Soto11-Sep-05 6:27 
GeneralRe: Dear Fernando: about .net version supported Pin
dundask12-Sep-05 6:33
dundask12-Sep-05 6:33 
GeneralRe: Dear Fernando: about .net version supported Pin
Fernando Soto12-Sep-05 6:47
Fernando Soto12-Sep-05 6:47 
GeneralValidation pattern Pin
Oleg A.Lukin22-Apr-05 7:46
Oleg A.Lukin22-Apr-05 7:46 
GeneralRe: Validation pattern Pin
Fernando Soto22-Apr-05 18:39
Fernando Soto22-Apr-05 18:39 

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.