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

Validate Captcha by arithmetic operation.

1 Answer 140 Views
Captcha
This is a migrated thread and some comments may be shown as answers.
Berkman
Top achievements
Rank 1
Berkman asked on 08 Mar 2013, 07:38 AM
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.

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 08 Mar 2013, 08:21 AM
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.
Tags
Captcha
Asked by
Berkman
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or