Home > ASP.Net > Captcha Verification Image

Captcha Verification Image


Hi

try this example to create Contact us Page with Verification Image (Captcha)

Step1 :Create CaptchaImage.aspx Page

in code behind:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;

public partial class CaptchaImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Random Rand = new Random();
        int iNum = Rand.Next(10000, 99999);
        Bitmap Bmp = new Bitmap(90, 50);
        Graphics Gfx = Graphics.FromImage(Bmp);
        Font Fnt = new Font("Verdana", 12, FontStyle.Bold);
        Gfx.DrawString(iNum.ToString(), Fnt, Brushes.Yellow, 15, 15);
        // Create random numbers for the first line
        int RandY1 = Rand.Next(0, 50);
        int RandY2 = Rand.Next(0, 50);

        // Draw the first line
        Gfx.DrawLine(Pens.Yellow, 0, RandY1, 90, RandY2);
        // Create random numbers for the second line
        RandY1 = Rand.Next(0, 50);
        RandY2 = Rand.Next(0, 50);
        // Draw the second line
        Gfx.DrawLine(Pens.Yellow, 0, RandY1, 90, RandY2);
        Bmp.Save(Response.OutputStream, ImageFormat.Gif);
        Session["Number"] = iNum;
    }
}

step 2: Create Default.aspx page

<%@ PAGE LANGUAGE="C#" AUTOEVENTWIREUP="true" CODEFILE="Default.aspx.cs" INHERITS="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<HTML XMLNS="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<HEAD RUNAT="server">

</HEAD>
<BODY>
    <FORM ID="form1" RUNAT="server">
        <H2>Contact Us Form</H2>
        <TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 WIDTH=80%>
            <TR>
                <TD>
                    <strong>Full Name</strong></TD>
                <TD>:</TD>
                <TD style="width: 431px"><ASP:TEXTBOX ID=txtName RUNAT=server></ASP:TEXTBOX>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
                        ErrorMessage="Enter Your Name">*</asp:RequiredFieldValidator></TD>
            </TR>
            <TR>
                <TD>
                    <strong>Email Address</strong></TD>
                <TD>:</TD>
                <TD style="width: 431px"><ASP:TEXTBOX ID=txtEmail RUNAT=server></ASP:TEXTBOX>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail"
                        Display="Dynamic" ErrorMessage="Enter Email">*</asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail"
                        ErrorMessage="Enter Valid Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator></TD>
            </TR>
            <TR>
                <TD>
                    <strong>Subject </strong>
                </TD>
                <TD>:</TD>
                <TD style="width: 431px"><ASP:TEXTBOX ID=txtSubject RUNAT=server></ASP:TEXTBOX>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtSubject"
                        ErrorMessage="Enter Subject">*</asp:RequiredFieldValidator></TD>
            </TR>
            <TR>
                <TD VALIGN=top>
                    <strong>Body</strong></TD>
                <TD VALIGN=top>:</TD>
                <TD style="width: 431px"><ASP:TEXTBOX ID=txtBody RUNAT=server TEXTMODE=multiLine ROWS=10 COLUMNS=50></ASP:TEXTBOX>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtBody"
                        Display="Dynamic" ErrorMessage="Enter your Message">*</asp:RequiredFieldValidator>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtBody"
                        ErrorMessage="Message not more than 20 characters" OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator></TD>
            </TR>
             <TR>
                <TD VALIGN=top>
                    <strong>Verification Image</strong></TD>
                <TD VALIGN=top>:</TD>
                <TD style="width: 431px"><IMG SRC="CaptchaImage.aspx" /></TD>
            </TR>
            <TR>
                <TD VALIGN=top>
                    <strong>Please Enter the Number Above</strong></TD>
                <TD VALIGN=top>:</TD>
                <TD style="width: 431px"><ASP:TEXTBOX ID=txtNumber RUNAT=server></ASP:TEXTBOX></TD>
            </TR>
             <TR>
                <TD COLSPAN=3><ASP:BUTTON ID=btnSubmit RUNAT=server TEXT="Submit" ONCLICK="btnSubmit_Click" /></TD>
            </TR>
        </TABLE>
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please Enter the following:"
            Height="85px" Font-Bold="True" />
    </FORM>
</BODY>
</HTML>

in code behind:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Session["Number"].ToString().Trim() == txtNumber.Text)
        {
            try
            {
                // method to send email here

            }
            catch (Exception ex)
            {

            }
        }
        else
        {
            Response.Write("Verification Email not match. Please re-enter");
        }
    }

 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        //args.IsValid = (args.Value.Length <= 20);

        if (txtBody.Text.Length > 20)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
}

Hope it helps

Good Luck

Advertisements
Categories: ASP.Net Tags: ,
  1. jamshed
    June 18, 2010 at 7:39 am

    Nice article, it’s help me alot, keep on

  2. yasserzaid
    June 20, 2010 at 3:56 pm

    @jamshed :- you are welcome hope this helps you

  3. Virgo
    January 30, 2012 at 2:47 am

    simply great…keep it up

  4. Johne434
    May 11, 2014 at 5:48 pm

    hi and thanks for the actual blog post ive recently been searching regarding this specific advice online for sum hours these days as a result thanks kkeedbbgcfed

  5. Johnd514
    May 11, 2014 at 5:48 pm

    Hi, Neat post. There’s a problem with your site in internet explorer, would test this IE still is the market leader and a good portion of people will miss your excellent writing because of this problem. kbfacacebefd

  1. July 6, 2014 at 12:06 am

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: