Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed



email article
printer friendly
get the code
more resources

Create Self-Validating Controls
Learn everything you need to know to write self-validating controls using regular expressions.
by Michael Kennedy

November 15, 2004

Technology Toolbox: C#, Windows Forms

Microsoft .NET lets you build complex applications quickly and easily, but its selection of controls does have notable gaps. Fortunately, you can address part of this deficiency yourself by building your own self-validating text input controls.

ADVERTISEMENT

.NET's existing controls restrict the user input based on the type of input required. The canonical example of this type of control is a TextBox control that lets the user enter only numbers. This kind of control should also provide real-time feedback indicating whether the text is valid as the user enters it. Microsoft Foundation Classes (MFC) includes these types of TextBox controls, such as the Edit control, but the MFC numerical Edit control takes the simplistic and less useful interpretation of this requirement to mean that you can enter only the characters 0 through 9 into the textbox. The MFC controls don't let you enter numbers such as 1.25 or -12 if you need to.

.NET lacks an equivalent to the MFC numerical Edit control, but it does have rich support for inheritance with regard to Windows Forms Controls. You can use inheritance to build an entire family of self-validating controls that go far beyond restricting input to the characters 0 through 9. You can use the power of inheritance and regular expressions to develop a set of TextBox-derived controls that support a variety of input.

The first step is to create a base class that serves as the foundation control. Next, build a TextBox control that supports restricting the input to a variety of number types, such as floating point numbers, integers, positive integers, and so on. Additionally, you can create a textbox that allows only Social Security numbers in the form of XXX-XX-XXXX—using only two lines of code outside the standard wizard-generated code (see Figure 1).

While Windows Forms Controls do include a built-in mechanism for input validation, that mechanism isn't the best way to build self-validating controls. You can provide realtime feedback and cancel invalid input by subscribing to the Validating and Validated events of a TextBox control and setting the CausesValidation property of the parent form. Unfortunately, this procedure gives no architectural guidance beyond allowing your form to decide whether the particular input is valid. Further, the validation code for the control itself is pushed into the consuming application, rather than encapsulated within the control—a significant drawback.

This increased level of encapsulation in self-validating controls has many benefits. For example, you can add your specialized controls to a common control library shared across many applications, contributing to a significantly higher level of code reuse. Another benefit: You can maintain and upgrade the validation process more easily because the implementation exists in only one location.

The built-in method is fine for cases where validating user input is not purely structural in nature. Validation is a good candidate for encapsulation in a self-validating control if the input is independent of the rest of your application's state.

Employ Regular Expressions
Regular expressions give you powerful tools for matching structured text. They're ideal for validating phone numbers, Social Security numbers, and even raw numerical input. The validation mechanism described in this article uses a regular expression system because it provides flexibility and power while retaining the familiar validation mechanism.

It's worth a high-level look at how you'll put the regular expressions to use before examining the details of creating the control proper. Don't be concerned if you're not familiar with regular expressions. The regular expressions used in this article aren't complex, and I'll explain everything you need to know to implement them in the validation control.

You can create a control that provides realtime feedback by letting the user type the input and have that input validated for each character as it's entered. This means you need two regular expressions in the validation process: one that validates partial input, and one that validates the final input.




Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
| | Discussions | Newsletters | FTP Home