Thursday, 17 May 2012

Validation Controls in ASP.NET: System.Web.UI.WebControls Class

System.Web.UI.WebControls contains all the validation controls. Here is a brief description of all the validation controls in ASP.NET:

1. RequiredFieldValidator (<asp:RequiredFieldValidator>)

Checks that the validated control contains a value. It cannot be empty.

<asp:RequiredFieldValidator
id="validateTxtName"
runat="server"
display="static"
controlToValidate="txtName"
errorMessage="Name must be entered" >
</asp:RequiredFieldValidator>

2. RegularExpressionValidator (<asp:RegularExpressionValidator>)

Checks the value against a regular expression (pattern). Checks that the value in the control matches a specified regular expression. If the validated control is empty, no validation takes place. The most important property in the RegularExpressionValidator is ValidationExpression.

<asp:RegularExpressionValidator
id="regvH"
runat="server"
display="static"
controlToValidate="txtH"
errorMessage="Hours must be 1-3 digits only"
validationExpression="\d{1,3}">
</asp:RegularExpressionValidator>

3. CompareValidator (<asp:CompareValidator>)

Checks if the value is acceptable compared to a given value or compared to the content of another control. In other words, it checks that the value in the validated control matches the value in another control or a specific value. The data type and comparison operation can be specified. If the validated control is empty, no validation takes place. The most important properties in the CompareValidator are ControlToCompare, Operator, and type.

<asp:CompareValidator
id="comvR"
runat="server"
display="static"
controlToValidate="txtR"
errorMessage="Rate must be numeric"
ValueToCompare="txtA">
</asp:CompareValidator>

4. RangeValidator (<asp:RangeValidator>

Checks if the input control’s value is within a specified range. In other words, it checks that the value in the validated control is within the specified text or numeric range. If the validated control is empty, no validation takes place. The most important properties in the RangeValidator are MaximumValue, MinimumValue, and type.

<asp:RangeValidator
id="ranvDependents"
runat="server"
display="static"
controlToValidate="txtDependents"
errorMessage="Must be from 0 to 10"
type="Integer"
minimumValue=0
maximumValue=10>
</asp:RangeValidator>

5. CustomValidator (<asp:CustomValidator>)

Allows you to develop custom validation. Performs user-defined validation on an input control using a specified function (client-side, server-side, or both). If the validated control is empty, no validation takes place. The most important property in the CustomValidator is ClientValidationFunction.

<asp:CustomValidator
id="cusvDeptNum"
runat="server"
display="static"
controlToValidate="txtDeptNum"
onServerValidate="validateDeptNum"
errorMessage="Must be in multiples of 10" >
</asp:CustomValidator>

6. ValidationSummary (<asp:ValidationSummary>)

Displays a summary of all current validation errors. In other words, reports a summary of all errors. The most important properties in the ValidationSummary are DisplayMode, ShowHeaderText, ShowMessageBox, and ShowSummary.

<asp:ValidationSummary
id="valSummary"
runat="server"
display="static"
headerText="Please correct the following errors"
showSummary= "True" />

No comments:

Post a Comment