ASP.NET
  Home arrow ASP.NET arrow Page 3 - Improved Input Validation
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 
Moblin 
JMSL Numerical Library 
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


    Improved Input Validation - Regular Joe


    (Page 3 of 9 )

    Let's take a look at some code that builds on the theory you learned on the previous page:


    <%@ Page Language="C#" %>
    <html>
     
    <head>
      
    <title>Registration Form</title>
       
    <basefont face="Arial">
     
    </head>
     
    <body>
       
    <div align="center">
        
    <h1>User Registration Form</h1>
        
    <form runat="server" method="POST" >
        
    <asp:label id="lblUserName" runat="server" text="Username (at least eight alphabetic characters):  " />
        
    <asp:textbox id="strUserName" runat="server">
       
    </asp:TextBox><br/>
      
    <asp:RequiredFieldValidator id="strUserNameRFV" ControlToValidate="strUserName" ErrorMessage="Please enter a username!" runat="server" Display="dynamic" EnableClientScript="false"/>


      
    <asp:RegularExpressionValidator id="strUserNameREV" runat="server" ControlToValidate="strUserName" ErrorMessage="Please enter at least eight alphabetic characters!" ValidationExpression="[a-zA-Z]{8,}"
    display
    ="dynamic" EnableClientScript="false"/><br/>


      
    <asp:button id="Submit" Text="Submit" runat="server"/>
     
        </
    form>
       
    </div>
      
    </body>
    </html>

    Once you have previewed this example in the browser, it's time to review the code. First, I have the mandatory <asp:textbox> and <asp:label> server controls to display the form field in the browser. Next, I have the RequiredFieldValidator control, which forces the user to enter a value in the associated server control. It is essential to have this validation control in my ASP.NET form, for reasons to be explained in the next few paragraphs.

    This brings us to the RegularExpressionValidator validation control which, in addition to the standard "id", "ControlToValidate" and "ErrorMessage" attributes, also comes with a "ValidationExpression" attribute used to assign the regular expression to the validation control. Here, the value "[a-zA-Z]{8,}" is a regex that means "a string consisting of at least eight alphabetic characters".

    The other two attributes, "Display" and "EnableClientScript", have been set so that the control is only displayed if there is an error and the validation takes place on the server.

    Test the Control

    Finally, it's time to explain the reason for using a RequiredFieldValidator control above. Try the following: remove this validation control and submit a blank entry. You will find that no error is reported because of a simple quirk - if there is no data in the text control, the RegularExpressionValidator control does not get activated at all. This quirk means that the end user could end up entering a blank value and the ASP.NET script will not complain at all.

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


     

    ASP.NET ARTICLES

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





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