?
Solved

Make form validation and anti-spam work together

Posted on 2013-05-20
1
Medium Priority
?
378 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

777 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