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

Improved Input Validation
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    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 - 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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek