Validate Captcha by arithmetic operation.

2 posts, 1 answers
  1. Berkman
    Berkman avatar
    23 posts
    Member since:
    Feb 2013

    Posted 08 Mar 2013 Link to this post


    I have the following scenario to be achieved. I want to display a captcha in numeric form and want the user to perform an arithmetic calculation and enter the result in captcha textbox and hence perform a validation. Is this possible using RadCaptcha?

    Please help,
  2. Answer
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Mar 2013 Link to this post

    Hello Berk,

    Yes, it is possible to achieve your scenario. Please have a look at the following code in which captcha validation is done based on the sum of numbers.

    <telerik:RadCaptcha ID="RadCaptcha1" runat="server" ValidationGroup="CaptchaValidation" ErrorMessage="Page not valid. The code you entered is not valid."
        CaptchaTextBoxCssClass="textBox" OnCaptchaValidate="RadCaptcha1_CaptchaValidate">
        <CaptchaImage ImageCssClass="imageClass" BackgroundColor="#dff3ff" TextColor="Black"
            RenderImageOnly="true" />
    Enter the sum of the digits in the validation code <br />(enter 0 if there are not any):<br />
    <asp:TextBox ID="ValdiationCodeDigitsSum" runat="server" />
    <asp:Button ID="btnValidate" runat="server" Text="Verify Code" ValidationGroup="CaptchaValidation" />

    private string oldCaptchaCode;
    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 validationCodeNumbers = Regex.Replace(Session["CaptchaCode"].ToString(), "[^0-9]", "");
        int digitsSum = 0;
        foreach (char c in validationCodeNumbers)
            digitsSum += Convert.ToInt32(c.ToString());
        int enteredDigitsSum = 0;
        e.IsValid = Int32.TryParse(ValdiationCodeDigitsSum.Text, out enteredDigitsSum) && digitsSum == enteredDigitsSum;
        if (e.IsValid)
            RadCaptcha1.CaptchaImage.ImageCssClass = "imgCorrectCode";
            RadCaptcha1.CaptchaImage.ImageCssClass = "imgWrongCode";
        Session["CaptchaCode"] = RadCaptcha1.CaptchaImage.Text;

  3. DevCraft R3 2016 release webinar banner
Back to Top