使用随机数组的简单 JavaScript 验证码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8987511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple JavaScript captcha using random array
提问by user1167075
I have created a simple Javascript Captcha but currently the values are randomly generated but I want to get values from the array only(want to randomized these two array), plz check my code I want to do this using simple Javascript with no dependency also plz fix the function validate().
我创建了一个简单的 Javascript 验证码,但目前这些值是随机生成的,但我只想从数组中获取值(想要随机化这两个数组),请检查我的代码我想使用简单的 Javascript 来做到这一点,也没有依赖修复函数validate()。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function myCaptcha() {
var leftnum =[5, 10, 15, 20];
var rightnum =[2, 4, 6, 8];
var leftnum = Math.floor(Math.random()*11);
var rightnum = Math.floor(Math.random()*11);
var tolnum = leftnum + rightnum ;
document.getElementById("sh").innerHTML = leftnum + " + " + rightnum;
}
function validate() {
var captchanow = document.getElementById("captcha");
if (captchanow != "") {
alert("Please enter a captcha");
return false;
}
else if(captchanow == tolnum) {
alert("wrong captcha entered");
return false;
}
else {
form.submit
}
return true;
}
</script>
</head>
<body onload="myCaptcha()">
<form action="" method="post" onsubmit="return false;">
<p>Captcha :
<input name="captcha" type="text" id="captcha" value="" />
<label id="sh" value=""></label></p>
<p>
<input type="submit" name="button" id="button" value="Submit" onclick="validate()"/>
</p>
</form>
</body>
</html>
回答by joar
I discourage you strongly from using a JavaScript captcha as it will entirely remove the point of a captcha, which is to not be machine-readable.
我强烈建议您不要使用 JavaScript 验证码,因为它会完全消除验证码的要点,因为它不是机器可读的。
Instead, I would recommend you to switch to something like reCAPTCHA.
相反,我建议您切换到reCAPTCHA 之类的东西。
Using a JavaScript CAPTCHA will only upset your users and spambots may solve it with their eyes closed [figuratively].
使用 JavaScript CAPTCHA 只会让您的用户感到不安,而垃圾邮件机器人可能会闭上眼睛 [比喻地] 解决它。
回答by Guffa
First of all, a Javascript CAPTCHA is completely pointless, as the bots that you want to stop won't be running the Javascript in the page.
首先,Javascript CAPTCHA 完全没有意义,因为您想要阻止的机器人不会在页面中运行 Javascript。
Anyhow, this is how you pick items from arrays:
无论如何,这就是您从数组中挑选项目的方式:
var leftnums =[5, 10, 15, 20];
var rightnums =[2, 4, 6, 8];
var leftnum = leftnums[Math.floor(Math.random() * leftnums.length)];
var rightnum = rightnums[Math.floor(Math.random() * rightnums.length)];
To get the value of the input field, use the value
property:
要获取输入字段的值,请使用以下value
属性:
var captchanow = document.getElementById("captcha").value;
To check if two numbers differ, use the !=
operator, and parse the string to an integer value:
要检查两个数字是否不同,请使用!=
运算符,并将字符串解析为整数值:
else if(parseInt(captchanow,10) != tolnum) {