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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Name: <asp:TextBox ID="tbName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server"
ErrorMessage="You must enter your name"
ControlToValidate="tbName" ForeColor="Red"/>
<br />
Date of birth: <asp:TextBox ID="tbDOB" runat="server" />
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Enter a date in the form MM/DD/YYYY"
ControlToValidate="tbDOB" ForeColor="Red"
ValidationExpression="(0[1-9]|1[012])/([1-9]|0[1-9]|[12][0-9]|3[01])/\d{4}"
/>
<br />
Amount of candies you eat daily (&lt; than 1000):
<asp:TextBox ID="tbCandy" runat="server" />
<asp:CompareValidator
ID="CompareValidator1" runat="server"
ErrorMessage="Number is not < 1000"
ControlToValidate="tbCandy"
Operator="LessThan" ForeColor="Red"
ValueToCompare="1000" Type="Integer" />
<br />
Number of Doctor visits (1 - 10):
<asp:TextBox ID="tbDr" runat="server" />
<asp:RangeValidator
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" />
</div>
</form>
</body>
</html>


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


image


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.

27 comments:

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

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  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

    ReplyDelete
  5. Thank you for sharing this useful information.

    C C++ Training in Chennai | C++ Training

    ReplyDelete
  6. 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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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

    ReplyDelete
  9. 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

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

    ReplyDelete
  11. 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

    ReplyDelete

  12. 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.

    ReplyDelete

  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

    ReplyDelete
  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

    ReplyDelete

  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

    ReplyDelete
  16. "Very practical tips, much appreciated." for more .NET click here ...
    Dot Net Course

    ReplyDelete