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

custom validation

1 Answer 23 Views
Captcha
This is a migrated thread and some comments may be shown as answers.
Lovella Bacaud
Top achievements
Rank 1
Lovella Bacaud asked on 19 Sep 2013, 07:57 AM
Hi telerik

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

Any help appreciated
Lovella

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 19 Sep 2013, 10:32 AM
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.
Tags
Captcha
Asked by
Lovella Bacaud
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Share this question
or