custom validation

2 posts, 0 answers
  1. Lovella Bacaud
    Lovella Bacaud avatar
    20 posts
    Member since:
    Apr 2013

    Posted 19 Sep 2013 Link to this post

    Hi telerik

    Can I perform mathematical validation using radcaptcha? Ex. 3+2=? like this.

    Any help appreciated
    Lovella
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Sep 2013 Link to this post

    Hi Lovella,

    RadCaptcha supports custom validation. Please have a look at the sample code I tried which works fine at my end.

    ASPX:
    <telerik:RadCaptcha ID="RadCaptcha1" runat="server" CaptchaImage-TextChars="Numbers"
        ValidationGroup="CaptchaValidation" CaptchaImage-RenderImageOnly="true" CaptchaImage-TextLength="3"
        ErrorMessage="Wrong" CaptchaTextBoxLabel="Enter Sum of Digits" OnCaptchaValidate="RadCaptcha1_CaptchaValidate">
    </telerik:RadCaptcha>
    <telerik:RadTextBox ID="RadTextBox1" runat="server">
    </telerik:RadTextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="CaptchaValidation"
        ControlToValidate="RadTextBox1" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
    <br />
    <telerik:RadButton ID="RadButton1" runat="server" ValidationGroup="CaptchaValidation"
        Text="Click">
    </telerik:RadButton>

    C#:
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["CaptchaCode"] = RadCaptcha1.CaptchaImage.Text;
        }
    }
    protected void RadCaptcha1_CaptchaValidate(object sender, CaptchaValidateEventArgs e)
    {
        e.CancelDefaultValidation = true;
        string captcha = Session["CaptchaCode"].ToString();
        int digitsSum = 0;
        foreach (char c in captcha)
        {
            digitsSum += Convert.ToInt32(c.ToString());
        }
        int enteredsum = Convert.ToInt32(RadTextBox1.Text);
        if (digitsSum == enteredsum)
        {
            e.IsValid = true;
        }
        else
        {
            e.IsValid = false;
        }
        Session["CaptchaCode"] = RadCaptcha1.CaptchaImage.Text;
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top