ASP.NET
  Home arrow ASP.NET arrow Page 6 - 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 - validateNumber() function


    (Page 6 of 9 )

    Now, it's time to bring the validateNumber() function to your notice. This JavaScript function is used to check whether the number entered by the user is an even number using a complex equation devised by the Greeks eons ago. Just kidding, folks - it's just good ol' arithmetic at work.


    <%
     
    // snip
     
    <script language="JavaScript">
    function validateNumber
    (oSrcargs) {
     args
    .IsValid = (args.Value == 0);
    }
    </script>
     
    // snip
     
    %>

    You can name your custom validation function however you like so long it confirms to the following syntax:


    function customFunction(oSrcargs) {
     
     
    // code comes here
    }

    Now that we have our custom validation function in place, it's time to associate it with the CustomValidator validation control defined earlier. The "ClientValidationFunction" attribute of the control is used to associate the validateNumber() function with the CustomValidator server control.

    Something to note here - more on this when we discuss server side validation using the "CustomValidator" control - is the "IsValid" property. This property is set to false if any of the validation controls present in the ASP.NET script fails. We shall see more of this property in action in the next example, which explains server-side validation.

    Bringing In The Server

    The previous example demonstrated custom validation using a client side JavaScript function. Let me now take the action to the server where you can define similar custom functions in C# (or any .NET compatible language) on the server.


    <%@ Page Language="C#" %>
    <html>
     
    <head>
      
    <title>Odd or Even...</title>
      
    <script language="C#" runat=server>
     
    protected 
    void NumberValidate(object sourceServerValidateEventArgs
    args

      
    {
      args
    .IsValid = (Convert.ToInt16(args.Value)%== 0);
      
    }
     
    void Submit_Click(object sourceEventArgs args) {
        

        
    if (IsValid) {
      output
    .Text "Thank you very much.";   
      
    } else {
      output
    .Text "Sorry, an error has occurred.";
     
     }

     

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


       </form>
      </body>
    </html> 

    As before, this example asks the user to enter an even number. If the user enters valid data, the text in the ASP.NET label control displays a "Thank You" message. And if the user enters an odd number, an error message immediately provides a warning.

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


     

    ASP.NET ARTICLES

    - 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...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT