ASP.NET
  Home arrow ASP.NET arrow Page 8 - Improved Input Validation
Iron Speed
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 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Improved Input Validation
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2004-03-31

    Table of Contents:
  • Improved Input Validation
  • Common Applications
  • Regular Joe
  • The Number Game
  • Custom Craft
  • validateNumber() function
  • A Closer Look
  • A Comedy of Errors
  • Server Control

  • 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

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Improved Input Validation - A Comedy of Errors


    (Page 8 of 9 )

    One thing that has been pretty consistent across all the validation controls you've seen so far has been the display of error messages. I have always placed them just under the corresponding server control. Interestingly, there is one more way to display these ugly error messages to the user, and ensure that he/she rectifies them immediately. How? With a ValidationSummary server control that displays all the error messages at one go. Take a look:


    <%@ Page Language="C#" %>
    <html>
     
    <head>
       
    <title>Credit Card Information</title>
        
    <basefont face="Arial">
     
    </head>
     
    <body>
        
    <div align="center">
        
    <h2>Credit Card Information</h2>
     
       <
    form runat="server" method="POST" >
     
      <
    asp:ValidationSummary runat="server" ID="cc_validsumm" ValidationSummaryDisplayMode="bulletlist" HeaderText="Sorry, the form could not be submitted because the following errors occurred:" />
     
    <!-- 
    Credit Card Number -->
     
      <
    asp:label id="lblcc_num" runat="server" text="Credit Number:  " />
      
    <asp:textbox id="cc_num" runat="server" ></asp:textbox><br/>
     
      <
    asp:RequiredFieldValidator id="cc_numRFV" ControlToValidate="cc_num" ErrorMessage="Please enter your credit card number!" runat="server" Display="none" EnableClientScript="false"/>
     
      <
    asp:RegularExpressionValidator id="cc_numREV" runat="server" ControlToValidate="cc_num" ErrorMessage="Please enter the correct credit card number [XXXX-XXXX-XXXX-XXXX]" ValidationExpression="[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}"
    Display
    ="none" EnableClientScript="false"/><br/>
     
    <!-- 
    Credit Card Type -->
     
      <
    asp:label id="lblcc_type" runat="server" text="Credit Card Type
    [Mastercard/VISA]:  " 
    />
      
    <asp:textbox id="cc_type" runat="server"
     
      </
    asp:textbox><br/>
     
      <
    asp:RequiredFieldValidator id="cc_typeRFV" ControlToValidate="cc_type" ErrorMessage="Please select your credit card type!" runat="server" Display="none" EnableClientScript="false"/>
     
      <
    asp:RegularExpressionValidator id="cc_typeREV" runat="server" ControlToValidate="cc_type" ErrorMessage="Please select whether you have a Mastercard or a VISA!!!" ValidationExpression="Mastercard|VISA"
    Display
    ="none" EnableClientScript="false"/><br/>
     
    <!-- 
    Credit Card Date of Expiry -->
     
      <
    asp:label id="lblcc_doe" runat="server" text="Credit Card Date of
    Expiry: " 
    />
      
    <asp:textbox id="cc_doe" runat="server" >
      
    </asp:textbox><br/>
     
      <
    asp:RequiredFieldValidator id="cc_doeRFV" ControlToValidate="cc_doe" ErrorMessage="Please enter the credit card date of expiry!" runat="server" Display="none" EnableClientScript="false"/>
     
      <
    asp:RegularExpressionValidator id="cc_doeREV" runat="server" ControlToValidate="cc_doe" ErrorMessage="Please enter the correct date of expiry [MM/YYYY]" ValidationExpression="^([0][1-9]|[1][1-2])/20(0[3-9]|10)$"Display="none" EnableClientScript="false"/> <br/>
     
    <!-- 
    PIN code of card billing address -->
     
      <
    asp:label id="lblcc_pin" runat="server" text="PIN code of card billing
    address:  " 
    />
      
    <asp:textbox id="cc_pin" runat="server">
      
    </asp:textbox><br/>
     
      <
    asp:RequiredFieldValidator id="cc_pinRFV" ControlToValidate="cc_pin" ErrorMessage="Please enter your PIN code!" runat="server" Display="none" EnableClientScript="false"/>
     
      <
    asp:RegularExpressionValidator id="cc_pinREV" runat="server" ControlToValidate="cc_pin" ErrorMessage="Please enter a correct Pin Code (atleast five digits or more)." ValidationExpression="^[0-9]{5,}$"
    Display
    ="none" EnableClientScript="false"/><br/>
     
    <!-- 
    Email Address -->
     
      <
    asp:label id="lblcc_email" runat="server" text="Email Address:  " />
      
    <asp:textbox id="cc_email" runat="server" >
      
    </asp:textbox><br/>
     
      <
    asp:RequiredFieldValidator id="cc_emailRFV" ControlToValidate="cc_email" ErrorMessage="Please enter your email address!" runat="server" Display="none" EnableClientScript="false"/>
     
      <
    asp:RegularExpressionValidator id="cc_emailREV" runat="server" ControlToValidate="cc_email" ErrorMessage="Please enter a valid email address!" ValidationExpression
    "^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+
    (.[a-zA-Z0-9_-]+)+" 
    Display="none" EnableClientScript="false"/><br/>
     
    <
    asp:button id="Submit" Text="Submit" runat="server"/>
     
       </
    form>
      
    </body>
    </html>

    This example is a copy of one of the previous listings with a few minor changes. In the previous example, the error messages were displayed under the text boxes. However, in this case, they have been grouped together at the top of the page.

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


     

    ASP.NET ARTICLES

    - 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
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...
    - Order-Related Modules for an ASP.NET AJAX Se...
    - User and Role Management for an ASP.NET AJAX...
    - Programming an ASP.NET AJAX Server-Centric B...

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway