Solved

Make form validation and anti-spam work together

Posted on 2013-05-20
1
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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!

Question has a verified solution.

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

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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 …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

738 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