Click here to Skip to main content
13,005,991 members (61,401 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 25 Jan 2003

TextBox with Validation Functions and MaskEdit

, 30 May 2003
Rate this:
Please Sign up or sign in to vote.
CustomControl that inherits from TextBox class and adds input validation functions, required validation functions and MaskEdit mode.

Sample Image - ValidText.jpg


This is my first custom control, which one I decide to do to learn about this topic and to make me easy to work with forms validation data. This control inherits all attributes and methods from the TextBox class and adds some input and required validations.

Using the code

ValidText as any other component, can be added to the IDE ToolBox to put graphically into windows forms and modify theirs properties from the properties window in the Validation Category, or can be used like reference using their methods and modify their attributes directly from the code, generating a instance of its class.

The properties of ValidText are:

  1. ValitationMode (ValidationTypes): Sets the validation mode to use for the text or data field. This validation mode ca be: None, VaidChars, InvalidChars, Letters, Numbers and MaskEdit.
  2. MaskString (String): Sets the allowed mask for the input data when ValidationMode = MaskEdit. Character “#” suggest a digit, “A” suggest upper letter, “z” suggest lower letter, any other characters will be seen as a fixed character in the mask. For example: If the ValidationMode property = MaskEdit and y MaskString = ###- (AAA) - ##/aa/AA, user can be able to write data of 345 - ( ABC ) - 22/ad/CD types in the text control.
  3. ValidString (String): Sets the valid and not valid characters allowed when ValidationMode = ValidChars or InvalidChars.
  4. ShowErrorIcon (Boolean): Sets if the icon and tooltip of the ErrorProvider is showed in the validation of the data field.
  5. Required (Boolean): Sets if the data field must have at last one character to be validated.
  6. Reference (String): Sets the link between the text control and the data field to get. This property is used to make the validation error messages.

The unique method or function of ValidText is:

Validate: This method or function returns a

value to set if data field approve the required validation. I recommend to use this function in the Click Event of the button or the control that process of the data. For example:

Private Sub Button1_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) 
  Handles Button1.Click 
If Me.ValidText1.Validate = False Then

  MsgBox("The field " & Me.ValidText1.Reference & " is 
  required!", MsgBoxStyle.Critical, Me.Text)


  Exit Sub

  End If

  If Me.ValidText2.Validate = False Then

  MsgBox("The field " & Me.ValidText2.Reference & " is 
  required!", MsgBoxStyle.Critical, Me.Text)


Future Plans

In the future I am thinking to add custom error messages, more options in MaskEdit, other ValidationModes like email, IP address, Dates, etc. Please send me feedback, comments, and suggest to:


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

Web Developer
Costa Rica Costa Rica
Ing. Luis Alberto Ruiz Arauz
Software Engineering / Industrial Engineering

You may also be interested in...

Comments and Discussions

GeneralError found and solution Pin
Jose Juan Calvo15-Nov-04 23:40
sussJose Juan Calvo15-Nov-04 23:40 
QuestionHow do I get it to work in VS 2003 Pin
Picine Karppehd29-Oct-04 4:02
memberPicine Karppehd29-Oct-04 4:02 
GeneralGreat idea... Pin
Matt Berther5-Apr-03 17:54
memberMatt Berther5-Apr-03 17:54 
GeneralRe: Great idea... Pin
machocr29-May-03 6:51
membermachocr29-May-03 6:51 
Generalnice idea Pin
yarix4-Apr-03 1:24
memberyarix4-Apr-03 1:24 
GeneralRe: nice idea Pin
machocr29-May-03 6:40
membermachocr29-May-03 6:40 
QuestionWhat do you think about validText? Pin
machocr29-Jan-03 10:29
membermachocr29-Jan-03 10:29 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 31 May 2003
Article Copyright 2003 by machocr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid