ASP.NET
  Home arrow ASP.NET arrow Page 5 - 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 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Improved Input Validation - Custom Craft


    (Page 5 of 9 )

    So far, I have demonstrated four types of validation controls over two tutorials. This next one is the last in the entire series - and probably also the most interesting one of the lot. If you read the introduction, you'll remember the CustomValidator validation control that allows you to create customized validation routines.

    Take a look at the next example that explains client-side validation using a CustomValidator validation control.


    <%@ Page Language="C#" %>
    <html>
     
    <head>
      
    <title>Odd or Even...</title>
     
       <
    script language="JavaScript">
        
    function validateNumber(oSrcargs) {
        args
    .IsValid = (args.Value == 0);
       
    }
       </script>
     
      <basefont face="Arial">
     </head>
    <body>
     
      <asp:label id="output" runat="server" text="" />
     
      <form runat="server" method="POST" >
     
      <asp:label id="lblMyNumber" runat="server" text="Please enter your favourite even number: " />
      <asp:textbox id="txtMyNumber" runat="server" />
     
      <asp:RequiredFieldValidator id="txtMyNumberRFV" ControlToValidate="txtMyNumber" ErrorMessage="Please enter a number" runat="server" display="dynamic" />
     
      <asp:CustomValidator id="txtMyNumberCV" runat="server" ClientValidationFunction="validateNumber"
    ControlToValidate="txtMyNumber" ErrorMessage="Please enter an even number." display="dynamic" /> <br/>
     
      <input type="submit" value="Submit"> 
     
      </form>
     </body>
    </html>

    Enter a number and tab to the Submit button. The script will immediately warn the user if he/she enters an odd number.
     
    The first step is to add the CustomValidator control to the ASP.NET code and associate it with the server control to be validated.


    <%
     
    // snip
     
      
    <asp:label id="lblMyNumber" runat="server" text="Please enter your favourite even number: " />


      
    <asp:textbox id="txtMyNumber" runat="server" />
     
      <
    asp:RequiredFieldValidator id="txtMyNumberRFV" ControlToValidate="txtMyNumber" ErrorMessage="Please enter a number" runat="server" display="dynamic" />
     
      <
    asp:CustomValidator id="txtMyNumberCV" runat="server" ClientValidationFunction="validateNumber"
    ControlToValidate
    ="txtMyNumber" ErrorMessage="Please enter an even number." display="dynamic" /> <br/>
     
    // snip
     
    %>

    As you can see above, many of the attributes of this new CustomValidator control are similar to the ones encountered before. However, there is one new attribute, the "ClientValidationFunction" attribute, which is unique to this control. More on this in a moment.

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





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