Click here to Skip to main content
Licence 
First Posted 19 Jun 2005
Views 23,117
Bookmarked 23 times

WinFormsRegexLibrary

By | 19 Jun 2005 | Article
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

About the Author

CWEIS



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey22:29 12 Mar '12  
GeneralGood RegEx...but Pinmembertwesterd20:27 25 Mar '06  
GeneralThank you for Share! Pinmemberyanminhua22:09 11 Jan '06  
QuestionHave you considered what validation for other sources might look like? PinmemberNick Harrison9:03 25 Nov '05  
GeneralGood Example & Neat Coding Pinmembertee_cpe18:00 19 Oct '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 20 Jun 2005
Article Copyright 2005 by CWEIS
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid