2015-08-26 29 views
4

IM,我想驗證如果選中與否,已經被填充或不和IM的方式做它是通過這個代碼(參見下圖)檢查谷歌驗證碼進行檢查或使用谷歌驗證碼</p> <pre><code><label for="template-contactform-botcheck">Botcheck</label> <div id="recaptcha_sme"></div> </code></pre> <p>已經充滿

if ($("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) { 
    alert("the captcha has been filled"); 
} else { 
    alert("Please fill the captcha!"); 
} 

,我不打算走線槽服務器端驗證(驗證碼谷歌API檢查),我只是想通知(使用警報提示),如果谷歌的驗證碼進行檢查或填充。任何想法,線索,建議,建議,使之成爲可能?

下面是我的完整表單提交腳本代碼(在jquery中提交函數)。

$("#sme_application_dialog form").submit(function(e){ 
    if ($(this).find("#recaptcha_sme #recaptcha-anchor").hasClass("recaptcha-checkbox-checked")) { 
     alert("the captcha has been filled"); 
    } else { 
     alert("Please fill the captcha!"); 
    } 
    e.preventDefault(); 


}); 

,你可以從上面看到的參考,我只是想檢查是否有「驗證碼錨」的ID的元素有一個類「驗證碼,複選框,選中」的(因爲我可以從使用Chrome dom瀏覽器的DOM結構中看到,那個類已添加到具有「recaptcha-anchor」標識的元素,所以我認爲這可能工作但可悲但不幸,它不會工作

PS:假設我有div的id爲「sme_application_dialog_form」,裏面有一個from和裏面的表單,還有google recaptcha,我有「$(document).ready」。假定一切都設置和工作(除了爲驗證我在檢查google recaptcha已檢查或已被填充)

+2

文檔https://developers.google.com/recaptcha/docs/display#js_api - 'grecaptcha.getResponse( opt_widget_id )' – mplungjan

+0

看一看這個帖子:http://stackoverflow.com/questions/ 29612879/google-recaptcha-how-to-make-required/29613089#29613089 – colecmc

+0

[如何檢查js用戶選中了Google recaptcha中的複選框?](http://stackoverflow.com/questions/28674946 /如何對簽入JS-是,用戶已覈對最複選框功能於谷歌,驗證碼) –

回答

1

您可以驗證它回調函數通過獲取響應值爲null或不。

回調函數

function submit(){ 
    var response = grecaptcha`.`getResponse(); 
    if(response != '0'){ 
      //captcha validated and got response code 
      alert("the captcha has been filled"); 
    }else{ 
      //not validated or not clicked 
      alert("Please fill the captcha!"); 
    } 
} 

它將工作。

2

添加data-callbackReCapthca DIV:

<div class="g-recaptcha" data-sitekey="***" data-callback="recaptchaCallback"></div> 

添加此功能,你的代碼。

var recaptchachecked; 
function recaptchaCallback() { 
    //If we managed to get into this function it means that the user checked the checkbox. 
    recaptchachecked = true; 
} 

您現在可以在您的OnSubmit()函數上使用recaptchachecked。

相關問題