In this article we will create a registration page with captcha implemented to it. Before going forward we should know what is captcha?

CAPTCHA: - Completely Automated Public Turing test to tell Computers and Humans Apart. A captcha is a program that protects websites against bots by generating and grading tests that humans can pass but current computer programs cannot. For example, humans can read distorted text but current computer programs can't.

Characteristics: A captcha is a means of automatically generating challenges which intends to:

For more information please visit

http://www.captcha.net

http://en.wikipedia.org/wiki/CAPTCHA

Table structure

Create two tables employee and country as below:

Table-in-SQL-Server.gif
Program

Default.aspx code

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Registration Page</title>
  6. <style type="text/css">
  7. #datePicker
  8. {
  9. display:none;
  10. position:absolute;
  11. border:solid 2px black;
  12. background-color:white;
  13. }
  14. .content
  15. {
  16. width:400px;
  17. background-color:white;
  18. margin:auto;
  19. padding:10px;
  20. }
  21. html
  22. {
  23. background-color:white;
  24. }
  25. .style10
  26. {
  27. background-color: #eeeeee;
  28. height: 26px;
  29. }
  30. .style11
  31. {
  32. height: 26px;
  33. }
  34. .style12
  35. {
  36. background-color: #eeeeee;
  37. }
  38. </style>
  39. <link href="style.css" rel="stylesheet" type="text/css" />
  40. </head>
  41. <body>
  42. <form id="form1" runat="server">
  43. <div>
  44. <table>
  45. <tr>
  46. <td class="deepbluetextbold"><b>Employee Information</b></td>
  47. </tr>
  48. <tr>
  49. <td class="colouredCell">
  50. <asp:Label runat="server" ID="LocationLabel1" AssociatedControlID="LocationLabel1"CssClass="style7" Text="Title" />
  51. </td>
  52. <td>
  53. <asp:RadioButtonList id="rb1" runat="server" RepeatLayout="Flow" RepeatColumns="4"
  54. RepeatDirection="Horizontal" Width="200px" >
  55. <asp:ListItem Text="Mr." Selected="False"/>
  56. <asp:ListItem Text="Mrs."/>
  57. <asp:ListItem Text="M/S"/>
  58. <asp:ListItem Text="Dr."/>
  59. </asp:RadioButtonList>
  60. </td>
  61. <td><asp:RequiredFieldValidator ID="rfv1" Text="Select any Choice" runat="server"
  62. ControlToValidate="rb1"></asp:RequiredFieldValidator></td>
  63. </tr>
  64. <tr>
  65. <td class="colouredCell">
  66. <asp:Label runat="server" ID="LocationLabel2" AssociatedControlID="LocationLabel2"CssClass="style7" Text="Name*" />
  67. </td>
  68. <td>
  69. <asp:TextBox runat="server" ID="txtname" Width="200px" />
  70. </td>
  71. <td>
  72. <asp:RequiredFieldValidator ID="rfv2" Text="Please Enter the Name" runat="server"ControlToValidate="txtname"></asp:RequiredFieldValidator>
  73. </td>
  74. </tr>
  75. <tr>
  76. <td class="colouredCell">
  77. <asp:Label runat="server" ID="LocationLabel3" AssociatedControlID="LocationLabel3"CssClass="style7" Text="Email*" />
  78. </td>
  79. <td>
  80. <asp:TextBox runat="server" ID="txtemail" Width="200px"/>
  81. </td>
  82. <td>
  83. <asp:RequiredFieldValidator ID="rfv3" Text="Please Enter the Email" runat="server"ControlToValidate="txtemail"></asp:RequiredFieldValidator>
  84. </td>
  85. <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
  86. ErrorMessage="Please enter valid email"
  87. ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
  88. ControlToValidate="txtemail"></asp:RegularExpressionValidator></td>
  89. </tr>
  90. <tr>
  91. <td class="colouredCell">
  92. <asp:Label runat="server" ID="LocationLabel4" AssociatedControlID="LocationLabel4"CssClass="style7" Text="Date of Birth*" />
  93. </td>
  94. <td colspan="0" rowspan="1">
  95. <asp:TextBox runat="server" ID="txtdateofbirth" Width="200px"/>
  96. </td>
  97. <td>
  98. <asp:RequiredFieldValidator ID="rfv4" Text="Please Enter the Dateofbirth" runat="server"ControlToValidate="txtdateofbirth"
  99. </asp:RequiredFieldValidator>
  100. </td>
  101. </tr>
  102. <tr>
  103. <td class="colouredCell">
  104. <asp:Label runat="server" ID="LocationLabel5" AssociatedControlID="LocationLabel5"CssClass="style7" Text="Guest?*" />
  105. </td>
  106. <td>
  107. <asp:RadioButtonList id="rb2" runat="server" RepeatLayout="Flow" RepeatColumns="3"RepeatDirection="Horizontal" >
  108. <asp:ListItem Text="Yes" Selected="False"/>
  109. <asp:ListItem Text="No"/>
  110. </asp:RadioButtonList>
  111. </td>
  112. <td>
  113. <asp:RequiredFieldValidator ID="rfv5" Text="Select yes or no" runat="server"ControlToValidate="rb2"></asp:RequiredFieldValidator>
  114. </td>
  115. </tr>
  116. <tr>
  117. <td class="colouredCell">
  118. <asp:Label runat="server" ID="LocationLabel6" AssociatedControlID="LocationLabel6"
  119. CssClass="style7" Text="Company/Organization*" />
  120. </td>
  121. <td>
  122. <asp:TextBox runat="server" ID="txtcompany" Width="200px"/>
  123. </td>
  124. <td>
  125. <asp:RequiredFieldValidator ID="rfv6" Text="Please Enter the Company" runat="server"ControlToValidate="txtcompany"></asp:RequiredFieldValidator>
  126. </td>
  127. </tr>
  128. <tr>
  129. <td class="colouredCell">
  130. <asp:Label runat="server" ID="LocationLabel7" AssociatedControlID="LocationLabel7"
  131. CssClass="style7" Text="Designation*" />
  132. </td>
  133. <td>
  134. <asp:TextBox runat="server" ID="txtdeg" Width="200px"/>
  135. </td>
  136. <td>
  137. <asp:RequiredFieldValidator ID="rfv7" Text="Please Enter the Designation" runat="server"ControlToValidate="txtdeg"></asp:RequiredFieldValidator>
  138. </td>
  139. </tr>
  140. <tr>
  141. <td class="colouredCell">
  142. <asp:Label runat="server" ID="LocationLabel8" AssociatedControlID="LocationLabel8"
  143. CssClass="style7" Text="Street Address*" />
  144. </td>
  145. <td>
  146. <asp:TextBox runat="server" ID="txtaddress" Width="200px"/>
  147. </td>
  148. <td>
  149. <asp:RequiredFieldValidator ID="rfv8" Text="Please Enter the Street Address"runat="server" ControlToValidate="txtaddress"
  150. </asp:RequiredFieldValidator>
  151. </td>
  152. </tr>
  153. <tr>
  154. <td class="colouredCell">
  155. <asp:Label runat="server" ID="LocationLabel9" AssociatedControlID="LocationLabel9"
  156. CssClass="style7" Text="City*" />
  157. </td>
  158. <td>
  159. <asp:TextBox runat="server" ID="txtcity" Width="200px"/>
  160. </td>
  161. <td>
  162. <asp:RequiredFieldValidator ID="rfv9" Text="Please Enter the Street City" runat="server"ControlToValidate="txtcity"</asp:RequiredFieldValidator>
  163. </td>
  164. </tr>
  165. <tr>
  166. <td class="colouredCell">
  167. <asp:Label runat="server" ID="LocationLabel10" AssociatedControlID="LocationLabel10"
  168. CssClass="style7" Text="State" />
  169. </td>
  170. <td>
  171. <asp:TextBox runat="server" ID="txtstate" Width="200px"/>
  172. </td>
  173. <td>
  174. <asp:RequiredFieldValidator ID="rfv10" Text="Please Enter the Street state"runat="server" ControlToValidate="txtstate"
  175. </asp:RequiredFieldValidator>
  176. </td>
  177. </tr>
  178. <tr>
  179. <td class="colouredCell">
  180. <asp:Label runat="server" ID="LocationLabel11" AssociatedControlID="LocationLabel11"
  181. CssClass="style7" Text="Zip Code*" />
  182. </td>
  183. <td>
  184. <asp:TextBox runat="server" ID="txtzip" Width="200px"/>
  185. </td>
  186. <td>
  187. <asp:RequiredFieldValidator ID="rfv11" Text="Please Enter the Zip" runat="server"ControlToValidate="txtzip"></asp:RequiredFieldValidator>
  188. </td>
  189. </tr>
  190. <tr>
  191. <td class="style10">
  192. <asp:Label runat="server" ID="LocationLabel12" AssociatedControlID="LocationLabel12"
  193. CssClass="style7" Text="Phone*" />
  194. </td>
  195. <td class="style11">
  196. <asp:TextBox runat="server" ID="txtphone" Width="200px"/>
  197. </td>
  198. <td>
  199. <asp:RequiredFieldValidator ID="rfv12" Text="Please Enter the Phone" runat="server"ControlToValidate="txtphone
  200. </asp:RequiredFieldValidator>
  201. </td>
  202. </tr>
  203. <tr>
  204. <td class="colouredCell">
  205. <asp:Label runat="server" ID="LocationLabel13" AssociatedControlID="LocationLabel13"
  206. CssClass="style7" Text="Fax*" />
  207. </td>
  208. <td>
  209. <asp:TextBox runat="server" ID="txtfax" Width="200px"/>
  210. </td>
  211. <td>
  212. <asp:RequiredFieldValidator ID="rfv13" Text="Please Enter the Fax" runat="server"ControlToValidate="txtfax"></asp:RequiredFieldValidator>
  213. </td>
  214. </tr>
  215. <tr>
  216. <td class="colouredCell">
  217. <asp:Label runat="server" ID="LocationLabel14" AssociatedControlID="LocationLabel14"
  218. CssClass="style7" Text="Website*" />
  219. </td>
  220. <td>
  221. <asp:TextBox runat="server" ID="txtwebsite" Width="200px"/> </td>
  222. <td>
  223. <asp:RequiredFieldValidator ID="rfv14" Text="Please Enter the Website" runat="server"ControlToValidate="txtwebsite"</asp:RequiredFieldValidator>
  224. </td>
  225. <td> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
  226. ErrorMessage="Please enter valid Url"
  227. ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"
  228. ControlToValidate="txtwebsite"></asp:RegularExpressionValidator>
  229. </td>
  230. </tr>
  231. <tr>
  232. <td class="style12">
  233. <asp:Label runat="server" ID="LocationLabel15" AssociatedControlID="LocationLabel15"
  234. CssClass="style7" Text="Country*" />
  235. </td>
  236. <td>
  237. <asp:DropDownList ID="d1" Width="200px" runat="server">
  238. <asp:ListItem>Select</asp:ListItem>
  239. </asp:DropDownList>
  240. </td>
  241. <td>
  242. <asp:RequiredFieldValidator ID="rfv15" Text="Please select any value" runat="server"ControlToValidate="d1"></asp:RequiredFieldValidator>
  243. </td>
  244. </tr>
  245. <tr>
  246. <td class="colouredCell">
  247. <asp:Label runat="server" ID="LocationLabel16" AssociatedControlID="LocationLabel16"
  248. CssClass="style7" Text="Captcha Image*" />
  249. </td>
  250. <td><asp:TextBox runat="server" ID="txtcaptcha" Width="200px"/></td>
  251. <td>
  252. <img style="WIDTH: 119px; HEIGHT: 34px" alt="" src="Captcha.aspx" />
  253. </td>
  254. <td><a href="Default.aspx" id="uu">Can't Read?</a></td>
  255. </tr>
  256. </table>
  257. </div>
  258. <asp:Button ID="b1" runat="server" Text="Register" />
  259. </form>
  260. </body>
  261. </html>

Default.aspx.vb code

  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Partial Class _Default
  4. Inherits System.Web.UI.Page
  5. Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
  6. Dim sqlda As SqlDataAdapter
  7. Dim com As SqlCommand
  8. Dim ds As DataSet
  9. Dim dt As DataTable
  10. Dim str As String
  11. Dim con As New SqlConnection(strConnString)
  12. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
  13. If Not Page.IsPostBack Then
  14. con.Open()
  15. str = "select * from country"
  16. com = New SqlCommand(str, con)
  17. Dim reader As SqlDataReader = com.ExecuteReader
  18. While reader.Read
  19. d1.Items.Add(reader("countryname"))
  20. End While
  21. reader.Close()
  22. con.Close()
  23. End If
  24. End Sub
  25. Protected Sub b1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles b1.Click
  26. Try
  27. con.Open()
  28. If Session("randomStr").ToString() = txtcaptcha.Text Then
  29. str = "insert into employee values ('" & rb1.SelectedValue & "','" & txtname.Text & "','" & txtemail.Text & "','" & txtdateofbirth.Text & "','" & rb2.SelectedValue & "','" & txtcompany.Text & "','"& txtdeg.Text & "','" & txtaddress.Text & "','" & txtcity.Text & "','" & txtstate.Text & "','" & txtzip.Text & "'," & txtphone.Text & "," & txtfax.Text & ",'" & txtwebsite.Text & "','" & d1.SelectedValue & "','" & txtcaptcha.Text & "')"
  30. com = New SqlCommand(str, con)
  31. com.ExecuteNonQuery()
  32. Response.Redirect("Success.aspx")
  33. con.Close()
  34. Else
  35. Response.Write("Wrong text inserted ,Please enter new characters shown in image textbox")
  36. End If
  37. Catch err As Exception
  38. Response.Write(err.Message)
  39. End Try
  40. End Sub
  41. End Class
Captcha.aspx code
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Captcha.aspx.vb" Inherits="Captcha"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Untitled Page</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. </div>
  11. </form>
  12. </body>
  13. </html>
Captcha.aspx.vb code
  1. Imports System.Drawing
  2. Imports System.Drawing.Imaging
  3. Imports System.Drawing.Text
  4. Partial Class Captcha
  5. Inherits System.Web.UI.Page
  6. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
  7. 'create object of Bitmap Class and set its width and height.
  8. Dim objBMP As Bitmap = New Bitmap(180, 51)
  9. 'Create Graphics object and assign bitmap object to graphics' object.
  10. Dim objGraphics As Graphics = Graphics.FromImage(objBMP)
  11. objGraphics.Clear(Color.OrangeRed)
  12. objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
  13. Dim objFont As Font = New Font("arial", 30, FontStyle.Regular)
  14. 'genetating random 6 digit random number
  15. Dim randomStr As String = GeneratePassword()
  16. 'set this random number in session
  17. Session.Add("randomStr", randomStr)
  18. objGraphics.DrawString(randomStr, objFont, Brushes.White, 2, 2)
  19. Response.ContentType = "image/GIF"
  20. objBMP.Save(Response.OutputStream, ImageFormat.Gif)
  21. objFont.Dispose()
  22. objGraphics.Dispose()
  23. objBMP.Dispose()
  24. End Sub
  25. Public Function GeneratePassword() As String
  26. ' Below code describes how to create random numbers.some of the digits and letters
  27. ' are ommited because they look same like "i","o","1","0","I","O".
  28. Dim allowedChars As String = "a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,"
  29. allowedChars += "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,"
  30. allowedChars += "2,3,4,5,6,7,8,9"
  31. Dim sep() As Char = {","c}
  32. Dim arr() As String = allowedChars.Split(sep)
  33. Dim passwordString As String = ""
  34. Dim temp As String
  35. Dim rand As Random = New Random()
  36. Dim i As Integer
  37. For i = 0 To 6 - 1 Step i + 1
  38. temp = arr(rand.Next(0, arr.Length))
  39. passwordString += temp
  40. Next
  41. Return passwordString
  42. End Function
  43. End Class
Output

CAPTCHA-in-VB.NET.gif