Click here to Skip to main content
15,888,351 members
Articles / Google
Tip/Trick

Google ReCaptcha 2.0 Client Side and Server Side Validation in ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
9 Aug 2015CPOL 16.2K   2   1
Google ReCaptcha 2.0 does not provide any straight forward validation process, rather they have provided HTML data attribute to tweak your validation and form submission process. Let's see how we can utilize those... :)

Introduction

Google ReCaptcha 2.0 Client Side and Server Side Validation. I am not sure if it's a standard solution but it's definitely working without any fallback.

Background

Google has introduced Google NoCaptcha ReCaptcha which works effectively on Google servers to validate a human and bots. There is no provision as of now to customize the validation process on client side, yet some external agencies have validation add-ons to meet those requirements. But still I am sharing one more simple way to achieve this task.

Using the Code

Integrate Google ReCaptcha as per Google instructed on their website. I am going to show only validation.

For Client Side

Create a textbox with display hidden. Add two html data attributes, viz. data-expired-callback and data-callback:

HTML
<div class="g-recaptcha" data-expired-callback="expiredReCaptcha" 
data-callback="verifyReCaptcha" lang="en" 
data-sitekey="XXXX-XXXX-XXX"> </div> 

<asp:TextBox ID="tbIsCaptchaChecked" 
style="display:none" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="rfvtbIsCaptchaChecked" 
CssClass="tfrm-error" runat="server" ErrorMessage="Please verify captcha" 
Display="Dynamic" ControlToValidate="tbIsCaptchaChecked" 
ValidationGroup="register"></asp:RequiredFieldValidator>

<asp:Label ID="isCaptchaChecked" CssClass="tfrm-error" 
runat="server" Text="Please verify captcha"></asp:Label>

Now define your JavaScript function for those two attributes, i.e., expiredReCaptcha and verifyReCaptcha.

JavaScript
function expiredReCaptcha() 
{
    //This method will execute only if Google ReCaptcha expired
    $("#tbIsCaptchaChecked").val(""); // making hidden textbox empty 
}
 function verifyReCaptcha() 
 { 
    //This method will execute only if Google ReCaptcha successfully validated
    $("#tbIsCaptchaChecked").val("Success"); // making a dummy entry to hidden textbox
    $("#rfvtbIsCaptchaChecked").hide(); // hiding asp.Net requiredFieldValidator
    $("#isCaptchaChecked").hide(); // hiding custom error message
 }

For server side validation (ASP.NET: C#):

C#
ReCaptcha reCaptcha = new ReCaptcha();
string response = Request["g-recaptcha-response"];
bool valid = reCaptcha.Validate(response);
    if (valid)
    { 

        //do your thing 

    }
    else 
    { 
        // validation failed , handle exception
        isCaptchaChecked.Visible = true;  // showing error message
    }

Hope this will help you. Feel free to share your comments.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Arab Emirates United Arab Emirates
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWell done!!! Pin
certocheno22-Nov-16 2:43
certocheno22-Nov-16 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.