Thursday, November 29, 2012

RegularExpression Validation using Regex class in C# Windows Application

Asp.Net provides a RegularExpression Validator to validate custom expressions, in Windows Forms there are no such validators. However we can make use of the methods in the Regex class to validate custom expressions in C# Windows forms.

In this post we shall see on how to use the Regex class to validate custom expressions in a C# Windows forms Application.

The Regex class is part of the System.Text.RegularExpressions Namespace, to use this class we should first add the following line to the form.

using System.Text.RegularExpressions;
Now we can Create an instance of the Regex class, by passing the validation expression and use the IsMatch() method of the class to validate any string against the Regular Expression defined.

Here is the full Syntax.

regExp = new Regex(@"",RegexOptions.Compiled);
if (regExp.IsMatch())
      // Validation Success
      // Validation Failed

We can use this type of validation along with the ErrorProvider control to display a summary of all Validation errors in the form.

Search Flipkart Products:

No comments: