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

Call javascript function after validation

1 Answer 254 Views
Captcha
This is a migrated thread and some comments may be shown as answers.
Anders
Top achievements
Rank 1
Anders asked on 05 Mar 2010, 10:44 AM
Hi, is there a good way to call a javascript method after the Captcha check is validated?

Im trying to add the captcha to a blog comment module. The module uses jquery to post the comment.

Thx.
Anders

1 Answer, 1 is accepted

Sort by
0
Pero
Telerik team
answered on 09 Mar 2010, 09:12 AM
Hello Anders,

The easiest way to call a JavaScript function after the Captcha has been validated correctly is to register a script from the server. The source code pasted below renders a script from the server, that attaches an event handler to the "load" (MS AJAX Framework) specific event.

.aspx
<%@ 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">
<head id="Head1" runat="server">
    <title></title>
 
    <script type="text/javascript">
        function TestMethod()
        {
            alert("Captcha Valid!");
            Sys.Application.remove_load(TestMethod);
        }      
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
        </Scripts>
    </asp:ScriptManager>
    <div>
        <telerik:RadCaptcha ID="RadCaptcha1" runat="server" ErrorMessage="Wrong Input" Width="386px">
        </telerik:RadCaptcha>
        <asp:Button ID="Button1" runat="server" Text="Validate Input" CausesValidation="true"
            OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Captcha_CallJS_afterPostback : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Validate", "Sys.Application.add_load(TestMethod);", true);
        }
    }
}



Kind regards,
Pero
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Captcha
Asked by
Anders
Top achievements
Rank 1
Answers by
Pero
Telerik team
Share this question
or