Monday, August 12, 2013

Simple captcha using jQuery in PHP?


PHP Code

$length = 6;
$captcha = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);


HTML Code

<input type="text" name="captcha" value="<?php echo $captcha; ?>"  id="captcha" />

Enter the code above here :

<input type="password" name="Enter Code" />

Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</td>


JS Code

<script>
function refreshCaptcha()
{
 var text = '';
    var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

    for(var i=0; i < 6; i++)
    {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    $("#captcha").change().val(text);
}
</script>


Keep Smileing...

1 comment:

  1. nyc tut.., simple and understandable.. :)
    like also mine. http://pinoysourcecode.blogspot.com/

    ReplyDelete