What are the different types of Validation controls in ASP.NET?

 ASP.NET provides validation server controls. All validation controls inherits from BaseValidator class which contains the common validation properties and methods like ControlToValidate, Enabled, IsValid, EnableClientScript, ValidationGroup,Validate() etc.
ASP.Net provides a range of validation controls:

RequiredFieldValidator

RequiredFieldValidator validates compulsory/required input. It checks that the validated control contains a value. It cannot be empty. Can be used in conjunction with other validators on a control to trap empty values. Simply, we can use it to check if the input control has any value. The most important property in the RequiredFieldValidator is InitialValue.

<asp:RequiredFieldValidator>
<asp:RequiredFieldValidator id="validTxtName runat="server" controlToValidate="txtName" errorMessage="Name must be entered"

display="static">
</asp:RequiredFieldValidator>

RegularExpressionValidator

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

CompareValidator

CompareValidator validates or compares the input of a control with another control value or with a fixed value. It 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 ValueToCompare, ControlToCompare, Operator, and type.

<asp:CompareValidator>
<asp:CompareValidator id="comvR" runat="server" display="static" controlToValidate="txtR" errorMessage="Rate must be numeric" type="Double"  operator="DataTypeCheck">
</asp:CompareValidator>

RangeValidator

RangeValidator validates the range. Validates that input falls between the given range values.It checks if the input controls 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>
<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>

CustomValidator

CustomValidator allows to customize the validation logic with respect to our application logic. It 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>
<asp:CustomValidator id="cusvDeptNum" runat="server" display="static" controlToValidate="txtDeptNum" onServerValidate="validateDeptNum" errorMessage="Must be in multiples of 10" >
</asp:CustomValidator>

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>
<asp:ValidationSummary id="valSummary" runat="server" headerText="Please correct the following errors" display="static" 

showSummary= "True" />

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*