Free Scripts


Form Input Validation JavaScript: Step 1
by Christopher Heng

Many websites, like thesitewizard.com, have a feedback form of some sort so that visitors can make comments to the webmasters . If you have such a form on your site, I'm sure that from time to time, you would have received the results of your form with some essential field (like the email address or the visitor's name, or even the feedback itself) omitted.
 
Don't worry - this is a common problem. One way around it is to validate the essential fields with a simple JavaScript.
 
To do this, you will need to add a call to your validation function when the form is submitted. You do this by adding a "onsubmit" attribute to your FORM tag, like the following (keep it on one line if possible):

<form action="mailto:yourname@yourdomain.com" method="post"
onsubmit="return checkform(this);">

If the field you want to validate is something like:
<input type="text" name="email">

then your validation routine will look like the following (put it, say, in the HEAD of your document):
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkform ( form )
{
    // see http://www.thesitewizard.com/archive/validation.shtml
    // for an explanation of this script and how to use it on your
    // own website

    // ** START **
    if (form.email.value == "") {
        alert( "Please enter your email address." );
        form.email.focus();
        return false ;
    }
    // ** END **
	return true ;
}
//-->
</SCRIPT>


Next Step: Form Input Validation in JavaScript: Step 1b >>


Copyright 2000 by Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from http://www.thesitewizard.com/

+ Sniffers
+ Forms
+ Windows
+ 4x Browser
+ Images
+ Links
+ Frames
+ Clocks/Time
+ Misc.

Navigate

 Home
 Submit Script
 Tutorials
 Forums
 Link To Us
 Contact
 Tell A Friend
 Links
 Bookmark Us

  Sign up for our free, low volume email newsletter. Be notified when new free scripts are added.

 Email Address