Click here to Skip to main content
Click here to Skip to main content

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

, 18 Nov 2006 CPOL
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 Wink | ;)


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.

Comments and Discussions

QuestionCaptcha image is not showing up. Pinmemberfniles31-Oct-13 12:20 
Generalcapthca Pinmembermmilitar30-Oct-09 14:17 
GeneralupdatePanel PinmemberMember 260850925-Nov-08 19:08 
QuestionWith AJAX Page... It Fails.... PinmemberSandeepan2-May-08 18:17 
QuestionWhat does this requiere? PinmemberNetzie11-May-07 13:51 
AnswerRe: What does this requiere? PinmemberBlaiseBraye11-May-07 21:05 
Generalclient-side validation Pinmemberrelish2710-May-07 18:33 
GeneralRe: client-side validation PinmemberBlaiseBraye10-May-07 22:28 
GeneralSaved me hours of coding and headaches. Pinmemberarsindelve19-Apr-07 8:52 
Thanks for posting this. It was really easy to get up and running and I had no problems at all. I'm sure it would have taken hours and hours to do this from scratch! Thanks!
GeneralSuggested modifications Pinmembermarkrae18-Jan-07 14:24 
QuestionCaptcha Image VB? Pinmemberullk27-Nov-06 9:27 
AnswerRe: Captcha Image VB? PinmemberBlaiseBraye27-Nov-06 10:04 
GeneralRe: Captcha Image VB? Pinmemberullk28-Nov-06 9:56 
GeneralRe: Captcha Image VB? PinmemberBlaiseBraye28-Nov-06 10:12 
GeneralRe: Captcha Image VB? Pinmemberullk30-Nov-06 11:38 
GeneralRe: Captcha Image VB? PinmemberBlaiseBraye30-Nov-06 12:22 
GeneralGenerateRandomText Pinmemberdeveloperz26-Nov-06 5:01 
GeneralRe: GenerateRandomText PinmemberBlaiseBraye26-Nov-06 6:17 
GeneralUpdate [modified] PinmemberBlaiseBraye18-Nov-06 8:15 
GeneralRe: Update PinmemberGuzz27-Nov-06 23:14 
GeneralRe: Update PinmemberBlaiseBraye28-Nov-06 6:50 

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

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

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