$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
Captcha Server Side Event in ASP.NET
Home > How to > Captcha Server Event

Simple Information of CaptchaValidate Control

Generally speaking, the CaptchaValidate event of KT.UI for ASP.NET AJAX occurrs and starts berfore the control is validated. This event allows users to implement server-side code on PerCaptcha validation with advanced PerCaptcha component. Users are even able to cancel the default validation system of the captcha control and to specify and define your own logic for validating the PerCaptcha.
PerCaptchaValidate serve event owns the following properties and benefits when it is fired within ASP.NET application:
  • Support for CancelDefaultValidation - this property is used to specify whether the default validation of PerCaptcha control will be applied after the event is fired.
  • IsValid – this property is used to determine whether the PerCaptcha validation is successful or not and should be used if CancelDefaultValidation is set to true.

How to Set CaptchaValidate

Example ASPX and C# codes below is used to demonstrate PerCaptcha that is validated if the sum of the digits, displayed in the code, is entered in the input. In order to reach this goal, users need to set the property CancelDefaultValidation as true and the new validation logic is implemented in the CaptchaValidate event handler. And finally, IsValid is set according to the result of the validation.
Please refer following ASPX demo codes to set and display captcha on web page.

kettic:PerCaptcha ID="PerCaptcha1" runat="server" ValidationGroup="Group" ErrorMessage="Page not valid. The code you entered is not valid."
CaptchaTextBoxCssClass="textBox" OnCaptchaValidate="PerCaptcha1_CaptchaValidate">
<CaptchaImage ImageCssClass="imageClass" BackgroundColor="#dff3ff" TextColor="Black"
RenderImageOnly="true"></CaptchaImage>
</kettic:PerCaptcha>
Sample C# codes are for setting CaptchaValidate based on above two properties within ASP.NET C# project.

protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["CaptchaCode"] = PerCaptcha1.CaptchaImage.Text;
}
}

protected void PerCaptcha1_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)
{
PerCaptcha1.CaptchaImage.ImageCssClass = "imgCorrectCode";
}
else
{
PerCaptcha1.CaptchaImage.ImageCssClass = "imgWrongCode";
}
Session["CaptchaCode"] = PerCaptcha1.CaptchaImage.Text;
}
If users input the wrong sum digits, the read error message will occur promptly. Displayed below is a sample screenshot for inputting wrong sum value.

/how-to/aspnet-ajax/controls-captcha/controls-captcha-server-event.jpg

Find Other Related Online Tutorials

This page mainly focuses on CaptchaValidate control and how to set its two relevant properties by using KT.UI for ASP.NET Captcha Controls within ASP.NET project, for further operations on captcha component, users can get more guides via following recommended tutorials.
ASP.NET AJAX UI Controls