SunQuest
 
       DHTML
  Home arrow DHTML arrow Page 4 - Rough Guide To The DOM (part 2)
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? 
DHTML

Rough Guide To The DOM (part 2)
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2001-05-09

    Table of Contents:
  • Rough Guide To The DOM (part 2)
  • Making The Swap()
  • Turning The Tables
  • Well-Formed
  • In The Frame
  • Branching Out
  • Dumbing It Down
  • Conclusions

  • 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

    Rough Guide To The DOM (part 2) - Well-Formed


    (Page 4 of 8 )

    Another very popular use of JavaScript is form validation - verifying the data entered into an online form. In the next example, I'll be using the DOM and some simple JavaScript to ensure that the checkbox is ticked and the email address entered into the text field is in the correct format. Here's the form

    <html> <head> </head> <body> <form action="somescript.cgi" method="post" onSubmit="return check();"> Email address: <br> <input id="email" type="text" name="email" size="30"> <br> <input id="spam_me" type="Checkbox" name="spam_me">Yes, I want you to send me megabytes of useless advertisements via email. I love buying crummy products from people who probably flunked kindergarten. <br> <input type="submit"> </form> </body> </html>

    and here's the validation script.

    <script language="JavaScript"> function checkEmail() { // get to the field var obj = document.getElementById("email"); // value of field var str = obj.value; // define pattern to match email address var pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/; // match the string to the pattern var flag = pattern.test(str); if(!flag) { alert ("Please enter a valid email address"); return false; } else { return true; } } function checkSpam() { // get to the field var obj = document.getElementById("spam_me"); // checkbox ticked? if (obj.checked) { return true; } else { alert ("Please check the box"); return false; } } function check() { // perform validation and submit form if all is well if(checkEmail() && checkSpam()) { return true; } else { return false; } } </script>

    As you can see, the form is submitted only after receiving a positive (true) result from the JavaScript function check(). This function, in turn, calls the functions checkEmail() and checkSpam(),which first obtain references to their respective form elements and then check their values for validity.

    This article copyright Melonfire 2001. All rights reserved.

    More DHTML Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    DHTML ARTICLES

    - Rough Guide To The DOM (part 2)
    - Rough Guide To The DOM (part 1)
    - Filters And Transitions In IE5
    - Understanding Embedded Fonts





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