Validation for captcha code using jquery and php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
priyanka1234
Forum Newbie
Posts: 1
Joined: Thu Mar 27, 2014 2:00 am

Validation for captcha code using jquery and php

Post by priyanka1234 »

I have a form in php which contains captcha code in it. I need to validate the captcha using jquery.If the captcha is wrong the form as display an alert saying the captcha u have entered is wrong.. Please tell me how can I validate it using jquery. Here is the code

Code: Select all

$(function() {
    $("#XISubmit").click(function(){
var photo= document.forms["XIForm"]["photo"].value;
if (photo==null || photo=="") { alert("Please Enter captcha"); return false; }

    document.getElementById("XIForm").submit();
        });

Code: Select all

<div class="formLeft">
        <h4>Admission</h4>

        <form name="XIForm" id="XIForm" method="POST" action="pdf/pdf.php">

<label>Security Validation</label>  

  <img src="captcha_image.php" alt=""/>
  <input  type="text" name="photo" maxlength="60" size="30"/>
<br><br>

</form>


captcha_image.php

<?
// *** CAPTCHA image generation ***
// *** http://frikk.tk ***

session_start();

// *** Tell the browser what kind of file is come'n at 'em! ***
header("Content-Type: image/jpeg");

// *** Send a generated image to the browser ***
die(create_image());

// *** Function List ***
function create_image()
{
    // *** Generate a passcode using md5
    //  (it will be all lowercase hex letters and numbers ***
    $md5 = md5(rand(0,9999));
    $pass = substr($md5, 10, 5);

    // *** Set the session cookie so we know what the passcode is ***
    $_SESSION["pass"] = $pass;

    // *** Create the image resource ***
    $image = ImageCreatetruecolor(100, 20);

    // *** We are making two colors, white and black ***
    $clr_white = ImageColorAllocate($image, 255, 255, 255);
    $clr_black = ImageColorAllocate($image, 0, 0, 0);

    // *** Make the background black ***
    imagefill($image, 0, 0, $clr_black);

    // *** Set the image height and width ***
    imagefontheight(15);
    imagefontwidth(15);

    // *** Add the passcode in white to the image ***
    imagestring($image, 5, 30, 3, $pass, $clr_white);

    // *** Throw in some lines to trick those cheeky bots! ***
    imageline($image, 5, 1, 50, 20, $clr_white);
    imageline($image, 60, 1, 96, 20, $clr_white);

    // *** Return the newly created image in jpeg format ***
    return imagejpeg($image);

    // *** Just in case... ***
    imagedestroy($image);
}
?>
Please help me how can I validate captcha using jquery
Last edited by requinix on Thu Mar 27, 2014 2:30 am, edited 1 time in total.
Reason: please use [syntax] tags when posting code
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Validation for captcha code using jquery and php

Post by requinix »

One of the main points of a CAPTCHA is that the client/browser cannot verify it. Only the server can. Using Javascript for it defeats the purpose.
Post Reply