2014-03-27 103 views
0

我在php中有一個包含驗證碼的表單。我需要使用jquery驗證驗證碼。如果驗證碼錯誤,表單顯示提示說驗證碼已輸入錯誤。請告訴我如何使用jquery驗證它。 下面是代碼使用jquery驗證碼驗證碼

$(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(); 
     }); 

<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); 
} 
?> 

請幫助我,我如何可以驗證使用驗證碼的jQuery

回答

-1

試試這個

$(function() { 
     $("#XISubmit").click(function(){ 
     var photo= document.forms["XIForm"]["photo"].value; 
     var cap = '<?php echo $_SESSION["pass"]; ?>' // try this 
     if (photo==null || photo=="" && photo !==cap) { 
     alert("Please Enter captcha"); return false; 
     } 
     else{ 
     document.getElementById("XIForm").submit(); 
     } 
      }); 
+0

只有當沒有輸入驗證碼這個WL顯示。我想驗證輸入的驗證碼是錯誤的,因爲顯示警報msg captcha輸入的是錯誤的!! – user3377635

0

如果你能準備或找到一個圖像生成PHP頁面,你可以得到圖片頁

`

// captcha creator 
session_start(); 
$code=rand(1000,9999); 
$_SESSION["code"]=$code; 
$im = imagecreatetruecolor(50, 24); 
$bg = imagecolorallocate($im, 22, 86, 165); //background color blue 
$fg = imagecolorallocate($im, 255, 255, 255);//text color white 
imagefill($im, 0, 0, $bg); 
imagestring($im, 5, 5, 5, $code, $fg); 
header("Cache-Control: no-cache, must-revalidate"); 
header('Content-type: image/png'); 
$captha = imagepng($im); 
imagedestroy($im); 

`

你可以準備一個JS代碼並將其放在onClick事件的提交按鈕上。然後你會檢查與該JS的驗證碼。

if($_SESSION['captcha'] == $("#form_captcha_text").val()){} 

你會繼續

else{ you will use here alert code.} 
0

剛剛更新莫希特耆那教的代碼

$(function() { 
    $("#XISubmit").click(function(){ 
    var photo= document.forms["XIForm"]["photo"].value; 
    var cap = '<?php echo $_SESSION["pass"]; ?>' // try this 
    if (photo==null || photo=="" || photo !=cap) { 
    alert("Please Enter captcha"); return false; 
    } 
    else{ 
    document.getElementById("XIForm").submit(); 
    } 
     }); 
+0

無效 – user3377635

+0

立即檢查。謝謝 –

+0

顯示錯誤未定義變量:_SESSION – user3377635