Solved

Make form validation and anti-spam work together

Posted on 2013-05-20
1
349 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SharePoint Online Adding A People Picker to Search 14 21
angularls and plnkr 14 17
JavaScript Scope issue 4 17
Bootstrap 3 icons 3 4
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

772 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

9 Experts available now in Live!

Get 1:1 Help Now