如何使用JavaScript对Captcha进行验证?

时间:2014-02-24 12:58:43

标签: javascript html5 asp.net-mvc-4

我是UI设计的新手。我创建了Captcha,请有人给我一个使用JavaScript验证Captcha的想法。提前谢谢。我在下面添加了我的代码。

获取新Captcha的代码:

<html>
<head>
<script language="javascript" type="text/javascript">
function getCaptcha() {
    var chars = "0Aa1Bb2Cc3Dd4Ee5Ff6Gg7Hh8Ii9Jj0Kk1Ll2Mm3Nn4Oo5Pp6Qq7Rr8Ss9Tt0Uu1Vv2Ww3Xx4Yy5Zz";
    var string_length = 5;
    var captchastring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        captchastring += chars.substring(rnum,rnum+1);
    }
    document.getElementById("randomfield").innerHTML = captchastring;
}
</script>
<style>

HTML代码:

</head>
<body onload="getCaptcha();">
<form name="randform">
<table style="border:1px solid #ecece4">
<tr><td colspan="2" align="center"><strong>Contact Us Form</strong></td></tr>
<tr><td>Enter Captcha Code</td><td><input type="text" id="txtcode"/></td></tr>
<tr>
<td>
</td>
<td>
<div id="captcha">
<div id="captcha_gen">
<label align="center" id="randomfield"></label>
</div>
</div><input type="button" value="Refresh" onClick="getCaptcha();"/></td></tr>
<tr><td colspan="2" align="center"><input type="button" value="Submit"/></td></tr>
</table>
</form>
</body>
<html>

1 个答案:

答案 0 :(得分:0)

function validateCaptcha()
{

    var chr = document.getElementById("tbxCaptcha").value;
    var cap = document.getElementById("randomfield").innerHTML;
    if (chr==cap)
    {
        return true;
    }
    else
    {
        alert ("Please enter valid verification code");
        return false;
    }
}