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

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

Introduction

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 Boolean 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)

  Me.ValidText1.Focus()

  Exit Sub

  End If

  If Me.ValidText2.Validate = False Then

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

  Me.ValidText2.Focus()

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: ra_luis@yahoo.com

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

machocr
Web Developer
Costa Rica Costa Rica
Ing. Luis Alberto Ruiz Arauz
Software Engineering / Industrial Engineering
Follow on   Twitter

Comments and Discussions

 
GeneralError found and solution PinsussJose Juan Calvo15-Nov-04 23:40 
Hello,
 
I found the next error in the component.
 
When I make a maskedit over a date and I press the key Supr the component crashes.
 
I solved the problem inserting a KeyDown Event and intercepting the Supr Key code redifining it like a backspace.
 
Private Sub ValidText_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Delete
keyType = "BackSP"
Case Else
keyType = "Other"
End Select
End Sub
 
It work's for my.
 
Sorry for my poor english. I'm from spain.
 
Jose Juan
 
Jose Juan Calvo.
QuestionHow do I get it to work in VS 2003 PinmemberPicine Karppehd29-Oct-04 4:02 
GeneralGreat idea... PinmemberMatt Berther5-Apr-03 17:54 
GeneralRe: Great idea... Pinmembermachocr29-May-03 6:51 
Generalnice idea Pinmemberyarix4-Apr-03 1:24 
GeneralRe: nice idea Pinmembermachocr29-May-03 6:40 
QuestionWhat do you think about validText? Pinmembermachocr29-Jan-03 10:29 

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
Web03 | 2.8.140916.1 | Last Updated 31 May 2003
Article Copyright 2003 by machocr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid