Click here to Skip to main content
13,352,631 members (34,773 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 18 Nov 2006

Encapsulate a CaptchaImage anti-spam project in a composite web control

, 18 Nov 2006
Rate this:
Please Sign up or sign in to vote.
With this web control, you will be able to prevent spammers on your webforms (validation inplemented).

Sample Image - CaptchaImageControl.jpg


This article continues on an existing article found on CodeProject and written by Brainjar. You'll find this project here. He proposed a good strategy to prevent web forms from spammers:

CAPTCHA stands for "completely automated public Turing test to tell computers and humans apart." What it means is, a program that can tell humans from machines using some type of generated test. A test most people can easily pass but a computer program cannot.

You've probably encountered such tests when signing up for an online email or forum account. The form might include an image of distorted text, like that seen above, which you are required to type into a text field.

The idea is to prevent spammers from using web bots to automatically post form data in order to create email accounts (for sending spam) or to submit feedback comments or guestbook entries containing spam messages. The text in the image is usually distorted to prevent the use of OCR (optical character reader) software to defeat the process. Hotmail, PayPal, Yahoo!, and a number of blog sites have employed this technique.


What I wanted is to have a control which I could use to put on my web page, and this control should immediately prevent a form from spam.

The idea

I have written a WebControl which encapsulates:

  • an image: this image is using as source "CaptchaImage.axd". This "page" is in fact an HTTP handler which will return an image in the response stream. This image contains the "anti-spam" code, and has been generated using the CaptchaImage object provided by BrainJar.
  • a textbox: the user has to fill the image code in the textbox in order to ensure it is not an automated bot which is working on the form.
  • a RequiredValidator: this will ensure that there is data filled in the textbox.
  • a CustomValidator: it will test the text in the textbox with the anti-spam code which has been stored in session.

The Content

Design of CaptchaImageControl usage

How to install it

  1. Add a reference to the CaptchaImage assembly.
  2. In the web.config file, you have two lines to add in /configuration/system.web:
    1. In the ./HttpHandler section, add a reference to the handler which returns a generated CaptchaImage.
    2. <httpHandlers>
        <add verb="GET" path="CaptchaImage.axd" 
    3. In the ./controls section, add the tag reference of the Captcha image assembly which contains the interesting web control.
    4. <controls>
      <add tagPrefix="Captcha" namespace="CaptchaImage" assembly="CaptchaImage"/>
  3. Well... it is almost done. Now, let's go to use it.

How to use it

  1. Create your form to prevent from spam.
  2. Drag and drop the web control which has appeared in your toolbar, or add this tag:
  3. <Captcha:CaptchaControl runat="server" ID="captcha" />
  4. Specify a ValidationGroup on your Submit button and give the same name to the ValidationGroup property of the captcha control.
  5. ValidationGroup="Contact"
  6. Another property which could be of interest is "MessageError". This will set an error message that you could get through a validation summary like in the sample project.
  7. MessageError="An error has occured at validation of anti-spam code.
                  <br/> Pleade check it in again."
  8. The tag should have this appearance now:
  9. <Captcha:CaptchaControl runat="server" ID="captcha" 
       MessageError="An error has occured at validation of anti-spam code.
                     <br/> Pleade check it in again." />
  10. The last step to do is to add a test in the OnClick event of your Submit button.
  11. protected void SubmitButton_Click(object sender, EventArgs e)
        if (Page.IsValid) { //TO DO } 


Learn about:

  • Validators strategy provided by ASP.NET
  • Web controls
  • HttpHandler


HttpHandlers are a quite new logic for me, and I chose this technology mainly for the background work instead of building a web page. May be it's not done for this purpose... may be it is... but I think it's more logical to work like that when doing automated stuff.

CaptchaImage was a well built project and it was a pleasure to use it.

Greetz to


and every source which is provided on the net.

Sorry... as usual, for my poor English ;)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Other UMH
Belgium Belgium
* Bachelor of Information Management at ISAT Charleroi in Belgium.
* Master II of Information science at UMH Mons.

I spend my time in small projects which can help me or small enterprises and
I am very interested in designing projects
with specifications of UML or in applying usual design patterns.

You may also be interested in...


Comments and Discussions

QuestionCaptcha image is not showing up. Pin
fniles31-Oct-13 12:20
memberfniles31-Oct-13 12:20 
Generalcapthca Pin
mmilitar30-Oct-09 14:17
membermmilitar30-Oct-09 14:17 
GeneralupdatePanel Pin
Member 260850925-Nov-08 19:08
memberMember 260850925-Nov-08 19:08 
QuestionWith AJAX Page... It Fails.... Pin
Sandeepan2-May-08 18:17
memberSandeepan2-May-08 18:17 
QuestionWhat does this requiere? Pin
Netzie11-May-07 13:51
memberNetzie11-May-07 13:51 
AnswerRe: What does this requiere? Pin
BlaiseBraye11-May-07 21:05
memberBlaiseBraye11-May-07 21:05 
Generalclient-side validation Pin
relish2710-May-07 18:33
memberrelish2710-May-07 18:33 
GeneralRe: client-side validation Pin
BlaiseBraye10-May-07 22:28
memberBlaiseBraye10-May-07 22:28 
If I had some time for me, I ensure you I would do everything you want but it's not the case Frown | :(

anyway, about the client side validation, I am not for, and this only for security purposes.

But if you want to do this working on client side with the captcha control, you have two possible ways:
1. the magic word is encrypted somwhere in the web page and an algorythm from your brain is able to compare the given user word with the good one ---> if you do that, post it on code project but I don't think it's a good issue.

2. create a callback when you want to validate this one. You will have a call to the server but behind. So, you won't have to reload the webpage but you must do a callback.

but really I still think it's a bad idea and you risk to lose time more than else with this idea... (Listen to me, I am an expert about losing my time Big Grin | :-D )

Let's make code sharing our goal...
Blaise Braye

GeneralSaved me hours of coding and headaches. Pin
arsindelve19-Apr-07 8:52
memberarsindelve19-Apr-07 8:52 
GeneralSuggested modifications Pin
markrae18-Jan-07 14:24
membermarkrae18-Jan-07 14:24 
QuestionCaptcha Image VB? Pin
ullk27-Nov-06 9:27
memberullk27-Nov-06 9:27 
AnswerRe: Captcha Image VB? Pin
BlaiseBraye27-Nov-06 10:04
memberBlaiseBraye27-Nov-06 10:04 
GeneralRe: Captcha Image VB? Pin
ullk28-Nov-06 9:56
memberullk28-Nov-06 9:56 
GeneralRe: Captcha Image VB? Pin
BlaiseBraye28-Nov-06 10:12
memberBlaiseBraye28-Nov-06 10:12 
GeneralRe: Captcha Image VB? Pin
ullk30-Nov-06 11:38
memberullk30-Nov-06 11:38 
GeneralRe: Captcha Image VB? Pin
BlaiseBraye30-Nov-06 12:22
memberBlaiseBraye30-Nov-06 12:22 
GeneralGenerateRandomText Pin
developerz26-Nov-06 5:01
memberdeveloperz26-Nov-06 5:01 
GeneralRe: GenerateRandomText Pin
BlaiseBraye26-Nov-06 6:17
memberBlaiseBraye26-Nov-06 6:17 
GeneralUpdate [modified] Pin
BlaiseBraye18-Nov-06 8:15
memberBlaiseBraye18-Nov-06 8:15 
GeneralRe: Update Pin
Guzz27-Nov-06 23:14
memberGuzz27-Nov-06 23:14 
GeneralRe: Update Pin
BlaiseBraye28-Nov-06 6:50
memberBlaiseBraye28-Nov-06 6:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 18 Nov 2006
Article Copyright 2006 by BlaiseBraye
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid