Javascipt Form Check For Spam

Don't let spammers use your relay

On different sites that I have helped developed, we always made sure to secure our forms so that spammers cannot enter characters that will send their spam out to other users, from your gateway. While it is not unbreakable, it is a pretty easy way to stop spammers right from the get go.

NOTE: This function should never be used by itself, make sure that you do checking on the server side before sending out the email. This script is only meant to be the first piece of protection in the war against spam.

<script>

function checkForSpam() {

   var Comments, Name, Company, Email, Phone

   Comments = document.getElementById('Comments').value.toLowerCase();
   Name = document.getElementById('Name').value.toLowerCase();
   Company = document.getElementById('Company').value.toLowerCase();
   Email = document.getElementById('Email').value.toLowerCase();
   Phone = document.getElementById('Phone').value.toLowerCase();

   if (Comments.indexOf('cc:') != -1 || Name.indexOf('cc:') != -1 || Company.indexOf('cc:') != -1 || Email.indexOf('cc:') != -1 || Phone.indexOf('cc:') != -1) {
      alert("Due to spam rules, we do not allow the submission of this form with the text 'CC:' or 'BCC:' contained in it. Please try again.");
      return false;
   }

   if (document.getElementById('Name').value == '' || document.getElementById('Email').value == '' || document.getElementById('Phone').value == '' || document.getElementById('Company').value == '') {
      alert('You must enter your name, company, email address and phone in order to continue.');
      return false;
   }

}
</script>

To use this script on your page, you must add the following code to your form call:

<form action="thank-you.asp" method="post" onSubmit="return checkForSpam();">

   Name: <input name="Name" type="text" size="35" id="Name" maxlength="255" value="" class="">

   Email: <input name="Email" type="text" size="35" id="Email" maxlength="255" value="" class="">

   Phone: <input name="Phone" type="text" size="35" id="Phone" maxlength="255" value="" class="">

   Company: <input name="Company" type="text" size="35" id="Company" maxlength="255" value="" class="">

   Comments/Questions: <textarea name="Comments" cols="40" rows="6" id="Comments" class=""></textarea>

   <input type="submit" name="Submit" value="Submit Your Comments" />

</ form>

NOTE 2: You can remove and modify the above code as much as you want to get it to fit in with what elements you have on your page. To do so, either add or remove the calls in the javascript function.

Javascript

Ads