Hi there!
I'm working on a form for our company website, and despite having reCaptcha successfully installed on it, my boss insisted he wanted one of those "2+2=4" boxes. So I resorted to a simple Javascript. The problem is, I have formmail set up... so when the wrong number is entered, an alert comes up telling the user such, but then it goes through with the POST action and sends the email anyway. I can catch it when the user leaves the field blank, due to the required field tag, but... is it possible to stop the page in the above instance?
code (edited):
If someone could help, I'd really appreciate it.
I'm working on a form for our company website, and despite having reCaptcha successfully installed on it, my boss insisted he wanted one of those "2+2=4" boxes. So I resorted to a simple Javascript. The problem is, I have formmail set up... so when the wrong number is entered, an alert comes up telling the user such, but then it goes through with the POST action and sends the email anyway. I can catch it when the user leaves the field blank, due to the required field tag, but... is it possible to stop the page in the above instance?
code (edited):
<script type="text/javascript">
function addNums(){
var answer = document.getElementById("answer").value;
var digit1 = parseInt(document.getElementById("digit1").innerHTML);
var digit2 = parseInt(document.getElementById("digit2").innerHTML);
var sum = digit1 + digit2;
if(answer == ""){
alert("Please add the numbers.");
}else if(answer != sum){
alert("The numbers were added incorrectly.");
}else{
// all good now! //
document.getElementById("status").innerHTML = "Correct, it is now safe to submit the form";
document.getElementById("answer").value = "";
}
}
function randomNums(){
var rand_num1 = Math.floor(Math.random() * 10) + 1;
var rand_num2 = Math.floor(Math.random() * 10) + 1;
document.getElementById("digit1").innerHTML = rand_num1;
document.getElementById("digit2").innerHTML = rand_num2;
}
</script>
....
<body onload="randomNums();">
....
<form method="post" action=".....formmail.php" name="ContactForm" onSubmit="stamp()">
<input type="hidden" name="required" value="FullName:Your Name,email:Your E-mail Address,PhoneNumber:Your Phone
Number,number:The numbers were not added properly" />
....
<td colspan="2"><br><span>Please verify you are human:</span>
<span id="digit1"></span> + <span id="digit2"></span> = <input type="text" name="answer" id="answer"
style="background-color:#ff9966" /> <span style="font-size:12px">*required</span>
<div id="status"></div></td></tr>
<tr><td width="300"></td>
<td width="75"><br>
<input value="Submit" name="submit" id="submit" type="submit" onclick="addNums();" />
If someone could help, I'd really appreciate it.