JavaScript
  Home arrow JavaScript arrow Page 6 - Understanding The JavaScript Event Mod...
The Best Selling PC Migration Utility.
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

Understanding The JavaScript Event Model (part 1)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 35
    2002-06-25

    Table of Contents:
  • Understanding The JavaScript Event Model (part 1)
  • Popeye() And Olive()
  • Handling Things
  • Red Alert
  • Mouse Hunt
  • Forty Two
  • Flavour Of The Month
  • Linking Up
  • Game Over

  • 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
     
     
    Iron Speed
     
    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

    Understanding The JavaScript Event Model (part 1) - Forty Two
    (Page 6 of 9 )

    JavaScript also includes a bunch of event handlers for forms and form elements, including text fields, selection lists, and buttons.

    The onSubmit handler is triggered when a form is submitted. This handler is usually placed in the <form> tag. For example, consider the following form, which asks the user to enter a value into the text field and then pops up an alert with a message incorporating that value:

    <html>
    <head>
    <script language="JavaScript">
    function printMessage()
    {
    alert("No, the answer is not " + document.forms[0].answer.value
    + ".\nEveryone knows the answer is 42.");
    return false;
    }
    </script>
    </head>
    <body>
    <form action="submit.php" method="post" onSubmit="return
    printMessage()"> What is the answer? <br> <input type="text"
    name="answer"> <br> <input type="submit" value="Tell me the answer!">
    </form>
    </body>
    </html>
    Of course, this isn't all that useful. Here's another, more practical example:
    <html>
    <head>
    <script language="JavaScript">
    // check name field
    function checkName()
    {
    // if empty, pop up alert
    if (document.forms[0].name.value == "")
    {
    alert("Please enter a valid name");
    return false;
    }
    else
    {
    return true;
    }
    }
    // check email field
    function checkEmail()
    {
    var flag;
    var str = document.forms[0].email.value;
    // regex to match email addresses
    var pattern =
    /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/;
    var flag = pattern.test(str);
    if(!flag)
    {
    alert ("Please enter a valid email address");
    return false;
    }
    else
    {
    return true;
    }
    }
    // function to check all form data
    function checkForm()
    {
    if (checkName() && checkEmail())
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    </script>
    </head>
    <body>
    <form action="submit.php" method="post" onSubmit="return checkForm()">
    Please enter your name. <br> <input type="text" name="name"> <p> Please
    enter your email address. <br> <input type="text" name="email"
    size="25"> <br> <input type="submit" value="Hit me!"> </form>
    </body>
    </html>
    In case you haven't figured this one out yet, it pops up a warning dialog box if the user attempts to submit the form with an invalid name or email address - very useful when you want to cut down on mistyped or incorrect data being entered into your forms.

    There's one important quirk of the onSubmit handler that you should know about, because I've used it in both scripts above. Once the onSubmit event handler is invoked, the browser decides whether or not to process the form further on the basis of the value returned by the event handler. If the handler returns false, the form is not processed further; if it returns true, the form is submitted to the named form processing script.

    As the example above demonstrates, this comes in particularly handy when attempting to perform JavaScript-based validation of form data. It's possible to write a comprehensive set of JavaScript routines to verify the data entered into the form by the user, activate these routines via the onSubmit handler once the user submits the form, and only process the data in the form if the validation routines return a positive result. This is a technique used commonly on the Web to reduce the incidence of bad data entry.

    More JavaScript Articles
    More By Team Melonfire, (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

    Iron Speed



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