Validator ClassCleanCode C# Libraries v1.2.03 API
Validates form fields (OnlineTextBox or OnlineDataGridView controls) by attributes you define on them, manifesting with both an individual error indication and a disabling of a designated common form submittal (ok) button.
Inheritance Hierarchy

OnlineSystem Object
  CleanCode.Forms Validator

Namespace: CleanCode.Forms
Assembly: CleanCode (in CleanCode.dll) Version: (1.2.03)

public class Validator

Tired of writing custom validation code for every input field in your applications? This class provides a generic validation engine that can automate many validation scenarios. Besides the description here, I have also published an article discussing the practical uses of this tool -- OnlineExploring Secrets of Windows Form Validation -- available on the DevX online magazine.

A validation error may be displayed in either an individual OnlineLabel component unique to each control, or by using the standard OnlineErrorProvider component. The latter has the advantage that you do not have to create and associate a Label component with each validating component, but the disadvantage that you must use the mouse to see the validation error.

In order to validate a control, you add it to a Validator instance, then call one of the Validate methods inside either the OnlineTextChanged or OnlineValidating event handler. MSDN documentation suggests the Validating event handler is the appropriate place. Note that the handler will be called only when focus leaves a control. So if you want finer control (as in every keystroke) then TextChanged is a better choice.

In addition to adding the control to the Validator, you must attach attributes to the control that specify just what you want to validate. This is done using the Tag property. You specify a list of 1 or more attributes from those below that you want to validate. All of the ones you specify will be evaluated, but only one error will be reported at a time.

Each singular OnlineControl (e.g. TextBox) must have a validation attribute list (stored on the Tag property) in the following format:

The available attributes are:
field must contain no more than the specified number of characters
field must contain at least the specified number of characters
field must contain a number that is no larger than the specified value
field must contain a number that is no smaller than the specified value
field must match the specified regular expression

A collective control, such as a OnlineDataGridView, uses the same list of attributes, but the Tag definition is more complex. Each column in the DataGridView requires its own attribute list. The format is:

where each columnX is the name of a column and each attrListX is a complete validation attribute list as defined above.

The table below illustrates some sample validation attribute lists. Except for pattern, the attribute meanings should be clear. The pattern attribute is a regular expression (see Online.NET Framework Regular Expressions) providing a powerful, flexible mechanism for matching almost any input.

maxLen=10value may be no more than 10 characters
minVal=0value may not be negative
minVal=0;maxVal=100value must be between 0 and 100 inclusive
minLen=1value must be non-empty
pattern=.value must be non-empty
minLen=5;maxLen=5value must contain exactly 5 characters
pattern=.{5}value must contain exactly 5 characters
pattern=^\S+$value may not contain spaces
pattern=^-?(?:\d*\.?\d+|\d+\.)$value must be a number
pattern=^\d{3}-\d{3}-\d{4}$value must be a canonical US phone number
pattern=^\w[\w\.]*\@\w+\.\w[\w\.]*$value must be an e-mail address


In the OnlineLoad event handler for your OnlineForm include:

validator = new Validator(okButton);
validator.Add(xyzTextBox, xyzErrLabel);
validator.Add(otherTextBox, otherErrLabel);
. . . // Add additional controls to be validated.
Then in the OnlineTextChanged event handler for each OnlineTextBox, add this:
This current version only handles OnlineTextBox and OnlineDataGridView controls; more controls may be added in the future.

Since CleanCode 0.9.07.


For a DataGridView with columns (Cmd, Item, MatchExpr, Format), this Tag property:
specifies (coarsely speaking) that Cmd values must begin with a number or letter, Item values must contain letters and numbers but no spaces, MatchExpr values must contain a set of parentheses, and Format values must have at least 1 digit within a set of braces.
See Also