ASP.NET
  Home arrow ASP.NET arrow Page 7 - 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 
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

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 - A Closer Look


    (Page 7 of 9 )


    Here is a closer look at the code for this example:


    <%
     
    // 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" EnableClientScript="false"/>
     
    <
    asp:CustomValidator id="txtMyNumberCV" runat="server" OnServerValidate="NumberValidate" ControlToValidate="txtMyNumber" ErrorMessage="Please enter an even number." display="dynamic" EnableClientScript="false" /> <br/>
     
    // snip
     
    %>

    The only difference between the two examples is that I have used the "OnServerValidate" attribute here instead of the "ClientValidationFunction" attribute used in the previous example. From the nomenclature of the attribute, it is clear that this attribute is used to associate a function with the validation control, in this case the NumberValidate() function.


    <%
     
    protected 
    void NumberValidate(object sourceServerValidateEventArgs
    args
    ) {
     args
    .IsValid = (Convert.ToInt16(args.Value)%== 0);
    }
     
    %>


    Once again, you can write your own custom code so long as the function follows the following syntax:


    protected void MyValidationFunction(object sourceServerValidateEventArgs
    args
    ) {
     

     
    // code comes here
    }

    Instead of using the vanilla HTML <submit> form control, I have opted for an ASP.NET server control, as shown below.


    <% 
     
    <
    asp:Button OnClick="Submit_Click" Text="Submit" runat="server" />
     
    %>


    This <asp:button> control comes with a handy "OnClick" attribute that can be used to assign a function. In the example above, I have defined a Submit_Click() function for this purpose.


    <%
     
    void Submit_Click(object sourceEventArgs args) {
        
    if (IsValid) {
      output
    .Text "Thank you very much.";   
     
    } else {
      output
    .Text "Sorry, an error has occurred.";
     
     }

    }
     
    %>


    Basically, this function checks the status of the "IsValid" property of the ASP.NET page. I had told you earlier that this property is set to "true" if there are no validation errors or "false" if any of the server control validation rules fail. The Submit_Click() function then utilizes this property to update the text in the label server control with an appropriate message.

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


     

    ASP.NET ARTICLES

    - 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
    - Building a Simple Storefront with LINQ





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