Skip to content

Instantly share code, notes, and snippets.

@cemerson
Created February 8, 2013 21:33
Show Gist options
  • Select an option

  • Save cemerson/4742112 to your computer and use it in GitHub Desktop.

Select an option

Save cemerson/4742112 to your computer and use it in GitHub Desktop.
JavaScript: Client-Side CAPTCHA (Math)
<!doctype html>
<html lang="en">
<head>
<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("Your math is wrong");
}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>
</head>
<body onload="randomNums();">
<strong>Add these numbers</strong>
<div id="digit1"></div>
<div id="digit2"></div>
<input type="text" id="answer" />
<input type="submit" value="Add Numbers" onclick="addNums();" />
<div id="status"></div>
</body>
</html>
@carrel
Copy link

carrel commented Jun 10, 2018

Thanks guys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment