This is a migrated thread and some comments may be shown as answers.

Captcha enter button

1 Answer 181 Views
Captcha
This is a migrated thread and some comments may be shown as answers.
Mike Conine
Top achievements
Rank 1
Mike Conine asked on 27 Jun 2009, 02:24 PM
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?

1 Answer, 1 is accepted

Sort by
0
Pero
Telerik team
answered on 01 Jul 2009, 03:11 PM
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.
Tags
Captcha
Asked by
Mike Conine
Top achievements
Rank 1
Answers by
Pero
Telerik team
Share this question
or