Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

TextBox with Validation Functions and MaskEdit

0.00/5 (No votes)
30 May 2003 1  
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