JavaScript
  Home arrow JavaScript arrow Page 7 - Form Validation with JavaScript
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

Form Validation with JavaScript
By: Nariman K, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 271
    2003-12-01

    Table of Contents:
  • Form Validation with JavaScript
  • Check Point
  • Object Lessons
  • Rock On
  • Hammer Time
  • How Things Work
  • A Little Space
  • Expressing Yourself
  • Under Construction
  • A Quick Snack

  • 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

    Form Validation with JavaScript - A Little Space


    (Page 7 of 10 )

    Let's begin by setting up the class definition:

    // create object
    function formValidator()
    {
    // snip
    }
    


    Now, since I plan to build some basic error-handling routines into this class, I need to add a variable to hold this information.

    // create object
    function formValidator()
    {
    // set up array to hold error messages
    this.errorList = new Array;
    // snip
    }
    


    Let's now begin constructing the validation routines themselves.

    Here's the first one, which performs a very basic test; it checks to see whether the corresponding variable contains a value or not.

    // check to see if input is whitespace only or empty
    function isEmpty(val)
    {
    if (val.match(/^s+$/) || val == "")
    {
    return true;
    }
    else
    {
    return false;
    } 
    }
    


    This is fairly simple, but worth examining in detail, since all subsequent methods will follow a similar structure.

    The isEmpty()method is called with a single argument: the value to be tested. It then uses the match() method of the JavaScript String object to check this value against a regular expression. In this specific example, the regular expression is designed to match a string consisting of spaces only; such a string can reasonably be considered "empty" of any actual data. If such a string is found, or if the value is found to contain nothing, the function returns true; if not (that is, if the value actually contains some data), the function returns false.

    As you will see, this type of structure for the object method makes it simple to wrap the method call in a conditional test in your form processing script.

    Let's move on to another interesting function:

    // check to see if input is number
    function isNumber(val)
    {
    if (isNaN(val))
    {
    return false;
    }
    else
    {
    return true;
    } 
    }
    


    In this case, the JavaScript isNaN() function is used to check whether the value supplied to it is a number or not.

    Another useful method, especially when dealing with numbers, is theisWithinRange() method, which provides an easy way to check whether the input is within a certain numeric range.

    // check to see if value is within min and max
    function isWithinRange(val, min, max)
    {
    if (val >= min && val <= max)
    {
    return true;
    }
    else
    {
    return false;
    } 
    }
    


    Finally, you can include a small method to verify if a particular checkbox is checked or not, via the isChecked() method:

    // check to see if form value is checked
    function isChecked(obj)
    {
    if (obj.checked)
    {
    return true;
    }
    else
    {
    return false;
    } 
    }
    


    Note the difference between this method, and the ones that preceded it. Previous object methods have been written to accept a form value as argument; this one accepts an object, representing the checkbox element, as argument.

    More JavaScript Articles
    More By Nariman K, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript





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