Click here to Skip to main content
15,891,375 members
Articles / Desktop Programming / Windows Forms
Article

WinFormsRegexLibrary

Rate me:
Please Sign up or sign in to vote.
4.29/5 (4 votes)
19 Jun 20052 min read 33.6K   603   25   5
A regular expression validation library for Windows Forms.

Image 1

Introduction

WinFormsRegexLibrary is a regular expression validation library for Windows Forms. It provides validation methods for a number of different data, such as, phone numbers, Zip codes, email addresses, URLs, IP addresses, credit card numbers, date's, times, etc. The library is written in C#, and the demo is written in VB.NET. I will be posting a version of the library written in VB.NET, and a demo written in C#, soon. The concepts of using the library are the same, in either language.

Using the code

To use the library, the first thing you need to do is to copy the WinFormsRegexLibrary.xml file to your application's bin directory, in-order for the XML documentation comments to show up. Then add a reference to the WinFormsRegexLibrary.dll assembly. Then add an ErrorProvider control to your form, where you need validation. Then to use it in your code, you type the class name; RegexValidator, followed by the dot operator (.), and the name of the method you wish to use for validation.

Each method accepts two parameters, the first one being the control to validate, and the second one being the ErrorProvider to use for displaying an error message when incorrect data is entered. Optionally, some methods, such as the IsPresent method, also accepts a string for the name of the field to validate. This is used in the error message so the user knows which control is required, etc.

C#
// C# Sample Code

using WinFormsRegexLibrary;

public class MainForm : System.Windows.Form
{
    // Constructor
    public MainForm()
    {
    }

    public static void Main()
    {
        Application.Run(new MainForm());
    }

    // This is a sample validating method for a textbox,
    // could also be used for any control,
    // that supports the errorProvider.
    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
        if (RegexValidator.IsUSPhoneNumber(textBox1, errorProvider1)
        {
            e.Cancel = false;
        }
        else
        {
            e.Cancel = true;
        }
    }

    // This is a sample of validation the text,
    // as the user types. Note that this also does not make them enter
     // valid data before moving from one control to the next, or closing the form.
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (RegexValidator.IsUSPhoneNumber(textBox1, errorProvider1);
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
VB
'VB.NET Sample Code

Imports WinFormsRegexLibrary

Public Class MainForm

    ' This is a sample validating method for a textbox,
    ' could also be used for any control, that supports
    ' the errorProvider.
    Private Sub TextBox1_Validating(ByVal sender As Object, _
                ByVal e As System.ComponentModel.CanelEventArgs)
        If RegexValidator.IsUsPhonenUmber(TextBox1, ErrorProvider1) Then
            e.Canel = False
        Else
            e.Cancel = True
        End If
    End Sub

      ' This is a sample of validation the text,
      ' as the user types. Note that this also does not make them enter
      ' valid data before moving from one control to the next, or closing the form.
    Private Sub TextBox1_TextChanged(ByVal sender As Object, _
                                     ByVal e As System.EventArgs)
        If RegexValidator.IsUSPhoneNumber(TextBox1, ErrorProvider1) Then
            Return True
        Else
            Return False
        End If
    End Sub
End Class

Points of Interest

This library has a lot of validation methods, and I will keep adding to it. If you know of some common uses of regular expression validation that you would like to see in this library, please email me at codeintellects@msn.com.

History

  • Version 1.1 - No bugs, all expressions fully tested and working.

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey12-Mar-12 22:29
professionalManoj Kumar Choubey12-Mar-12 22:29 
GeneralGood RegEx...but Pin
twesterd25-Mar-06 20:27
twesterd25-Mar-06 20:27 
I think your article shows the use of RegEx well and I am sure many users like seeing samples of how to use RegEx to validate.

However, I personally believe that the example ties/ couples the validation to the form or presentation tier and I would recommend separating the validation itself from the form entirely. In fact, I would call the example HIGHLY coupled as you added an errorprovider as a parameter. Why would you do this? Create your own delegat and EventArgs, but don't use an ErrorProvider and tie the validation to the presentation of the data.

I like the article, but believe validation should not be coupled so tightly. I cringe at thinking that some user will do this in a real-life application and be left with what wil be much more difficult to mmaintain or modify.

There is another article here on the codeproject where the author created a RegEx library builder. Tie these two articles together and you have great harmony. (I.e the other article lacked passing the "why" back in the validation). You passed the why back but used an ErrorProvider.

Just my thoughts... take them as you see fit.

Trevor
GeneralThank you for Share! Pin
yanminhua11-Jan-06 22:09
yanminhua11-Jan-06 22:09 
QuestionHave you considered what validation for other sources might look like? Pin
Nick Harrison25-Nov-05 9:03
Nick Harrison25-Nov-05 9:03 
GeneralGood Example & Neat Coding Pin
tee_cpe19-Oct-05 18:00
tee_cpe19-Oct-05 18:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.