2013-09-29 48 views
2

我有一個評論表單,它在提交時向數據庫插入數據。以下是代碼;使用jQuery與reCAPTCHA驗證輸入表格

function reloadRecaptcha() { 
    var publicKey = "*************************************"; 
    var div = "recap"; 
    Recaptcha.create(publicKey,div,{theme: "white"}); 
    return false; 
} 

function validateForm() { 
    var x=document.forms["cmnts"]["name"].value; 
    if (x==null || x=="") { 
     jAlert('Please enter your name', 'Error'); 
     return false; 
    } 

    var x=document.forms["cmnts"]["email"].value; 
    var atpos=x.indexOf("@"); 
    var dotpos=x.lastIndexOf("."); 
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { 
     jAlert('Please enter a valid email address', 'Error'); 
     return false; 
    } 

    var x=document.forms["cmnts"]["comment"].value; 
    if (x==null || x=="") { 
     jAlert('Please enter a comment', 'Error'); 
     return false; 
    } 

    var challenge = Recaptcha.get_challenge(); 
    var response = Recaptcha.get_response(); 
    $.ajax({ 
     type: "POST", 
     url: "includes/valrecaptcha.php", 
     async: false, 
     data: { 
      challenge: challenge, 
      response: response 
     }, 
     success: function(resp) { 
      if(resp == "false") { 
       jAlert('Please enter captcha words correctly', 'Error'); 
       reloadRecaptcha(); 
      } 
     } 
    }); 
} 

一切(如表單驗證工作除了當我打的submit按鈕精緻,發佈評論無論驗證碼是否正確,頁面開始導航時,我看到警告信息權利之前。我。「M使用jAlert顯示警報消息以下是形式;

<h4>Leave your comment</h4> 
<form action="blog?post=".$_GET["post"]."#comments" onsubmit="return validateForm();" name="cmnts" method="post"> 
<div class="form_row"> 
    <label>Name</label><br /> 
    <input type="text" class="tbox" name="name" title="Type your name"/> 
</div> 
<div class="form_row"> 
    <label>Email (not visible to others)</label><br /> 
    <input type="text" class="tbox" name="email" title="Type your email" /> 
</div> 
<div class="form_row"> 
    <label>Comment</label><br /> 
    <textarea name="comment" class="tbox" rows="6" title="Type your comment" ></textarea> 
    <p>You may use following HTML tags and attributes: &lt;b&gt; &lt;cite&gt; &lt;del&gt; &lt;i&gt; &lt;u&gt;</p> 
</div> 
<div class="form_row" style="height:80px;"> 
    <label>Captcha</label><br /> 
    <div id="recap"></div> 
    <p>I must make sure that you're <i>not</i> a spammer or a bot</p> 
    <div style="clear:both;">   
</div> 
<input value="Comment" id="submit" name="submit" class="submit_btn float_l" type="submit"> 
</form> 

<body>標籤有一個onload事件return reloadRecaptcha();

那麼,爲什麼不形式獲得子之前驗證reCAPTCHA?

回答

4

發生這種情況是因爲validateForm()不會從ajax調用中返回任何東西。你應該有一個像isCaptchaValidated一個變量,並設置了Ajax的成功()裏面,然後阿賈克斯像下面後返回變量:

var isCaptchaValidated = false; 
$.ajax({ 
    type: "POST", 
    url: "includes/valrecaptcha.php", 
    async: false, 
    data: { 
     challenge: challenge, 
     response: response 
    }, 
    success: function(resp) { 
     if(resp == "false") { 
      jAlert('Please enter captcha words correctly', 'Error'); 
      reloadRecaptcha(); 
     } else { 
      isCaptchaValidated = true; 
     } 
    } 
}); 
return isCaptchaValidated; 

順便說一句,AJAX表示異步JavaScript和XML,所以我會違反設置異步:false。

+0

謝謝你!有效!! – Sid

+0

偉大的工作! – Rajesh

+0

@Rajesh:如果您使用的是異步方法,則可以在成功方法中設置值之前返回isCaptchaValidated。 – Terry