JavaScript
  Home arrow JavaScript arrow Page 2 - 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 
Moblin 
JMSL Numerical Library 
VPS 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? 
JAVASCRIPT

Form Validation with JavaScript
By: Nariman K, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 275
    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


    Form Validation with JavaScript - Check Point


    (Page 2 of 10 )

    In order to illustrate how form validation typically works, consider the following simple example:

    <html>
    <head>
    <basefont face="Arial">
    <script language="JavaScript">
    function validateForm()
    {
    // check name
    if (document.forms[0].elements[0].value == "")
    {
    alert ("Please enter a name!");
    return false;
    }
    // check password length
    if (document.forms[0].elements[1].value.length < 6)
    {
    alert ("Please enter a password 
    of at least 6 characters!");
    return false;
    }
    // check email address
    // check age
    if (isNaN(document.forms[0].elements[3].value))
    {
    alert ("Please enter a valid age!");
    return false;
    }
    // check age range
    if (parseInt(document.forms[0].elements[3].value) < 1 ||
    parseInt(document.forms[0].elements[3].value) > 99)
    {
    alert ("Please enter a valid age!");
    return false;
    }
    return true;
    }
    </script>
    </head>
    <body>
    <form action="someform.cgi" method="post" 
    onSubmit="return validateForm();">
    Name: 
    <br>
    <input type="text" name="name" id="name">
    <p>
    Password:
    <br>
    <input type="password" name="password" id="password">
    <p>
    Email address: 
    <br>
    <input type="text" name="email" id="email">
    <p>
    Age: 
    <br>
    <input type="text" name="age" id="age">
    <p>
    <input type="submit" value="Submit Form">
    </form>
    </body>
    </html>
    


    This is a simple form, with fields for the user to enter his or her name, a password, an email address, and an age value. Before the form is submitted, the "onSubmit" event handler calls the validateForm() function, which checks that
    • a name is present;

    • the length of the password is 6 or more characters;

    • the email address is in a valid format;

    • the age value is a number, and in the range 1-99.

    If any of these tests fail, the validateForm() function will print an error message and return false, which will prevent the form from being submitted. Only after all the validation tests are successfully passed will the function return true and allow the form submission to proceed. In this manner, basic client-side validation can prevent incorrect data from slipping through into your database.

    Since this kind of client-side validation is a fairly common task in the Web development world, it's a good idea to encapsulate some of the more common tasks in this context into reusable functions, which can be called as and when needed in your scripts, so as to reduce time and effort spent in this aspect of Web application development. The cleanest way to do this is to build a simple JavaScript form validation object, and add methods to it to validate different types of data. Let's look at that next.

    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