ASP.NET
  Home arrow ASP.NET arrow Page 5 - Input Validation With ASP.NET, Part 1
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Input Validation With ASP.NET, Part 1
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 38
    2004-02-16

    Table of Contents:
  • Input Validation With ASP.NET, Part 1
  • Making Friends
  • First Glance
  • If Looks Could Kill
  • Pizza Power
  • A Comparative Study

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Input Validation With ASP.NET, Part 1 - Pizza Power


    (Page 5 of 6 )

    So far, I have demonstrated the RequiredFieldValidator validation control. Now, take a look at this next example, which introduces the next member of the ASP.NET Validator family:


    <%@ Page Language="C#" %>
    <html>
    <head>
    <title>Great Pizza Offer</title>
    <basefont face="Arial">
    </style>
    </head>
    <body>
    <h2>The Great Pizza Offer</h2>
    <form runat="server" method="POST" >
     
    <
    asp:label id="lblPizzaName" runat="server" text="Which Pizza?" /> 
     
    <
    asp:textbox id="strPizzaName" runat="server"/>
     
    <
    asp:RequiredFieldValidator id="strPizzaNameRFV"
    ControlToValidate
    ="strPizzaName" ErrorMessage="You need to tell me which pizza you want!" runat="server" Display="dynamic" />
     
    <
    p> </p>
     
    <
    asp:label id="lblPizzaQuantity" runat="server" text="How many?" /> 
     
    <
    asp:textbox id="strPizzaQuantity" runat="server" />
     
    <
    asp:RequiredFieldValidator id="strPizzaQuantityRFV"
    ControlToValidate
    ="strPizzaQuantity" ErrorMessage="You need to tell me how many you want!" runat="server" Display="dynamic" />
     
    <
    asp:RangeValidator id="strPizzaQuantityRV"
    ControlToValidate
    ="strPizzaQuantity" ErrorMessage="You can select a maximum of three pizzas under this offer!" Type="Integer" MinimumValue="1"
    MaximumValue
    ="3"  runat="server" Display="dynamic"/>
     
    <
    p>
     
    <
    asp:button id="Submit" Text="Order" runat="server"/>
    </form>
    </body>
    </html>

    Hit the "Order" button without filling up the form correctly, and a whole bunch of error messages will be thrown back at you.

    Let's look at how I managed this feat:


    <%
    <asp:label id="lblPizzaName" runat="server" text="Which Pizza?" /> 
     
    <
    asp:textbox id="strPizzaName" runat="server"/>
     
    <
    asp:RequiredFieldValidator id="strPizzaNameRFV"
    ControlToValidate
    ="strPizzaName" ErrorMessage="You need to tell me which pizza you want!" runat="server" Display="dynamic" />
    %>


     
    Nothing new here: I've used the RequiredFieldValidator control again to ensure that the user enters the name of the pizza she wishes to order.


    <%
    <asp:label id="lblPizzaQuantity" runat="server" text="How many?" /> 
     
    <
    asp:textbox id="strPizzaQuantity" runat="server" />
     
    <
    asp:RequiredFieldValidator id="strPizzaQuantityRFV"
    ControlToValidate
    ="strPizzaQuantity" ErrorMessage="You need to tell me how many you want!" runat="server" Display="dynamic" />
     
    <
    asp:RangeValidator id="strPizzaQuantityRV"
    ControlToValidate
    ="strPizzaQuantity" ErrorMessage="You can select a maximum of three pizzas under this offer!" Type="Integer" MinimumValue="1"
    MaximumValue
    ="3"  runat="server" Display="dynamic"/>
    %>

    Now, here's something new: a RangeValidator control. As noted earlier, this allows you to specify a range of valid values for the ASP.NET control it is associated with. You've already seen what the id, ControlToValidate, and ErrorMessage attributes do; the RangeValidator control adds three new ones - Type, MinimumValue, and MaximumValue - that allow you to specify the datatype and boundary entries for the range of allowed values. In this case, the RangeValidator control will force the user to enter a number between 1 and 3.

    One more interesting thing about the example above: ASP.NET allows you to easily associate two Validator controls with the same server control. For example, in the example above, I have applied the RequiredFieldValidator and the RangeValidator controls to the same strPizzaQuantity text box control. The first forces the user to enter some value in the field; the second sets the allowed values for the field. This capability means that you can easily use a set of validation controls to enforce complex business rules.

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT