Introduction
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:
Imports System.Text.RegularExpressions
Public Class ValidatingTextBox
Inherits System.Windows.Forms.UserControl
Private objExpr As Regex
Private strExpr As String
Private strErr As String
Friend WithEvents TextBox1 As
System.Windows.Forms.TextBox
Public Event ValidationFailed
(ByVal sender As Object, ByVal e As EventArgs)
Public Property ErrorMessage() As String
Get
Return strErr
End Get
Set(ByVal Value As String)
strErr = Value
End Set
End Property
Public ReadOnly Property IsValid() As Boolean
Get
If Not objExpr Is Nothing Then
Return objExpr.IsMatch(TextBox1.Text)
Else
Return True
End If
End Get
End Property
Public Property ValidationExpression() As String
Get
Return strExpr
End Get
Set(ByVal Value As String)
strExpr = Value
objExpr = New Regex(strExpr)
End Set
End Property
Private Sub TextBox1_Validated
(ByVal sender As Object, ByVal e As System.EventArgs)
Handles TextBox1.Validated
If Not Me.IsValid Then
RaiseEvent ValidationFailed(Me, New EventArgs())
End If
End Sub
End Class
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 =
"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Public Shared URL As String =
"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"
Public Shared USPhone As String =
"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"
Public Shared SSN As String =
"\d{3}-\d{2}-\d{4}"
Public Shared ZIP As String =
"\d{5}(-\d{4})?"
Public Shared DateMMDDYYYY As String =
"^\s*\d{1,2}(/|-)\d{1,2}\1(\d{4}|\d{2})\s*$"
Public Shared IPAddress As String =
"^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]
[0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]
|1[0-9][0-9]|[1-9][0-9]|[0-9])$"
End Class
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.
Using ValidatingTextBox
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.