Anti-spam Measures in Javascript
First add Random Number generators
Put this in your header with your form validation functions so these variables are created every time the page loads:
function getRandom_0to10(){
var seed = Math.random();
//Multiply to create number 0.0-9.99, then round to integer
return seed = Math.ceil(seed * 10);
}
var x = getRandom_0to10(); // this is "x"
var y = getRandom_0to10(); // this is "y"
var answer = x + y; // this is what they add up to
Then add the line to your Form
<p>Please add these two numbers:
<script type="text/javascript">
document.write(x + " + " + y +" = ");
</script>
<input name="arithmetic" type="text" class="required add"
id="arithmetic" value=" " size="4" maxlength="2" />
</p>
Which displays as (with actual random numbers replacing the "x" and "y"):
Please add these two numbers: x + y =
Then add validation for this item
Add a parameter to your validation function (additions in red):
function validateForm(whichform,sumanswer){
and to the way you call it:
<form id="survey" name="survey"
onsubmit="return validateForm(this,answer)"
action="/cgi-bin/form-mail/~webclass/raneym3/forms/form.cfg" method="post"/>
Add this to your validation function after you've checked other parameters:
//validate addition
if (element.className.indexOf("add") != -1){
if(element.value != sumanswer){
alert ("Check your answer on the addition problem.");
return false;
}
}
For a demo of this, go to my form on the NAS server
Page Updated
02.17.2010
