Thursday, January 7, 2010

Captcha validation in Asp.net 2.0



Captcha validation in Asp.net 2.0.

Before Entering to topic first we must understand about the validation.Why most of the sites are validating the data's we entered.Like we gave a name with numeric values and special characters like.So these things are really wrong information by mistake wrongly typed.In email id most of the websites how they validate means they see the email id contains '@' followed with some words and having '.' symbol. So we have to give the informations correctly.

Ok, Let us come to the Topic.
most of the web sites having the Captcha validation to their sites.

What is a Captcha code?

Captcha code contains a char and integer values with mixed mode. It is used for validating this code was really typed by the human.

How to create our own captcha code in Asp.net 2.0

First create an image button in a Asp.net web page and in the imageurl give the capthanew.aspx. like this,

<asp:imagebutton id="ImageButton1" runat="server" width="105px" height="35px" imageurl="~/capthanew.aspx">

<asp:TextBox ID="txtcaptha" runat="server" Width="105px" MaxLength="6" CssClass="txtbx2"></asp:TextBox>

<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />

give a text box to enter the captcha text and also a button.

download the capthanew.aspx file from here Download

save the file in your website folder.

In aspx.cs file go to the btnSubmit_Click event to write the validation to check the user given code and captcha code are same.

if (Page.IsValid && (txtcaptha.Text.ToString() != Session["RandomStr"].ToString()))
{
Response.Write("Captcha code was wrong.");
}

This is the implementation of a captcha code if you want to generate the code dynamically means just give a linkbutton near to the Captcha imagebutton and give the path to capthanew.aspx this will give a dynamic code whenever you reload the page by linkbutton.

Actually how this captcha code works means, we just create a random numbers and characters and create a bitmap image and render these two things. We can give our own color also.

Happy code for Captcha. download the capthanew.aspx file from here Download

8 comments:

  1. For share..

    Recently, I wrote the same think. But, in my captcha program, I didn't use as aspx file. I prefer to use a handler file (.ashx) named "spamhandler.ashx".

    Check this out
    http://angkringankode.wordpress.com/2010/01/17/membuat-anti-spam-di-asp-net-2/

    Happy Coding

    ReplyDelete
  2. Info in your blog help me with my project, whitch based on букмекеры

    ReplyDelete
  3. Great article

    We can also submit our .net related links on http://www.dotnettechy.com to increase traffics of your website / blog

    Its kind of social networking for .Net professionals only

    ReplyDelete
  4. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


    Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

    ReplyDelete
  5. his article is very nice and easy to understand, it was a very weighty once thanks a lot admin can hopefully be improved in the future and pardon me permission to share the article here : Cara menyembuhkan gagal jantung

    ReplyDelete
  6. articles that you provide a very interesting and very useful for me, thank you very much admin and pardon me permission to share articles herein may be useful also for the reading : cara menyembuhkan penyakit katup jantung

    ReplyDelete
  7. The Blog gave me idea about Captcha Validation in asp.net My sincere thanks for sharing this post
    Dot Net Training in Chennai

    ReplyDelete

Followers