Click here to Skip to main content
Click here to Skip to main content

WinFormsRegexLibrary

, 19 Jun 2005
Rate this:
Please Sign up or sign in to vote.
A regular expression validation library for Windows Forms.

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# 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.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

Share

About the Author

CWEIS

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermanoj kumar choubey12-Mar-12 22:29 
GeneralGood RegEx...but Pinmembertwesterd25-Mar-06 20:27 
GeneralThank you for Share! Pinmemberyanminhua11-Jan-06 22:09 
QuestionHave you considered what validation for other sources might look like? PinmemberNick Harrison25-Nov-05 9:03 
GeneralGood Example & Neat Coding Pinmembertee_cpe19-Oct-05 18:00 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 20 Jun 2005
Article Copyright 2005 by CWEIS
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid