Simple Captcha using JavaScript

by Dinesh 2014-03-07 17:47:30

Simple Captcha using JavaScript

Generate simple captcha code using Math.random() function in javascript for websites to protect auto spam submission.

<script language="javascript" type="text/javascript">
function getCaptcha() {
var chars = "0Aa1Bb2Cc3Dd4Ee5Ff6Gg7Hh8Ii9Jj0Kk1Ll2Mm3Nn4Oo5Pp6Qq7Rr8Ss9Tt0Uu1Vv2Ww3Xx4Yy5Zz";
var string_length = 6;
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("randomCaptcha").innerHTML = captchastring;
}
</script>

<body onload="getCaptcha();">
<form name="hioxForm">
<table>
<tr><td colspan="2" align="center"><strong>Contact Us Form</strong></td></tr>
<tr><td>Name</td><td><input type="text" id="txtname"/></td></tr>
<tr><td>Contact no</td><td><input type="text" id="txtno"/></td></tr>
<tr><td>Message</td><td><textarea id="msg" style="width:155px"></textarea></td></tr>
<tr><td>Enter Captcha Text</td><td><input type="text" id="txtcode"/></td></tr>
<tr>
<td>
</td>
<td>
<div id="captcha">
<div id="captcha_gen">
<label align="center" id="randomCaptcha"></label>
</div>
</div><input type="button" value="Refresh" onClick="getCaptcha();"/></td></tr>
<tr><td align="center"><input type="button" value="Submit" onclick="fnsubmit()"/></td></tr>
</table>
</form>

Tagged in:

1094
like
0
dislike
0
mail
flag

You must LOGIN to add comments