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 SinghPosted Mar 3, 2023, 9:57 AM
use this
Tuhin PaulPosted Mar 3, 2023, 10:43 AM
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:
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.
The code assumes that the label control lblErrorMessage is already present on the page and is used to display the error message.
Naimish MakwanaPosted Mar 3, 2023, 9:40 AM
Have you refered this link?
https://www.c-sharpcorner.com/UploadFile/sourabh_mishra1/captcha-image-in-Asp-Net/
Garima BansalPosted Mar 3, 2023, 9:36 AM
Naimish Makwana
None of your answer works for me
Naimish MakwanaPosted Mar 3, 2023, 9:27 AM
You can also use the
ValidateCaptchamethod provided by theCaptchaControlto validate the captcha textThanks
Naimish MakwanaPosted Mar 3, 2023, 9:25 AM
Please check below link:
https://www.c-sharpcorner.com/UploadFile/sourabh_mishra1/captcha-image-in-Asp-Net/
Garima BansalPosted Mar 3, 2023, 9:18 AM
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 MakwanaPosted Mar 3, 2023, 9:08 AM
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.Thanks
Naimish Makwana