Click here to Skip to main content
Click here to Skip to main content

Validator Controls in ASP.NET

, 10 Aug 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

vivekthangaswamy
Software Developer (Senior) Swordface
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralMy vote of 2 Pinmembersource.compiler11-Jul-13 12:47 
GeneralMy vote of 5 PinmemberMallikharjuna Rao Kona8-Nov-10 22:38 
GeneralCustom validator control on ASP.NET PinmemberMember 333565331-Oct-10 8:18 
GeneralMy vote of 1 PinmemberHenry.Ayoola18-Aug-10 4:19 
GeneralRe: My vote of 1 Pinmemberjulian@giant26-Aug-10 6:02 
GeneralAnother good article on ASP.NET Validation control PinmemberMember 333565320-Apr-10 12:03 
GeneralGood Article Pinmemberjaivardhan joshi23-Dec-09 0:32 
Generalhelp Pinmember^^o000o^^14-Dec-04 20:17 
GeneralNo very informative Pinmemberaprenot18-Aug-04 12:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.141220.1 | Last Updated 11 Aug 2004
Article Copyright 2004 by vivekthangaswamy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid