Home » Programming » C# » Story Details
Printable Version

Custom Validator in ASP.NET

by Manish Tewatia on Feb 21, 2012

The Custom Validator control allows you to create a validation control with customized validation logic.
Comments: 0    Views: 254

Introduction

The Custom Validator control allows you to create a validation control with customized validation logic. For example, you can create a validation control that checks whether the value entered into a text box is an even number. If none of the other validators can help you, the Custom Validator usually can. It doesn't come with a predefined way of working; you write the code for validating your self. This is of course very powerful, since the possibilities are basically endless. A common way of using the Custom Validator is when you need to make a database lookup to see if the value is valid.

Validation controls always perform validation checking on the server. They also have complete client-side implementation that allows DHTML supported browsers  to perform validation on the client. Client-side validation enhances the validation process by checking user input before it is sent to the server. This allows errors to be detected on the client before the form is submitted, avoiding the round-trip of information necessary for server side validation.

Recall that each of ASP.NET's validation Web controls contain a required property called ControlToValidate. This property specifies what form field the validation control is set to validate. Hence, our custom validation function has the simple task before it.

  1. Read in the value of the form field that the CustomValidator is set to validate.
  2. Perform the validation logic.
  3. Specify if the form field is valid or not.

parameter contains two properties that are vitally important

  1. Value : indicates the value of the form field the CustomValidator control is validating.
  2. IsValid : a Boolean property that you must set to reflect if the form field's entry is valid or not. Set IsValid to true if the Value is valid, false otherwise.

Example : Simple example of custom validation is given below.

Code

<asp:CustomValidator

     id="ProgrammaticID"

     ControlToValidate="programmatic ID of Server Control to Validate"

     ClientValidationFunction="ClientValidateID"

     OnServerValidate="ServerValidateID"

     ErrorMessage="Message to display in ValidationSummary control"

     Text="Message to display in control"

     ForeColor="value"

     BackColor="value" 

     runat="server" >

</asp:CustomValidator>

Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName