Please navigate to the bottom of the page for Table of Contents

Tuesday, May 24, 2011

ASP.NET page validation controls

In any non-trivial web-site that requires user to input some data, it is a must to validate the information submitted before you process it further. Validation also helps you against malicious user attacking your system, prevent SQL injection attacks, etc..

In general, there are two places to do input and data validation: on the client and the server. On the client side, ASP.NET provides various controls for automatically checking that the input is in a certain range. On the server, ASP.NET runs these same rules automatically for you. If you have more complex validation methods, these can be run on the server.

ASP.NET 4 includes six validation controls:

  • RequiredFieldValidator—Enables you to require a user to enter a value in a form field.
  • RegularExpressionValidator—Enables you to compare a value against a regular expression.
  • RangeValidator—Enables you to check whether a value falls between a certain minimum and maximum value.
  • CompareValidator—Enables you to compare a value against another value or perform a data type check.
  • CustomValidator—Enables you to perform custom validation.
  • ValidationSummary—Enables you to display a summary of all validation errors in a page.

You can associate the validation controls with any form controls included in ASP.NET Framework. For example, if you want to require a user to enter a value into a TextBox control, you can associate a RequiredFieldValidator control with the TextBox control.


<!-- Since this is a demo example, the aspx page does not have a code behind 
and put the C# code and server side functions in the aspx page itself -->

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<br />
Name: <asp:TextBox ID="tbName" runat="server"></asp:TextBox>
ID="RequiredFieldValidator1" runat="server"
ErrorMessage="You must enter your name"
ControlToValidate="tbName" ForeColor="Red"/>
<br />
Date of birth: <asp:TextBox ID="tbDOB" runat="server" />
ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Enter a date in the form MM/DD/YYYY"
ControlToValidate="tbDOB" ForeColor="Red"
<br />
Amount of candies you eat daily (&lt; than 1000):
<asp:TextBox ID="tbCandy" runat="server" />
ID="CompareValidator1" runat="server"
ErrorMessage="Number is not < 1000"
Operator="LessThan" ForeColor="Red"
ValueToCompare="1000" Type="Integer" />
<br />
Number of Doctor visits (1 - 10):
<asp:TextBox ID="tbDr" runat="server" />
ID="RangeValidator1" runat="server"
ErrorMessage="Enter a value from 1 to 10"
ControlToValidate="tbDr" ForeColor="Red"
MinimumValue="1" MaximumValue="10" />
<br />
<!-- Custom Validators work server side -->

<asp:ValidationSummary runat="server"
ID="validationSummary" ForeColor="Blue"/>

<br />
<asp:Button runat="server" Text="Submit" ID="btnSubmit" />

On running this page and entering incorrect values, the resulting output would look something like the following:


This should give you a good start on understanding and explaining the ASP.NET page validation controls. There is lot more to data validation than this. A very good resource is the MSDN documentation which is described in detail here.


  1. Thanks Mr Nikhil Singhal and his Team for this noble deeds.

    1. Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important.Project Center in Chennai

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Corporate TRaining Spring Framework the authors explore the idea of using Java in Big Data platforms.

      Spring Training in Chennai

      The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. This comment has been removed by the author.

  3. This comment has been removed by the author.

  4. Loose fitting robes are worn at the meditation resort reflect
    every state in India, and more than a hundred countries are sensual massage in london present.
    In my opinion, sensual massage in london this is a course that will make giving up smoking much easier.
    Now I am going to be general relaxation therapy, for the reality there is more focus on individuals who have high blood pressure medication and yarrow
    should not be used for more than six months.... My Web : Master Seo Blog Coretan Review Ernawati Blog Coretan Review Ernawati

  5. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
    qlik sense online training

  6. I have visited this blog first time and i got a lot of informative data from here which is quiet helpful for me indeed. 

    Tableau online training

  7. Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    msbi online training

  8. These are actually wonderful ideas in concerning blogging. You have touched some great technical points here. Any way keep up writing.
    Sql server dba online training

  9. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging..
    Oracle DBA Online Training

  10. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.
    Software testing online training
    Software testing certification training
    Software testing online course
    Software testing training course


  11. This post is really nice and informative. The explanation given is really comprehensive and informative. I want to share some information about the best oracle dba training and oracle weblogic tutorial training videos. Thank you .Hoping more articles from you.

  12. Pretty article! I found some useful information in your blog....

    so here we provide,

    We provide you with flexible services and complete hybrid network solutions. It can provide your organisation with exceptional data speeds, advanced external security protection, and high-resilience by leveraging the latest SD-WAN and networking technologies to monitor, manage and strengthening your organisation’s existing network devices.>

    #quadsel #network #security #technologies #managedservices #Infrastructure #Networking #OnsiteResources #ServiceDeskSupport #StorageServices #WarrantyAMCServices #datacentersolutions #DataCenterBuild #EWaste #InfraConsolidation #DisasterRecovery #NetworkingServices #ImagingServices #MPS #Consulting #WANOptimisation #enduserservices


  13. your blog are really helpful for all testers and beginners.this all provided knowledge are unique than other testing blog. Performance Testing Framework are lovely explain.keep updating,Thanks
    Software Testing Training in Chennai | Software Testing Training in Anna Nagar | Software Testing Training in OMR | Software Testing Training in Porur | Software Testing Training in Tambaram | Software Testing Training in Velachery

  14. Hmm!! This blog is really cool, I’m so lucky that I have reached here and got this awesome information.
    professional web design firm


  15. React Native was developed to make the development of mobile apps easier after ReactJS. The answer is straightforward: life is made much simpler if you can create an app once in JavaScript and release it to both Android and iOS.Using JavaScript, you can create native mobile apps with the React Native framework.

    React Native Training In Chennai