Click here to Skip to main content
15,746,741 members
Articles / Web Development / HTML
Posted 10 Aug 2004


21 bookmarked

Validator Controls in ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.51/5 (33 votes)
10 Aug 2004CPOL3 min read
Validator controls in ASP.NET.


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.


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.



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" 
errorMessage="Name must be entered" display="static">



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" 



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>



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>



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"
 errorMessage="Must be in multiples of 10" >

Examples In VBScript

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

Examples In JavaScript

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



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:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Chief Technology Officer at Zealots
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 2 Pin
ali_heidari_11-Jul-13 11:47
ali_heidari_11-Jul-13 11:47 
GeneralMy vote of 5 Pin
Mallikharjuna Rao Kona8-Nov-10 21:38
Mallikharjuna Rao Kona8-Nov-10 21:38 
GeneralMy vote of 1 Pin
Henry.Ayoola18-Aug-10 3:19
Henry.Ayoola18-Aug-10 3:19 
Less informative than MSDN
GeneralRe: My vote of 1 Pin
julian@giant26-Aug-10 5:02
julian@giant26-Aug-10 5:02 
GeneralGood Article Pin
jaivardhan joshi22-Dec-09 23:32
jaivardhan joshi22-Dec-09 23:32 
Generalhelp Pin
sanjit_rath14-Dec-04 19:17
sanjit_rath14-Dec-04 19:17 
GeneralNo very informative Pin
aprenot18-Aug-04 11:08
aprenot18-Aug-04 11:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.