Click here to Skip to main content
15,884,995 members
Articles / Web Development / ASP.NET

Custom reCaptcha Validation

Rate me:
Please Sign up or sign in to vote.
4.93/5 (22 votes)
15 Nov 2013CPOL2 min read 89.4K   3K   59  
How to manually integrate reCaptcha into your site
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <!-- recaptcha client library -->
    <script type="text/javascript" src="http://api.recaptcha.net/js/recaptcha_ajax.js"></script> 

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
<% 
    // don't show another captcha after success
    if (!Page.IsPostBack || (Page.IsPostBack && !Page.IsValid)) { 
        
        // client-side captcha creation  (note the server-side injection of the public key and the page culture)
        %>
        <div id="recaptcha_div"></div>
        <script type='text/javascript'>
            Recaptcha.create("<%=Config.PublicKey %>",
                "recaptcha_div", {
                    lang: "<%=LanguageCode %>",
                    theme: "clean",
                    callback: Recaptcha.focus_response_field
               });
        </script>
        
        <asp:Button runat="server" ID="submitBtn" Text="Submit" OnClick="OnSubmit" />
<% } %>
        <asp:CustomValidator runat="server" ID="recaptchaValidator" OnServerValidate="OnRecaptchaValidate" Text="Incorrect" />
        
        <asp:Label runat="server" ID="Outcome" />
    </div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
United Kingdom United Kingdom
I've been a web application developer since the late 1990s, working for eCommerce, media and telecoms companies across europe and america.

Comments and Discussions