如何使用 javascript/jQuery 验证 google reCAPTCHA v2? [英] How can I validate google reCAPTCHA v2 using javascript/jQuery?

查看:37
本文介绍了如何使用 javascript/jQuery 验证 google reCAPTCHA v2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple contact form in aspx. I want to validate the reCaptcha (client-side) before submitting the form. Please help.

示例代码:

    <%@ 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 单击时验证验证码.

I want to validate the captcha on cmdSubmit click.

请帮忙.

推荐答案

这个 reCaptcha 的客户端验证 - 以下对我有用:

This Client side verification of reCaptcha - the following worked for me :

如果客户端未验证 reCaptcha grecaptcha.getResponse(); 返回 null,否则返回 null 以外的值.

if reCaptcha is not validated on client side grecaptcha.getResponse(); returns null, else is returns a value other than null.

Javascript 代码:

var response = grecaptcha.getResponse();

if(response.length == 0)
    //reCaptcha not verified

else
    //reCaptch verified

这篇关于如何使用 javascript/jQuery 验证 google reCAPTCHA v2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆