Many times we need to validate data entered in a TextBox for appropriate format such as e-mail, phone number and zip code. ASP.NET web controls along with ReqularExpression validator provide such validation mechanism. However, there is no such built-in way in Windows Forms. In this article I will explain how to create a windows user control that enhances System.Windows.Forms.TextBox control to validate commonly used data formats.
Data Formats and Regular Expressions
The System.Text.RegularExpressions namespace provides classes that allow you to use regular expressions. You can use its IsMatch property to check whether a certain pattern is present in the given expression or not.
Creating the user control
We will now write our own class that extends from the System.Windows.Forms.UserControl class. We will provide additional properties and validation logic in our implementation. Following code shows various members of the class:
Public Class ValidatingTextBox
Private objExpr As Regex
Private strExpr As String
Private strErr As String
Friend WithEvents TextBox1 As
Public Event ValidationFailed
(ByVal sender As Object, ByVal e As EventArgs)
Public Property ErrorMessage() As String
Set(ByVal Value As String)
strErr = Value
Public ReadOnly Property IsValid() As Boolean
If Not objExpr Is Nothing Then
Public Property ValidationExpression() As String
Set(ByVal Value As String)
strExpr = Value
objExpr = New Regex(strExpr)
Private Sub TextBox1_Validated
(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Me.IsValid Then
RaiseEvent ValidationFailed(Me, New EventArgs())
Here, we created our own class with added three properties:
- ErrorMessage: The string that will be displayed as error message in case validation fails.
- IsValid: It is a read only property that tells whether the value contained in the TextBox is valid or not
- ValidationExpression: This property sets a regular expression that will be used to validate the content of the TextBox.
The class also exposes an event called ValidationFailed that will be raised if content entered does not match with the pattern indicated by the regular expression.
Commonly used regular expressions
ASP.NET regular expression validator provides some built in regular expressions that validate things such as e-mail and zip code. We will now create a class that provides such expressions readily to use.
Public Class CommonValidationExpressions
Public Shared Email As String =
Public Shared URL As String =
Public Shared USPhone As String =
Public Shared SSN As String =
Public Shared ZIP As String =
Public Shared DateMMDDYYYY As String =
Public Shared IPAddress As String =
We opted to create a class with shared members instead of an enumeration so that you can easily set the ValidationExpression property to one of these values.
In order to test our control we created a windows forms application and put our control on a form. We set validation expression to check e-mail address. You can run the application and test for various other regular expressions.
I hope you must have liked the article. See you soon.