gpt4 book ai didi

javascript - 检查变量输入JS的可行方法

转载 作者:行者123 更新时间:2023-12-03 06:56:03 25 4
gpt4 key购买 nike

我正在尝试为项目创建自定义验证码,我想知道如何检查输入是否等于变量。

window.onload = function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var place = document.getElementById("random");
for( var i=0; i < 5; i++ ) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
};
console.log(text);
place.innerHTML = text;
};
};

顺便说一句,我是 JavaScript 新手。

随机字符串生成器部分不是我的。

最佳答案

你在一个跨度中有一个验证码,剩下要做的就是放置一个输入框,检索其内容并将它们与验证码进行比较。(我使用阿里的代码作为起点)

        window.onload = function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var place = document.getElementById("random");

for (var i = 1; i <= 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
};

console.log(text);
place.innerHTML = text;
};

//This code waits for the user to press the enter key in order to trigger the function that compares the strings
inputbox = document.getElementById("userinput"); inputbox.addEventListener("keydown", function(e) {
if (e.keyCode === 13) { //checks whether the pressed key is "Enter"
validate(e);
}
});

//And this actually performs the comparison and shows a response
function validate(e) {
var captchatext = document.getElementById("random").innerHTML;
var usertext = document.getElementById("userinput").value;
if (captchatext == usertext) {
alert("Correct captcha");
} else {
alert("Incorrect captcha");
}
}

        <span id = "random" > < /span>
<input id = "userinput" type = "text" >

关于javascript - 检查变量输入JS的可行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37277162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com