Custom Mathematical CAPTCHA in asp .net

In this asp .net tutorial we will learn how to create custom mathematical numeric CAPTCHA with arithmetic operations inside our comment module. This numeric CAPTCHA will generate two random integer numbers and users have to add those numbers to solve the CAPTCHA. This CAPTCHA will helps us to stopping spam comments in our website.

 

Step1: Create a new asp .net website.

 

Step2: Create a new page in your asp .net website.

 

Step3: Pate following code in your default.aspx page.

 

 

Give your valuable comments.

Name
Email
Comment
 =
 

 

 

Step3: Paste following code in your default.aspx.cs page.

 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //===== Add text to stop spammer.
            generateStopSpamText();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtStopSpam.Text == ViewState["spam"].ToString())
        {
            //---- Do your operations here.
            //--- I have shown confirmation message.
            CustomValidator val = new CustomValidator();
            val.ValidationGroup = "com";
            val.IsValid = false;
            val.ErrorMessage = "Congratulations you have successfully solved the CAPTCHA.";
            this.Page.Validators.Add(val);

            //==== Create new spam protection code.
            generateStopSpamText();
        }
        else
        {
            CustomValidator val = new CustomValidator();
            val.ValidationGroup = "com";
            val.IsValid = false;
            val.CssClass = "valFailure";
            val.ErrorMessage = "You have entered invalid captcha code. Please retry.";
            this.Page.Validators.Add(val);

            //---- Generate new captcha code.
            generateStopSpamText();
        }
    }

    private void generateStopSpamText()
    {
        Random ran = new Random();
        //--- Here I have used numbers between 1 to 9 you can increase as per your req.
        int firstNumber = ran.Next(1, 9);
        int secondNumber = ran.Next(1, 9);
        ViewState["spam"] = firstNumber + secondNumber;
        lblStopSpam.Text = firstNumber.ToString() + " + " + secondNumber.ToString();
    }

 

 

Final output:


Custom Mathematical CAPTCHA in asp .net

 

We are nothing without our users ! You can help us offer even more high quality content. Please share our page !
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
4 + 3 =
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2019 CodingFusion
75 Important SQL Server queries you should know