free hosting   image hosting   hosting reseller   online album   e-shop   famous people 
Free Website Templates
Free Installer

Form Validation



This script from Javascript Source validates a form.

It has the following features:
  • ensures that the form can only be submitted once
  • checks that all fields are filled in
  • submits the form to a CGI mailform handler
  • resets the form when the page is loaded

  • Paste the code below in the head section of your page:
    Add this section inside your body tag with your other body requirements:
    And this section in the body itself:

    How It Works

    The function reset simply sets the value of all form elements to "", or null.


    The function checkFields ensures that all fields are filled in.

    If not, an alert box pops up asking you to fill in a particular field.

    For example, if you don't fill in your name, this section is called:
    if (document.emailform.name.value=="")
    {
    alert("Please enter your name");
    return false;
    }
    You are then returned to the form to fill in the missing field.


    When all fields are valid, the submit counter is initiated.

    This is set to zero at the top of the script:
    var submitcount=0;
    When the submit button is clicked, if submitcount==0, the form is submitted:
    if (submitcount == 0)
    {
    submitcount++;
    return true;
    }
    The return true line allows the form to be transmitted.


    The counter is now incremented, so its value is no longer zero.

    Hence, if the button is clicked again, an alert box pops up:
    alert("This form has already been submitted. Thanks!");
    return false;
    The return false line prevents the form from being re-submitted.


    In the body section, there are 2 hidden fields:
    <input type=hidden name=to value="you@your-website-address-here.com">
    <input type=hidden name=subject value="Feedback Form">
    These are for the the CGI mail program - in this example, mail.pl.

    They tell the mail handler to send the form to you via email at the address you specify.


    Try It Out

    Your Name:   
    Your Email:  
    
    Comments:
    
    
    
    
    



    © 2001 CyberSchool