JavaScript
  Home arrow JavaScript arrow Page 3 - Introducing Key Concepts for Form Validati...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
JAVASCRIPT

Introducing Key Concepts for Form Validation with the DOM
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-10-24

    Table of Contents:
  • Introducing Key Concepts for Form Validation with the DOM
  • Creating an old-fashioned form validation mechanism
  • Using the DOM for validating online forms
  • Removing error messages from the web page
  • Completing the validation script

  • 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


    Introducing Key Concepts for Form Validation with the DOM - Using the DOM for validating online forms


    (Page 3 of 5 )

    My initial approach for creating a DOM-based form validation script relies upon defining three core JavaScript functions. The first one will be responsible for checking whether the data entered in the respective form is valid or not, while the second and third ones will be tasked with displaying and hiding error messages respectively via the DOM.

    Having explained the driving logic of the pertinent form validation script, I'll define the signature of the first JavaScript function, called "showError()," which logically displays error notifications when a particular form entry is considered invalid. Here's the source code for the mentioned function:

    // display error messages via the DOM function showError(obj,message){      if(!obj.errorNode){               obj.onchange=hideError;               var span=document.createElement('span');               span.appendChild(document.createTextNode(message));               obj.parentNode.appendChild(span);               obj.errorNode=span;       }       valid=false;       return }

    If you examine the above function, then possibly you'll realize that it presents some interesting aspects that deserve an in-depth look. First, this function takes up the input box where the error was originally raised along with the respective error message as the unique incoming arguments.

    Obviously, this has a justification. Since the function appends a new <span> node to the offending form field and inserts the corresponding error notice as a text node, it displays errors without having to use alert methods. Wasn't that good?

    Also, you can see that an "onchange" event handler is attached to the input box in question, which triggers a new function named "hideError." Again, this makes sense, since the script assumes that the user has changed the wrong value entered in that field, thus the respective error message must be removed from the web document. Now, are you realizing how the DOM is used here for displaying and hiding notification messages? I bet you are.

    Well, having explained in detail how the "showError()" function does its business, it's time to move forward and take a look at another relevant function. I'm talking about the "hideError()" function, which will be defined in the following section. Not surprisingly it is tasked with removing the displayed error messages from the web page.

    Assuming that you're interested in learning how this new function will be created, click on the link shown below and keep reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this first part of the series, you'll learn how to use the DOM, in order to...
       · Hi:I would have loved to read your article, but when I increase the text size...
       · Hi, tedd. All of our sites are designed for 1024x768 resolution. The...
       · Hello Ted,Thank you for your interest on my DOM article. As Robert gently said...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







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