Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Validator Controls in ASP.NET

0.00/5 (No votes)
10 Aug 2004 1  
Validator controls in ASP.NET.

Disclaimer:

This source code is written and maintained by an individual, not a company. It has been provided for free to the Microsoft .NET user community with the expectation that users will take whatever action necessary to get the code to operate according to their needs. This includes debugging and enhancements. The source code is provided "as is" and there are neither warrantees to the quality of the work nor any expressed or implied agreements that the programmer will release any updates. The programmer will release updates and provide any support at his own discretion.

Introduction

Validation Controls:

This set of controls provide Rapid Application Development (RAD) features for automatically checking the specified validity of user inputs. These controls are available in the System.Web.UI.WebControls namespace.

One of the most tiresome tasks when building interactive web applications is the requirement for validating values that the user enters into the input controls. This is particularly the case if we need to perform client-side as well as server side validation. Mostly, we use JavaScript for client side coding. Help is at hand with the range of validation controls that are included in ASP.NET.

They cover almost all the validation scenarios. A validation control enables us to validate an input and display an error message if necessary. It is very much like other server-side controls, with certain additional methods and properties. First, the server treats it as an invisible control. After the user has entered erroneous data, it becomes visible. It is a powerful, rapid application development feature; however, a developer needs to understand its behavior and the methods thoroughly before he or she can appreciate it. All the validation controls inherit from the base class BaseValidator, which is part of the class library namespace. System.Web.UI.WebControls.BaseValidator exposes a series of properties and methods that are common to all the validation controls.

RequiredFieldValidator::

<asp:RequiredFieldValidator>

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 id="validTxtName runat="server" 
controlToValidate="txtName" 
errorMessage="Name must be entered" display="static">
</asp:RequiredFieldValidator>

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>
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 ValueToCompare, ControlToCompare, Operator, and type.

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

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>

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>

Examples In VBScript

Sub validateDeptNum(source As Object, s as ServerValidateEventArgs)
  If (CInt(s.Value) Mod 10)=0 Then
     s.IsValid= True
   Else
     s.IsValid=False
   End If
End Sub

Examples In JavaScript

function validateLength(oSrc, args)
{
    args.IsValid = (args.Value.length >= 10);
}

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"
 headerText="Please correct the following errors"
 display="static" showSummary= "True" />

OK, for more about the validator controls: vivekthnagswamy@rediffmail.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here