Posts Tagged javascript

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 (form1.email.value == “”) {
alert( “Please enter your email address.” );
form1.email.focus();
return false ;
}
if (form1.FirstName.value == “”) {
alert( “Please enter your first name.” );
form1.FirstName.focus();
return false ;
}
if (form1.LastName.value == “”) {
alert( “Please enter your last name.” );
form1.LastName.focus();
return false ;
}
if (form1.company.value == “”) {
alert( “Please enter your company name.” );
form1.company.focus();
return false ;
}
if (form1.honeypot.value != “”) {
alert( “Do not enter anything into the last field. It catches bots.” );
form1.honeypot.focus();
return false ;
}
return true ;
}
//–>
</script>

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 />
</form>

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)