SIGN UP MEMBER LOGIN:    
ARTICLE

Creating Captcha Code with JavaScript

Posted by Mahak Gupta Articles | ASP, JavaScript, CSS October 25, 2010
In this article we will see how to create a captcha code with JavaScript.
Reader Level:

Here I create a Captcha Code with JavaScript.

Step 1:

In the (.aspx) page we write the following Code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
     <script language="javascript">
         var a = 49, b = 65;
         var c = 100;
         var d = 70;
         function show() {
             if (a == 57) {
                 a = 49;
             }
             var main = document.getElementById('txt1');
             var a1 = String.fromCharCode(a);
             var b1 = String.fromCharCode(b);
             var c1 = String.fromCharCode(c);
             var d1 = String.fromCharCode(d);
             main.value = a1 + b1 + c1 + d1;
             a = a + 1;
             b = b + 1;
             c = c + 1;
             d = d + 1;
         }
  </script>
    <style type="text/css">
        #form1
        {
            height: 95px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="text" id="txt1" runat="server"
            style="border-style: none; border-color: inherit; border-width: medium; background-color:black; color:red; font-family: 'Curlz MT'; font-size: x-large; font-weight: bold; font-variant: normal; letter-spacing: 10pt; width: 120px; background-image: url('glitter_background_b4.gif');"
            value="5AbD" />
  <input type="button" onclick="show()" value="Change"  />
        </div>
    <asp:TextBox ID="txtverification" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="Verification"
        onclick="Button1_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label ID="lblmsg" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
    </form>
</body>
</html>

Step 2:

In the (.cs) page I write the following code for verification:

protected void Button1_Click(object sender, EventArgs e)
{
    if (txtverification.Text == txt1.Value)
    {
        lblmsg.Text = "Successful";
    }
    else
    {
        lblmsg.Text = "Failure";
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 

this is realy great... thanks for the article :)

Posted by Ravi K Kashyap Jun 23, 2011

suu waat se

Posted by zzzzzz dddddd Mar 15, 2011

the Captcha Code be created is too inflexible, and every time you entered this page,the Captcha is always be the same one, and the captcha code can't reflesh even you entered the false code,Overall this is good!

Posted by yelian chen Nov 09, 2010

Greaaat tutorial... thanks bro!

Posted by Arnel Nov 08, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor