Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Arithmetic Captcha in

Posted by Viral Sarvaiya on November 22, 2010

We know the captcha control, but now a day so many captcha code is available, most of the captcha is only for the text, but here I explain the new captcha that want human in intelligence as like arithmetic expression.
Create the new project,

Add a new file for create the captcha control named “captcha.aspx”.

In the page load of the captcha.aspx page.

protected void Page_Load(object sender, EventArgs e)

 void returnNumer()
 Random num1 = new Random();
 Random num2 = new Random();
 int numQ1, numQ2;
 string QString;
 numQ1 = num1.Next(10, 15);
 numQ2 = num1.Next(17, 31);
 QString = numQ1.ToString() + " + " + numQ2.ToString() + " = ";
 Session["answer"] = numQ1 + numQ2;

 Bitmap bitmap = new Bitmap(85,25);

 Graphics Grfx = Graphics.FromImage(bitmap);
 Font font = new Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel);
 Rectangle Rect = new Rectangle(100, 0, 0, 0);

 Grfx.FillRectangle(Brushes.Brown, Rect);
 Grfx.DrawRectangle(Pens.PeachPuff, Rect);
 // Border
 Grfx.DrawString(QString, font, Brushes.Azure, 0, 0);

 Response.ContentType = "Image/jpeg";
 bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);


Now in the default.aspx page take some control and 1 image control for the display captcha.

 <form id="form1" runat="server">
 Are You Human?  <asp:image ID="Image1" runat="server" ImageUrl="Capcha.aspx" />
 <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
 <br /><br />
 <asp:Button ID="Btnsubmit" runat="server" Text="Submit"  onclick="Btnsubmit_Click" /><br /><br />
 <asp:Label ID="lblValidate" runat="server"></asp:Label>

To check the input is valid or not, check the session value for the arithmetic expression.
So in default.aspx.cs file

protected void Page_Load(object sender, EventArgs e)

 protected void Btnsubmit_Click(object sender, EventArgs e)
 if (Session["answer"] != null)
 if (Session["answer"].ToString() == txtInput.Text)
 lblValidate.Text = "Current Answer";
 lblValidate.Text = "Incurrent Answer, Please input Valid Answer.";

Now run the page

inter the result and click the button

if result is currect then

and if answer id not currect then,

Hope this will help you…



6 Responses to “Arithmetic Captcha in”

  1. Great dear it is very useful in day to day working for projects …. excellent work … think one step ahead now ..create one custom control/ user control so you can just add in to your project and used it any time

  2. viralsarvaiya said


    Thanks for the valuable comment and advice.

  3. Good work. Only one problem. It is not safe to keep the answer in session. It is not encoded. At least encode it using some key or encryption method. But overall this is great.


  4. Persia said

    Very good, simple and efficient
    thanks alot

  5. Jayant said

    i read your articles.
    I’m quite new web developer and i need to develop a Contact Us form with Name, Phone, Email fields which will use JavaScript validation and Javascript Captcha(must be with image genearator) , Refresh button and Submit button, i tried many times , but couldn’t find so.
    Pls help me.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: