Captcha enter button

2 posts, 0 answers
  1. Mike Conine
    Mike Conine avatar
    12 posts
    Member since:
    Aug 2004

    Posted 27 Jun 2009 Link to this post

    Hey Guys;

    Great start on the Captcha function.

    What is the best recommendation for hooking the Captcha submission to a keyboard enter click, rather than verify code?
  2. Pero
    Admin
    Pero avatar
    1156 posts

    Posted 01 Jul 2009 Link to this post

    Hi Mike,

    Because, the RadCaptcha validates on a post back, the logical way to set the RadCaptcha validate on Enter key pressed, is to set the Enter key cause post back when it is clicked. One way to do this is by attaching an event handler to the "onkeypress" event of the RadCaptcha TextBox (the "onkeypress" event is attached dynamically to the TextBox), that will submit the form when Enter is pressed. The assumption here is that the user inputs the code as a last stage before the form is submitted. I have implemented a sample project that demonstrates the described scenario. Here is the full source code:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
     
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    <!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
        <style type="text/css"
            .label 
            { 
                color: Green; 
                background-color: Red; 
            } 
        </style> 
    </head> 
    <body onload="bodyOnLoad()"
        <form id="form1" runat="server" method="post"
        <asp:ScriptManager ID="ScriptManager1" runat="server"
        </asp:ScriptManager> 
        <div> 
            <telerik:RadCaptcha ID="RadCaptcha1" runat="server" ErrorMessage="RadCaptcha Error Message" ProtectionMode="Captcha"
            </telerik:RadCaptcha> 
            <br /> 
            <br /> 
        </div> 
     
        <script type="text/javascript"
            //get the client object of the RadCapthca TextBox 
            var captchaTextBox = document.getElementById('RadCaptcha1_CaptchaTextBox'); 
             
            function bodyOnLoad() 
            { 
                //attach an event to the textbox 
                var e = (window.event)? window.event : event; 
                captchaTextBox.attachEvent('onkeypress', function(e) { checkKeyPress(e); }); 
            } 
     
            function checkKeyPress(e) 
            { 
                var keypressed = null
                if (e.keyCode)  
                { 
                    keypressed = e.keyCode; 
                } 
                else  
                { 
                    keypressed = e.which; 
                } 
                 
                //if Enter is pressed submit the form 
                if (keypressed === 13) 
                { 
                    document.forms[0].submit(); 
                } 
            }    
        </script> 
     
        </form> 
    </body> 
    </html> 



    Regards,
    Pero
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top