Archive for February, 2009

Save the Javascript!

I had a bit of a scare this morning when my web site editor (MUST get Dreamweaver back!) decided to delete a chunk of js from one of my client’s sites during a routine update. Hooray, I got to rewrite it. I figure that I ought to save it for posterity and my posterior somehow in case it happens again. It’s a working form validation script.

<script language=”JavaScript” type=”text/javascript”>
function checkform ( )
{if ( == “”) {
alert( “Please enter your email address.” );;
return false ;
if (form1.FirstName.value == “”) {
alert( “Please enter your first name.” );
return false ;
if (form1.LastName.value == “”) {
alert( “Please enter your last name.” );
return false ;
if ( == “”) {
alert( “Please enter your company name.” );;
return false ;
if (form1.honeypot.value != “”) {
alert( “Do not enter anything into the last field. It catches bots.” );
return false ;
return true ;

This chunk of script is placed inside the <head> tags. My form looks like this:

<form action=”/gdform.asp” method=”post” name=”form1″ onsubmit=”return checkform();”><input name=”subject” value=”About ” type=”hidden” /><input name=”redirect” value=”/articles/thankyou.shtml” type=”hidden” />First
Name:<br />
<input name=”FirstName” type=”text” /><br />
Last Name:<br />
<input name=”LastName” type=”text” /><br />
Email:<br />
<input name=”email” type=”text” /><br />
Company or Organization:<br />
<input name=”company” type=”text” /><br />
Comments: <br />
<br />
<textarea name=”comments” cols=”40″ rows=”3″>Type
comments here.</textarea><br />
<br />
Put nothing in this field. It’s a bot catcher. <input size=”1″ name=”honeypot” /><br />
<input name=”submit” value=”submit” type=”submit” /><br />

I use a honeypot as a bot-catcher. It’s still in the early stages, so we’re seeing how it works, but I have not been thrilled with CAPTCHAs in the past. They seem to be going out of style, as well as being unreliable. Remember when using a form validation to modify the “onsubmit” of your form tags so that it runs the script. Also, in the form validation script, make sure that in the if (form1.LastName.value == “”) tag, “form1” corresponds to the name of your form, as specified in the “form” tag.


Comments (1)