Solved

Make form validation and anti-spam work together

Posted on 2013-05-20
1
354 Views
Last Modified: 2013-05-20
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
Comment
Question by:wesmanbigmig13
1 Comment
 

Accepted Solution

by:
wesmanbigmig13 earned 0 total points
ID: 39183097
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Regular Expression 1-100 with two digits 15 23
is this a cms? 8 35
Angular - disable input field on page load 4 14
Put shading on half of picture 8 26
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now