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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 36
    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

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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