Tool class - generate image verification code and verify

Generate image verification code and verify

 

A simple front-end JS to generate verification code and verify the function

 

<html>  
    <head>
        <title>Verification Code</title>  
        <style type="text/css">  
        #code
        {
            font-style:italic;
            font-weight:bold;
            border:0;
            letter-spacing:2px;
            color: #4567ff;
            background-color: #e4e4e4;
        }
        </style>
    </head>
    <body>
        <div>
        <form action="***" method='POST' id='loginForm'>
            <input type = "text" id = "inputCode"/>
            <input type = "button" id="code" onclick="createCode()"/>
            <button type="button" id="submitBtn">登录</button>
        </form>
        </div>
    </body>  
</html>

 

$(function(){
    createCode();
    var code;
    function createCode() {
        code = "";
        var codeLength = 4;
        var checkCode = document.getElementById("code");

        var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
        for(var i = 0; i < codeLength; i++) {
            var index = Math.floor(Math.random()*36);
            code += random[index];
        }
        checkCode.value = code;
    }

    $("#submitBtn").click(function () {
        var inputCode = $("#inputCode").val().toUpperCase();
        if(inputCode.length <= 0) {
            alert("Please enter the verification code");
        } else if(inputCode != code ) {
            alert("Verification code input error");
            createCode();
            $("#inputCode").val("")
        } else {
            $("#loginForm").submit();
        }
    });
})
 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326406656&siteId=291194637