2014-03-31 195 views
0

我有一個應用程序表單,我需要使用jquery驗證驗證碼。如果輸入的驗證碼不正確,那麼顯示的提示框請再次輸入驗證碼輸入的驗證碼是錯誤的。我怎樣才能使用jquery?請幫幫我。我是jquery的新手。使用jquery驗證驗證碼

下面是代碼:從https://code.google.com/p/recaptcha4j/downloads/list

<%@ page import="net.tanesha.recaptcha.ReCaptcha" %> 
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %> 

<form action="." method="post"> 
    <div class="loginbox"> 
      <c:set var="PUBLIC_KEY" scope="page" value="${requestScope.properties.get('PUBLIC_KEY')}" /> 
      <c:set var="PRIVATE_KEY" scope="page" value="${requestScope.properties.get('PRIVATE_KEY')}" /> 

      <% ReCaptcha c = ReCaptchaFactory.newSecureReCaptcha((String)pageContext.getAttribute("PUBLIC_KEY"), (String)pageContext.getAttribute("PRIVATE_KEY"), false); 
       out.print(c.createRecaptchaHtml("errorMessage","blackglass", 2)); %>       
      <button type="submit">Submit</button>   
    </div> 
</form> 

$(function() { 
    $("#XISubmit").click(function(){ 
     event.preventDefault() 
     val=$('#vercode').val() 
     $.ajax({ 
      type:"post", 
      url:"verify-captcha.php", 
      data:{'code':val}, 
      success:function(data){ 
       if(data=='false'){ 
        alert('Re-enter Captcha, You Entered wrong captcha code') 
        $("#cap").html("<img src='captcha_image.php'>"); } 
       else{ 
        document.getElementById("XIForm").submit(); 
       } 
      } 

     });  
    }); 
}); 

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

       }); 
Html page : 

<label>Security Validation</label>  

<span><img src="captcha.php"></span><input type="text" name="vercode" id="vercode" size="10" style="margin-top: -1px; float: left; width: 115px; margin-right: 12px;"></li><div id="msg"></div> 
captcha.php 

<?php 
session_start(); 
$ranStr = md5(microtime()); 
$ranStr = substr($ranStr, 0, 6); 
$_SESSION['cap_code'] = $ranStr; 
$newImage = imagecreatefromjpeg("cap_bg.jpg"); 
$txtColor = imagecolorallocate($newImage, 0, 0, 0); 
imagestring($newImage, 5, 5, 5, $ranStr, $txtColor); 
header("Content-type: image/jpeg"); 
imagejpeg($newImage); 
?> 
verify-captcha.php 

<?php 
session_start(); 
if ($_POST["code"] != $_SESSION["cap_code"] || $_SESSION["cap_code"]=='') { 
echo 'false'; 
}else{ 
echo 'true';} 
+1

它使用jQuery,你正在嘗試做的,因爲那麼就可以繞過所以這將是無用的不做......此驗證必須通過目標資源進行 –

+0

現在發生了什麼 –

+0

@ArunPJohny,你的意思是,可以修改JS文件嗎? –

回答

0

下載recaptcha.jar保持公鑰和私鑰在* .properties文件(如果沒有創建一個),並使用$ {requestScope.properties.get('PUBLIC_KEY')}獲取PUBLIC和PRIVATE KEY。

現在ajax以下面的參數調用2。並驗證後端的recaptchaResponseField和recaptchaChallengeField。

recaptchaResponseField = $(「input [name = recaptcha_response_field]」)。val(); ()輸入[name = recaptcha_challenge_field]「)。val();

var ajax=$.ajax({ 
      url: API.get_api_url('your_auth_url'), 
      data: {recaptchaResponseField:recaptchaResponseField,recaptchaChallengeField:recaptchaChallengeField}, 
      type: 'POST', 
      success: function(data, status, xhr){ 
       if(data){ 
        if(data.statusCode == 0){ 
         log("if successfully authenticated.. send to home page"); 
        } 
       } 
      } 
     }); 

在java(後端)。使用下面來驗證再驗證碼

public boolean isValidCapctha(HttpServletRequest request, String recaptchaResponseField, String recaptchaChallengeField) { 
     String remoteAddr = request.getRemoteAddr(); 
     ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); 
     reCaptcha.setPrivateKey(properties.getProperty("PRIVATE_KEY")); 
     ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, recaptchaChallengeField, recaptchaResponseField); 
     return reCaptchaResponse.isValid();  
    }