Validation Controls
I listed a few tips below for the proper use of validation controls.
Validation controls can be use with any other control that has the ValidationProperty attribute.
Check the value of Page.IsValid in the Page_Load method to test the values of all the Validation controls on the page.
There are six Validation Controls: RequiredFieldValidator, RangeValidator, CompareValidator, RegularExpressionValidator, CustomValidator and ValidationSummary.
Validation is performed on both the client and server sides. Validation can be turned off on the client with the setting EnableClientScript=false
There are three Display properties for the Validation Controls: Static (default), CSS Visibility property is set to hidden, space on web page taken up even if no message displayed; Dynamic, CSS Display property is set to None, space is used on the web page on if the message is displayed and None with the error displaying in the ValidationSummary.
The Text attribute will accept any HTML string, so it can be used to display an image. Ex: <img src=’filename.gif’ alt=’Name is required’ />
The ValidationGroup attribute allows for more than one form entry on a page, but it needs to be added to all the Validation controls and the button controls. Especially critical with Web Parts.
Controls can have more than one Validation Control assigned to them, and controls are assigned with the Validation Control’s ControlToValidate attribute.
To ensure the proper usage of a Cancel button add the CausesValidation=’false’ attribute so no validation will occur when the button is pressed.
With the RequiredFieldValidator use an InitialValue attribute when tied with a DropDownList to ensure user has selected a value other than the default value.
RangeValidator can use a DataType attribute to check the data type of the form field. Default setting is String.
There are three types of CompareValidator: Data Type, Fixed Value and Control comparison. The attributes are Type, ValueToCompare, ControlToCompare. Operator is a necessary value to determine how to compare. There are seven operator values: DataTypeCheck, Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, NotEqual.
When working with Dates, the CompareValidator checks the Short Date format: MM/DD/YYYY, and for currency no symbol is used.
CompareValidator can be used in the Page_Load method. Ex: cmpDate.ValueToCompare = DateTime.Now.ToString(”d”). The control still needs an operator and Type.
RegularExpressionValidator uses the ValidationExpression to validate with a regular expression supplied by the programmer. Regular Expressions are available at regexlib.com Visual Studio 2008 also has several regular expressions available when users open the ValidationExpression property.
The ValidationSummary shows the text set in the Validation control’s ErrorMessage property. If nothing has been set in the Text property, the ErrorMessage property will display in both the ValidationSummary and the Validation control section of the page. If Display=None than the message only appears in the summary.
There are three Display properties for the ValidationSummary: BulletList, List and SingleParagraph. There are three other properties for the summary: HeaderText, text above the summary display; ShowMessageBox, uses a popup alert box; and ShowSummary to control display of the summary.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments
No comments yet.
Leave a comment