Click here to Skip to main content
15,894,907 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
4.19/5 (6 votes)
18 Nov 2006CPOL3 min read 81.9K   767   29  
With this web control, you will be able to prevent spammers on your webforms (validation inplemented).
<%@ Page language="c#" CodeFile="Default.aspx.cs" AutoEventWireup="false" Inherits="CaptchaImage._Default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>CaptchaImage Test</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		<style type="text/css">
			BODY { FONT-SIZE: 10pt; FONT-FAMILY: sans-serif }
			TD { FONT-SIZE: 10pt; FONT-FAMILY: sans-serif }
			TH { FONT-SIZE: 10pt; FONT-FAMILY: sans-serif }
			.notice { font-size: 90%; }
			.info { FONT-WEIGHT: bold; COLOR: #008000 }
			.error { FONT-WEIGHT: bold; COLOR: #800000 }
		</style>
	</HEAD>
	<body>
	
	
		<h2>CaptchaImage Test</h2>
		<p>A demonstration using the <code>CaptchaImage</code> object to prevent automated 
			form submission.</p>
		<form id="Default" method="post" runat="server">
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="False"
                ValidationGroup="Contact" />
            <br />
            <table border="0" cellpadding="1" cellspacing="1">
                <tr>
                    <td align="right" style="width: 100px" valign="top">
                        First Name :</td>
                    <td align="right" valign="top">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="textBoxFirstName"
                            EnableClientScript="False" ErrorMessage="Fill in your first name please" SetFocusOnError="True"
                            ValidationGroup="Contact">*</asp:RequiredFieldValidator></td>
                    <td valign="top">
                        <asp:TextBox ID="textBoxFirstName" runat="server" MaxLength="50"></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" style="width: 100px" valign="top">
                        Last Name :
                    </td>
                    <td align="right" valign="top">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="textBoxLastName"
                            EnableClientScript="False" ErrorMessage="Fill in your last name please" SetFocusOnError="True"
                            ValidationGroup="Contact">*</asp:RequiredFieldValidator></td>
                    <td valign="top">
                        <asp:TextBox ID="textBoxLastName" runat="server" MaxLength="50"></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" style="width: 100px" valign="top">
                        Email :</td>
                    <td align="right" valign="top">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="textBoxEmail"
                            Display="Dynamic" EnableClientScript="False" ErrorMessage="Fill in your email please"
                            SetFocusOnError="True" ValidationGroup="Contact">*</asp:RequiredFieldValidator><asp:RegularExpressionValidator
                                ID="RegularExpressionValidator1" runat="server" ControlToValidate="textBoxEmail"
                                Display="Dynamic" EnableClientScript="False" ErrorMessage="verify the format of your email please"
                                SetFocusOnError="True" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                                ValidationGroup="Contact">*</asp:RegularExpressionValidator></td>
                    <td valign="top">
                        <asp:TextBox ID="textBoxEmail" runat="server" MaxLength="80" Width="200px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" style="width: 100px; height: 26px" valign="top">
                        Subject :</td>
                    <td align="right" valign="top">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="textBoxSubject"
                            EnableClientScript="False" ErrorMessage="Fill in the subject of your request please"
                            SetFocusOnError="True" ValidationGroup="Contact">*</asp:RequiredFieldValidator></td>
                    <td valign="top">
                        <asp:TextBox ID="textBoxSubject" runat="server" MaxLength="100" Width="300px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right" style="width: 100px" valign="top">
                        Request :</td>
                    <td align="right" valign="top">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="textBoxRequest"
                            EnableClientScript="False" ErrorMessage="Fill in your request please"
                            SetFocusOnError="True" ValidationGroup="Contact">*</asp:RequiredFieldValidator></td>
                    <td valign="top">
                        <asp:TextBox ID="textBoxRequest" runat="server" Columns="35" Height="160px" TextMode="MultiLine">
                                                                    </asp:TextBox></td>
                </tr>
                <tr>
                    <td align="right">
                        Anti-Spam :</td>
                    <td>
                    </td>
                    <td>
		    <Captcha:CaptchaControl runat="server" ID="captcha" ValidationGroup="Contact" 
		    MessageError="An error has occured at validation of anti-spam code.<br/> Pleade check it in again." />
                    </td>
                </tr>
                <tr>
                    <td align="right">
                    </td>
                    <td>
                    </td>
                    <td align="right">
				<asp:Button id="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" ValidationGroup="Contact"></asp:Button></td>
                </tr>
            </table>
            <br />
            &nbsp;
		    <br>
			<p>
				<br>
                &nbsp;</p>
			<p>
				<em class="notice">(Note: If you cannot read the numbers in the above<br>
					image, reload the page to generate a new one.)</em>
			</p>
		</form>
	</body>
</HTML>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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