अजपा योग आणि शांभवी मुद्रा ध्यान : श्वास, मंत्र, मुद्रा आणि ध्यान यांच्या सहाय्याने मनःशांती, एकाग्रता, चक्र संतुलन आणि कुंडलिनी जागृती. अधिक माहिती आणि आगामी तारखांसाठी येथे जा.

Create User Control to Validate Commonly Used Data Formats


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 
    Public Event ValidationFailed
    (ByVal sender As Object, ByVal e As EventArgs)

    Public Property ErrorMessage() As String
            Return strErr
        End Get
        Set(ByVal Value As String)
            strErr = Value
        End Set
    End Property

    Public ReadOnly Property IsValid() As Boolean
            If Not objExpr Is Nothing Then
                Return objExpr.IsMatch(TextBox1.Text)
                Return True
            End If
        End Get
    End Property

    Public Property ValidationExpression() As String
            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 = 
    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 = 
    Public Shared ZIP As String = 
    Public Shared DateMMDDYYYY As String = 
    Public Shared IPAddress As String = 
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.

Bipin Joshi is an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a meditation teacher and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his ASP.NET online courses go here. More details about his Ajapa Japa and Shambhavi Mudra online course are available here.

Posted On : 21 January 2003