I am working on Validate Captcha
1: if user enter incorrect captcha then its shows error message "incorrect", How do i implement
CaptchaHeight="60" CaptchaWidth="200"
CaptchaLineNoise="None" CaptchaMinTimeout="5"
CaptchaMaxTimeout="240" FontColor="#529E00" />
protected void btnLogin_Click(object sender, EventArgs e)
{
var db = new DatabaseContext();
var ab= db.login.Where(x => x.MobileNumber == txtMobileNumber.Text.ToString()).FirstOrDefault();
if (applicant == null)
{
lblMessage.Text = "Mobile number is not registered!";
return;
}
if (btnLogin.Text == "Get OTP")
{
Session["LoginOtp"] = "*****";
pnl_Otp.Visible = true;
txtMobileNumber.Enabled = false;
btnLogin.Text = "Submit";
txt_Otp.Focus();
}
else
{
if (Session["LoginOtp"].ToString() != txt_Otp.Text)
{
lblMessage.Text = "Invalid OTP";
return;
}
else
{
if (!ab.IsComplete)
{
Session["Username"] = applicant.MobileNumber;
Session["Id"] = applicant.Id;
Response.Redirect("~/index/default);
}
else
{
Session["Username"] = applicant.MobileNumber;
Session["Id"] = applicant.Id;
Session["usertype"] = "0";
Session["Type"] = applicant.Type;
Response.Redirect("~/Type/Default");
}
}
}

Sachin SinghRank #66 · 26.2K reputationPosted Mar 3, 2023, 4:57 AM EST
use this
protected void btnLogin_Click(object sender, EventArgs e) { cptCaptcha.ValidateCaptcha(txtCaptcha.Text.Trim()) if (!cptCaptcha.UserValidated) { lblMessage.Text = "Incorrect captcha"; pnl_Otp.Visible = false; }else{ var db = new DatabaseContext(); var ab= db.login.Where(x => x.MobileNumber == txtMobileNumber.Text.ToString()).FirstOrDefault(); if (applicant == null) { lblMessage.Text = "Mobile number is not registered!"; return; } if (btnLogin.Text == "Get OTP") { Session["LoginOtp"] = "*****"; pnl_Otp.Visible = true; txtMobileNumber.Enabled = false; btnLogin.Text = "Submit"; txt_Otp.Focus(); } else { if (Session["LoginOtp"].ToString() != txt_Otp.Text) { lblMessage.Text = "Invalid OTP"; return; } else { if (!ab.IsComplete) { Session["Username"] = applicant.MobileNumber; Session["Id"] = applicant.Id; Response.Redirect("~/index/default); } else { Session["Username"] = applicant.MobileNumber; Session["Id"] = applicant.Id; Session["usertype"] = "0"; Session["Type"] = applicant.Type; Response.Redirect("~/Type/Default"); } } } }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 3, 2023, 5:43 AM EST
I will go with Sachin's answer and try to explain a bit in detials:
I think you should modify your existing code:
1. Add a comparison between the user-entered captcha value and the captcha control's value:
if (txtCaptcha.Text != cptCaptcha.Text) { lblErrorMessage.Text = "Incorrect captcha"; return; }2. This code will check if the user-entered captcha value (txtCaptcha.Text) is equal to the captcha value generated by the captcha control (cptCaptcha.Text). If the values do not match, the error message "Incorrect captcha" will be displayed in the label control lblErrorMessage.
protected void btnLogin_Click(object sender, EventArgs e) { var db = new DatabaseContext(); var ab = db.login.Where(x => x.MobileNumber == txtMobileNumber.Text.ToString()).FirstOrDefault(); if (applicant == null) { lblMessage.Text = "Mobile number is not registered!"; return; } if (btnLogin.Text == "Get OTP") { Session["LoginOtp"] = "*****"; pnl_Otp.Visible = true; txtMobileNumber.Enabled = false; btnLogin.Text = "Submit"; txt_Otp.Focus(); } else { // Captcha validation if (txtCaptcha.Text != cptCaptcha.Text) { lblErrorMessage.Text = "Incorrect captcha"; return; } if (Session["LoginOtp"].ToString() != txt_Otp.Text) { lblMessage.Text = "Invalid OTP"; return; } else { if (!ab.IsComplete) { Session["Username"] = applicant.MobileNumber; Session["Id"] = applicant.Id; Response.Redirect("~/index/default"); } else { Session["Username"] = applicant.MobileNumber; Session["Id"] = applicant.Id; Session["usertype"] = "0"; Session["Type"] = applicant.Type; Response.Redirect("~/Type/Default"); } } } }The code assumes that the label control lblErrorMessage is already present on the page and is used to display the error message.
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 3, 2023, 4:08 AM EST
Hello Garima,
To validate the captcha, you can compare the user's input in the
txtCaptchatextbox with the captcha text generated by theCaptchaControl. You can add condition in btnLogin_Click Click.// Validate the captcha if (cptCaptcha.UserInput.ToLower() != txtCaptcha.Text.ToLower()) { lblErrorMessage.Text = "Incorrect captcha"; return; }Thanks
Naimish Makwana
Garima BansalRank #1,555 · 1K reputationPosted Mar 3, 2023, 4:18 AM EST
Hello
Naimish Makwana ,
implement this way but i enter correct captcha still shows "Incorrect Captcha"
var db = new DatabaseContext();
var ab= db.Logins.Where(x => x.MobileNumber == txtMobileNumber.Text.ToString()).FirstOrDefault();
if (applicant == null)
{
lblMessage.Text = "Mobile number is not registered!";
return;
}
if (cptCaptcha.UserValidated.ToString() != txtCaptcha.Text.ToLower())
{
lblErrorMessage.Text = "Incorrect captcha";
return;
}
if (btnLogin.Text == "Get OTP")
{
Session["LoginOtp"] = "12121212";
pnl_Otp.Visible = true;
txtMobileNumber.Enabled = false;
btnLogin.Text = "Submit";
txt_Otp.Focus();
}
else
{
if (Session["LoginOtp"].ToString() != txt_Otp.Text)
{
lblMessage.Text = "Invalid OTP";
return;
}
else
{
if (!ab.IsComplete)
{
Session["Username"] = ab.MobileNumber;
Session["Id"] = applicant.Id;
Response.Redirect("~/index");
}
else
{
Session["Username"] = applicant.MobileNumber;
Session["Id"] = applicant.Id;
Session["usertype"] = "0";
Session["ApplicantType"] = applicant.ApplicantType;
Response.Redirect("~/Type/Default");
}
}
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 3, 2023, 4:25 AM EST
Please check below link:
https://www.c-sharpcorner.com/UploadFile/sourabh_mishra1/captcha-image-in-Asp-Net/
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 3, 2023, 4:27 AM EST
You can also use the
ValidateCaptchamethod provided by theCaptchaControlto validate the captcha textif (!cptCaptcha.ValidateCaptcha(txtCaptcha.Text.Trim())) { lblErrorMessage.Text = "Incorrect captcha"; return; }Thanks
Garima BansalRank #1,555 · 1K reputationPosted Mar 3, 2023, 4:36 AM EST
Naimish Makwana
None of your answer works for me
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 3, 2023, 4:40 AM EST
Have you refered this link?
https://www.c-sharpcorner.com/UploadFile/sourabh_mishra1/captcha-image-in-Asp-Net/