天天看點

js自定義驗證碼

分享一個js自定義的驗證碼

window.onload = function () {

    var code;

    $('.changePassword').click(function () {

        if ($('#CheckText').val().toUpperCase()!== code) {

            alert("驗證碼輸入錯誤!");

            return;

        }

        $('#ForgetPassword').submit();

    });

    if ($('#errorInfo').val() !== null && $('#errorInfo').val() !== "")

        alert($('#errorInfo').val());

// if ($('#username').val() !== null && $('#username').val() !== "")

// alert('使用者名:' + $('#username').val() + '\t\n' + '密碼:' + $('#userpassword').val());

    createCode();

    /*驗證*/

    function createCode() {

        code = "";

        var codeLength = 5; //驗證碼的長度

        var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

        for (var i = 0; i < codeLength; i++) {

            var charIndex = Math.floor(Math.random() * 32);

            code += selectChar[charIndex];

        if (code.length != codeLength) {

            createCode();

        $('#CheckPic').text(code);

    }

    $('#CheckRefresh').click(function () {

        createCode();

}