• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

Make form validation and anti-spam work together

Hi Experts

I have got the following two Javascripts and I need BOTH to be run and successfully passed when a form is submitted.

1. This one is a form field validation script:

<SCRIPT LANGUAGE="JavaScript">
<!--
function testData() {
if (document.form.fullname.value == "") {
alert ("\nPlease provide your Full Name\t\n")
return false;
}
if (document.form.email.value == "") {
alert ("\nPlease provide a valid Email Address\t\n")
return false;
}
if (document.form.phone.value == "") {
alert ("\nPlease provide a contact Phone Number\t\n")
return false;
}
if (document.form.bookingdate.value == "dd/mm/yyyy") {
alert ("\nPlease provide a Booking Date\t\n")
return false;
}
if (document.form.pickup.value == "") {
alert ("\nPlease provide a Pickup Up location\t\n")
return false;
}
if (document.form.dropoff.value == "") {
alert ("\nPlease provide a Drop Off location\t\n")
return false;
}
if (document.form.occasion.value == "") {
alert ("\nPlease let us know which Occasion your booking is for\t\n")
return false;
}
if (!document.form.tcagree.checked) {
alert ("\nPlease tick the box to confirm that you agree with our Terms and Conditions\t\n")
return false;
}

}
// -->
</SCRIPT>

2. This one is an anti-spammer validation script that asks the user to enter the answer to a simple x + y sum:

<script type="text/javascript">
    var a = Math.ceil(Math.random() * 10);
    var b = Math.ceil(Math.random() * 10);      
    var c = a + b
    function DrawBotBoot()
    {
        document.write("What is "+ a + " + " + b +"? ");
        document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
    }    
    function ValidBotBoot(){
        var d = document.getElementById('BotBootInput').value;
        if (d == c) return true;  
      alert("Please type the correct answer to submit your enquiry");    
        return false;
       
    }
    </script>

3. Here is my form header and onsubmit code:

<form method="post" action="send_form_email.php" name="form" id="form" onSubmit="return testData(), ValidBotBoot();">

The problem I am having is that at the moment the form is submitted for processing as long as the ValidBotBoot sum is answered correctly - that is to say that even if there are form fields that have been left blank and therefore fail the validation script, as long as the user answers the sum correctly then the form gets submitted anyway.

I need to find a way that the form is only submitted when all of the fields pass validation AND the user types in the correct answer to the x +  y sum.

How can I get this done?

Thanks
0
wesmanbigmig13
Asked:
wesmanbigmig13
1 Solution
 
wesmanbigmig13Author Commented:
I have solved this one myself by updating the form header code to be as follows:

<form method="post" action="send_form_email.php" name="form" id="form" onSubmit="return (ValidBotBoot()&&testData())">


I hope this helps someone else out sometime.

Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now