Click here to Skip to main content
12,690,002 members (26,022 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Validator Controls in ASP.NET

, 10 Aug 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Chief Technology Officer at Zealots
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
source.compiler11-Jul-13 12:47
membersource.compiler11-Jul-13 12:47 
GeneralMy vote of 5 Pin
Mallikharjuna Rao Kona8-Nov-10 22:38
memberMallikharjuna Rao Kona8-Nov-10 22:38 
GeneralCustom validator control on ASP.NET Pin
Member 333565331-Oct-10 8:18
memberMember 333565331-Oct-10 8:18 
GeneralMy vote of 1 Pin
Henry.Ayoola18-Aug-10 4:19
memberHenry.Ayoola18-Aug-10 4:19 
GeneralRe: My vote of 1 Pin
julian@giant26-Aug-10 6:02
memberjulian@giant26-Aug-10 6:02 
GeneralAnother good article on ASP.NET Validation control Pin
Member 333565320-Apr-10 12:03
memberMember 333565320-Apr-10 12:03 
GeneralGood Article Pin
jaivardhan joshi23-Dec-09 0:32
memberjaivardhan joshi23-Dec-09 0:32 
Generalhelp Pin
<font color=#FFD700>^^o000o^^14-Dec-04 20:17
member^^o000o^^14-Dec-04 20:17 
GeneralNo very informative Pin
aprenot18-Aug-04 12:08
memberaprenot18-Aug-04 12: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170113.4 | Last Updated 11 Aug 2004
Article Copyright 2004 by vivekthangaswamy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid