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

    Hi,

    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,
    Berk.
  2. Answer
    Princy
    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.

    ASPX:
    <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" />
    </telerik:RadCaptcha>
    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" />

    C#:
    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";
        }
        else
        {
            RadCaptcha1.CaptchaImage.ImageCssClass = "imgWrongCode";
        }
        Session["CaptchaCode"] = RadCaptcha1.CaptchaImage.Text;
    }

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