以可行的方式检查变量以输入JS

时间:2022-03-03 00:58:37

I am trying to create a custom CAPTCHA for a project and i was wondering how i could check to see if an input was equal to a variable.

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

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;
        };
    };

BTW i am new to javascript.

BTW我是javascript的新手。

The random string generator portion is not mine.

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

2 个解决方案

#1


1  

You have a captcha in a span, all that is left to do is placing an input box, retrieving its contents and comparing them to the captcha.(I used Ali 's code as a starting point)

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

        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" >

        

#2


0  

You need to fix some syntax issues:

您需要修复一些语法问题:

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;
 };
    
<span id="random"></span>

If you want to increase a length of your generated string, just replace this condition i <= 5 with the other, for example: i <= 15 -> 15 symbols

如果要增加生成的字符串的长度,只需将此条件i <= 5替换为另一个,例如:i <= 15 - > 15个符号

#1


1  

You have a captcha in a span, all that is left to do is placing an input box, retrieving its contents and comparing them to the captcha.(I used Ali 's code as a starting point)

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

        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" >

        

#2


0  

You need to fix some syntax issues:

您需要修复一些语法问题:

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;
 };
    
<span id="random"></span>

If you want to increase a length of your generated string, just replace this condition i <= 5 with the other, for example: i <= 15 -> 15 symbols

如果要增加生成的字符串的长度,只需将此条件i <= 5替换为另一个,例如:i <= 15 - > 15个符号