如何使用javascript / jQuery验证Google reCAPTCHA v2?

我在aspx中有一个简单的联系表。我想在提交表单之前验证reCaptcha(客户端)。请帮忙。


样例代码:


    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

        <title>Test Form</title>

        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">

        <script src="//code.jquery.com/jquery-1.10.2.js"></script>

        <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

        <link rel="stylesheet" href="/resources/demos/style.css">

        <script src="https://www.google.com/recaptcha/api.js" async defer></script>

        <script>

            $("#cmdSubmit").click(function () {

                //need to validate the captcha

            });

        </script> 

    </head>

    <body>

        <form id="form1" runat="server">

            <label class="clsLabe">First Name<sup>*</sup></label><br />

            <input type="text" id="txtFName" name="txtFName" class="clsInput" /><br />

            <div class="g-recaptcha" data-sitekey="my_key"></div>

            <img id="cmdSubmit" src="SubmitBtn.png" alt="Submit Form" style="cursor:pointer;" />

        </form>

    </body>

    </html>

我想cmdSubmit点击验证验证码。


胡子哥哥
浏览 914回答 3
3回答

慕桂英3389331

reCaptcha的客户端验证-以下对我有用:如果未在客户端对reCaptcha进行验证,则“ grecaptcha.getResponse();”将返回null,否则返回非null的值。JavaScript代码:var response = grecaptcha.getResponse();if(response.length == 0)&nbsp; &nbsp; //reCaptcha not verifiedelse&nbsp; &nbsp; //reCaptch verified

蓝山帝景

使用此工具可通过简单的javascript验证Google验证码。html主体中的这段代码:<div class="g-recaptcha" id="rcaptcha" style="margin-left: 90px;" data-sitekey="my_key"></div><span id="captcha" style="margin-left:100px;color:red" />这段代码放在调用get_action(this)方法表单按钮的开头:function get_action(form)&nbsp;{&nbsp; &nbsp; var v = grecaptcha.getResponse();&nbsp; &nbsp; if(v.length == 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('captcha').innerHTML="You can't leave Captcha Code empty";&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('captcha').innerHTML="Captcha completed";&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP