Create Login & Register With Captcha on VB.Net - Gudio Tutorial

    Social Items


Create Login & Register With Captcha on VB.Net

Create Login & Register With Captcha On VB.Net - In this article, I will give you a tutorial on Creating a Captcha Login and Register on VB.Net. Many applications nowadays, be it a website or desktop using this captcha method, especially in the "Login" and "Registration" section of the user. What for ? in short, its function is to make sure that the login or register is true human and not a bot. For more details, read on this article to the end, so you can understand about the captcha function. And can make applications using the captcha method.

What is A Captcha?

Captcha literally stands for "Completely Automated Public Turing test to tell Computers and Humans Apart" (quoted from Wikipedia), made to distinguish between machines (bots) and humans. The term Captcha was coined by Luis von Ahn, Manuel Blum, and Nicholas J. Hopper of Carnegie MellonUniversity, and John Langford of IBM in 2000.


why should it be distinguished? because a lot of waste "form criminals" aka SPAM COMMENT programs that can just attack our website at any time without "manners" inserting comments on guestbooks / forums whose contents can be promotions or even links to pornographic sites. that's the possibility that will occur on our website if affected above.

Captcha What Is Used / Functions For?

So basically, the main functions of this Captcha are as follows:
  • Prevent spam comment in general the web / blog owner / and others do not just want the website to look good in appearance, quantity but of course quality, but do not know if the admin itself is spam
  • Prevent spam bots, you know that bots cannot read Captcha, because the string string is fixed. It is impossible for Capthca to change every time it can be read unless the bot comes from the engine blog itself
  • Prevent Flood who wants to be on Flood? except for flood like tp if there are many inappropriate words flood, who wants to try? For you who haven't, Flood is sending data over and over again.
  • Protecting Email / User Registration (webmail only) Once there was a Yahoo incident and Google was attacked by a bot that registered thousands of emails! thousands of fake e-mails.
Okay, go directly to the topic of discussion, here I use mysql database as a database processing. I assume you can connect mysql database with vb.net. If it hasn't, you can read this article:

Read : Connection To VB.Net With MySQL.

In this tutorial, we will use 3 forms, namely as:
  1. User Login
  2. User Registration
  3. Main Form

Design Form Login

First open your visual studio first, then create a form with the name "frmLogin",Please it's up to the way you design it, the important thing is that the object is:
Object Name Property QTY Function
Text Box txtUsername, txtCaptcha, txtPass 3 textbox function to hold user data to log in
Label lblUsername, lblPassword, lblNotif 3 lblNotif is used to record errors when a user clicks the login button
Button btnRefresh, btnLogin, btnRegister 3 as a trigger for the code in the application
PictureBox picUser, picPass, picCaptcha 3 so that the application's appearance is more interesting, and picCaptcha is used to display the captcha code
The result is like this:

Create Login & Register With Captcha on VB.Net


Please create your own imagination to design it.


Design Form Sign Up

After creating the login form, then now create the Sign Up form to register the new user. Create a new form named "frmSignUp" and its objects as in the table below:
Object Name Property QTY Function
Text Box txtUsername, txtCaptcha, txtPass, txtConfirmPass 4 textbox function to hold user data to log in
Label lblUsername, lblPassword, lblNotifikasi, lblConfirmPass 4 lblNotif is used to record errors when a user clicks the Register button
Button btnRefresh, btnLogin, btnRegister 3 as a trigger for the code in the application
PictureBox picUser, picPass, picCaptcha, picConfirmPass 4 so that the application's appearance is more interesting, and picCaptcha is used to display the captcha code
The result is like this:

Create Login & Register With Captcha on VB.Net

Then design the last form, we give the name "frmMain". And make the objects like the table below:
Object Name Property QTY Function
Label lblName, lblMessage 2 lblNotif is used to record errors when a user clicks the Register button
Button btnClose 1 to exit this form
The result is like this:


Create Login & Register With Captcha on VB.Net

Make Captcha Login on VB.Net (Coding)

If you have finished doing the design, proceed to the next step, which is to make a module to connect to the mysql database with vb.net.

Create Database
In this tutorial, we will create a database that is used to store user data and new user data. In this tutorial, I use a MySQL database.

Create Login & Register With Captcha on VB.Net

Connection Module
Click the project menu at the top then select the add module, and type the following code:

Module modConnect
    Public conn As New MySql.Data.MySqlClient.MySqlConnection
    Public Sub CheckConn(ByVal server As String, ByVal user As String, ByVal pass As String, ByVal db As String, ByVal port As String)
        Try
            If conn.State = ConnectionState.Closed Then
                conn.ConnectionString = "DATABASE=" & db & ";SERVER = " & server & ";user id=" & user & ";password=" & pass & ";port=" & port & ";charset=utf8"
                conn.Open()
            End If
        Catch ex As MySql.Data.MySqlClient.MySqlException
            MsgBox("Connection to server failed", MsgBoxStyle.Critical, "Gudio Application")
        End Try
    End Sub

    Public Sub disconnect()
        Try
            conn.Open()
        Catch ex As MySql.Data.MySqlClient.MySqlException
        End Try

    End Sub
End Module

Save the code above with the name "modKoneksi" or it's up to you to use any name.

Login Form
If so, double-click on the login form, and on the top line, import mysql data. Next is the code:
Imports MySql.Data.MySqlClient
If the mysql import option does not yet exist, then the method is, click the project menu and select add reference, then search for MySQL data, and click OK. And then for the variable declaration for the object, please type the following code:

Then add the following line of code to the form_load:

    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GenerateCaptcha()
        CheckConn("localhost", "root", "", "tutorloginreg", "")
    End Sub

Variable Declaration
Type the code below at the top of "frmLogin", the code below functions as a function variable declaration to generate the captcha :

    Dim DrawingFont As New Font("Arial", 20)
    Dim CaptchaImage As New Bitmap(140, 40)
    Dim CaptchaGraf As Graphics = Graphics.FromImage(CaptchaImage)
    Dim Alphabet As String = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    Dim CaptchaString, TickRandom As String
    Dim ProcessNumber As Integer

Generate Captcha Function
If so, then create a function to generate the captcha code, like this:

Private Sub GenerateCaptcha()
        ProcessNumber = My.Computer.Clock.LocalTime.Millisecond
        If ProcessNumber < 521 Then
            ProcessNumber = ProcessNumber \ 10
            CaptchaString = Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString = CStr(My.Computer.Clock.LocalTime.Second \ 6)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.Second
        If ProcessNumber < 30 Then
            ProcessNumber = Math.Abs(ProcessNumber - 8)
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(My.Computer.Clock.LocalTime.Minute \ 6)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.DayOfYear
        If ProcessNumber Mod 2 = 0 Then
            ProcessNumber = ProcessNumber \ 8
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(ProcessNumber \ 37)
        End If
        TickRandom = My.Computer.Clock.TickCount.ToString
        ProcessNumber = Val(TickRandom.Substring(TickRandom.Length - 1, 1))
        If ProcessNumber Mod 2 = 0 Then
            CaptchaString += CStr(ProcessNumber)
        Else
            ProcessNumber = Math.Abs(Int(Math.Cos(Val(TickRandom)) * 51))
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.Hour
        If ProcessNumber Mod 2 = 0 Then
            ProcessNumber = Math.Abs(Int(Math.Sin(Val(My.Computer.Clock.LocalTime.Year)) * 51))
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(ProcessNumber \ 3)
        End If
        For hasher As Integer = 0 To 5
            CaptchaGraf.DrawString(CaptchaString.Substring(hasher, 1), DrawingFont, Brushes.Black, hasher * 20 + hasher + ProcessNumber \ 200, (hasher Mod 3) * (ProcessNumber \ 
        200))
        Next
        picCaptcha.Image = CaptchaImage

Refresh Button Code
Then add the code to call the generate captcha function above on the refresh button. If the refresh button is clicked, the code will be encrypted again. the code is like this:

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GenerateCaptcha()
    End Sub

GenerateCaptcha ()
Login Button Code
Then continue by making a code on the login button. This code to validate user data before logging inType the code below on the login button :

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtUsername.Text = "" Then
            lblNotif.Text = "ERROR : INCOMPLETE DATA"
            lblNotif.ForeColor = Color.Red
            txtUsername.Focus()
            Exit Sub
        ElseIf txtPass.Text = "" Then
            lblNotif.Text = "ERROR : INCOMPLETE DATA"
            lblNotif.ForeColor = Color.Red
            txtPass.Focus()
            Exit Sub
        ElseIf txtCaptcha.Text = "" Then
            lblNotif.Text = "ERROR : THE CAPTCHA HAS NOT BEEN FILLED"
            lblNotif.ForeColor = Color.Red
            txtCaptcha.Focus()
            Exit Sub
        Else
            If txtCaptcha.Text = CaptchaString Then
                conn.Open()
                Dim myAdapter As New MySqlDataAdapter
                Dim sqlQuery = "SELECT * FROM tbUser WHERE name='" + txtUsername.Text + "'"
                Dim myCommand As New MySqlCommand
                myCommand.Connection = conn
                myCommand.CommandText = sqlQuery
                myAdapter.SelectCommand = myCommand
                Dim myData As MySqlDataReader
                myData = myCommand.ExecuteReader()

                If myData.HasRows = 0 Then
                    MsgBox("The username or password you entered is incorrect. It is also possible that your username is not yet registered.!", vbExclamation, "Gudio Application")
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    txtUsername.Focus()
                    Call GenerateCaptcha()
                Else
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    Me.Hide()
                    frmMain.lblName.Text = "Login As : " & txtUsername.Text
                    frmMain.Show()
                End If
            End If
        End If
    End Sub

Register Button Code
After that, continue by making a code on the register button. This code is for registering new users. Type the code below:

    Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        Me.Hide()
        frmSignUp.Show()
    End Sub

Full Code Login Form 
The following below is the full code from frmLogin :

Public Class frmLogin
    Dim DrawingFont As New Font("Arial", 20)
    Dim CaptchaImage As New Bitmap(140, 40)
    Dim CaptchaGraf As Graphics = Graphics.FromImage(CaptchaImage)
    Dim Alphabet As String = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    Dim CaptchaString, TickRandom As String
    Dim ProcessNumber As Integer
    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GenerateCaptcha()
        CheckConn("localhost", "root", "", "tutorloginreg", "")
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GenerateCaptcha()
    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtUsername.Text = "" Then
            lblNotif.Text = "ERROR : INCOMPLETE DATA"
            lblNotif.ForeColor = Color.Red
            txtUsername.Focus()
            Exit Sub
        ElseIf txtPass.Text = "" Then
            lblNotif.Text = "ERROR : INCOMPLETE DATA"
            lblNotif.ForeColor = Color.Red
            txtPass.Focus()
            Exit Sub
        ElseIf txtCaptcha.Text = "" Then
            lblNotif.Text = "ERROR : THE CAPTCHA HAS NOT BEEN FILLED"
            lblNotif.ForeColor = Color.Red
            txtCaptcha.Focus()
            Exit Sub
        Else
            If txtCaptcha.Text = CaptchaString Then
                conn.Open()
                Dim myAdapter As New MySqlDataAdapter
                Dim sqlQuery = "SELECT * FROM tbUser WHERE name='" + txtUsername.Text + "'"
                Dim myCommand As New MySqlCommand
                myCommand.Connection = conn
                myCommand.CommandText = sqlQuery
                myAdapter.SelectCommand = myCommand
                Dim myData As MySqlDataReader
                myData = myCommand.ExecuteReader()

                If myData.HasRows = 0 Then
                    MsgBox("The username or password you entered is incorrect. It is also possible that your username is not yet registered.!", vbExclamation, "Gudio Application")
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    txtUsername.Focus()
                    Call GenerateCaptcha()
                Else
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    Me.Hide()
                    frmMain.lblName.Text = "Login As : " & txtUsername.Text
                    frmMain.Show()
                End If
            End If
        End If
    End Sub

    Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        Me.Hide()
        frmSignUp.Show()
    End Sub
Private Sub GenerateCaptcha()
        ProcessNumber = My.Computer.Clock.LocalTime.Millisecond
        If ProcessNumber < 521 Then
            ProcessNumber = ProcessNumber \ 10
            CaptchaString = Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString = CStr(My.Computer.Clock.LocalTime.Second \ 6)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.Second
        If ProcessNumber < 30 Then
            ProcessNumber = Math.Abs(ProcessNumber - 8)
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(My.Computer.Clock.LocalTime.Minute \ 6)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.DayOfYear
        If ProcessNumber Mod 2 = 0 Then
            ProcessNumber = ProcessNumber \ 8
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(ProcessNumber \ 37)
        End If
        TickRandom = My.Computer.Clock.TickCount.ToString
        ProcessNumber = Val(TickRandom.Substring(TickRandom.Length - 1, 1))
        If ProcessNumber Mod 2 = 0 Then
            CaptchaString += CStr(ProcessNumber)
        Else
            ProcessNumber = Math.Abs(Int(Math.Cos(Val(TickRandom)) * 51))
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.Hour
        If ProcessNumber Mod 2 = 0 Then
            ProcessNumber = Math.Abs(Int(Math.Sin(Val(My.Computer.Clock.LocalTime.Year)) * 51))
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(ProcessNumber \ 3)
        End If
CaptchaGraf.Clear(Color.White)
        For hasher As Integer = 0 To 5
            CaptchaGraf.DrawString(CaptchaString.Substring(hasher, 1), DrawingFont, Brushes.Black, hasher * 20 + hasher + ProcessNumber \ 200, (hasher Mod 3) * (ProcessNumber \ 200))
        Next
        picCaptcha.Image = CaptchaImage
    End Sub

SignUp/Register Form
If you have finished typing the code for frmLogin, then we will now continue to code in frmSignUp. As explained above, frmSignUp is for registering new users.

Form Load
When this frmSignUp appears, the system checks the connection to the database. Type the following code:

    Private Sub frmSignUp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GenerateCaptcha()
        CheckConn("localhost", "root", "", "tutorloginreg", "")
        txtUsername.Focus()
    End Sub

SignUp Button
In this code below, it is used to register new users by validating new user data.



 Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        If txtUsername.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtPass.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtConfirmPass.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtCaptcha.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtConfirmPass.Text <> txtPass.Text Then
            lblNotifikasi.Text = "ERROR : PASSWORD DOES NOT MATCH"
            lblNotifikasi.ForeColor = Color.Red
            txtPass.Text = ""
            txtConfirmPass.Text = ""
            txtPass.Focus()
            Exit Sub
        ElseIf txtCaptcha.Text <> CaptchaString Then
            lblNotifikasi.Text = "ERROR : WRONG CAPTCHA"
            lblNotifikasi.ForeColor = Color.Red
            GenerateCaptcha()
            txtCaptcha.Text = ""
            txtCaptcha.Focus()
        Else
            If txtCaptcha.Text = CaptchaString Then
                Dim myAdapter As New MySqlDataAdapter
                Dim sqlQuery = "INSERT INTO tbUser (no, name, password, registerDate) VALUES ('','" & txtUsername.Text & "', '" & txtConfirmPass.Text & "', '" & System.DateTime.Now() & "')"
                Dim myCommand As New MySqlCommand
                myCommand.Connection = conn
                myCommand.CommandText = sqlQuery
                myAdapter.SelectCommand = myCommand
                Dim myData As MySqlDataReader
                myData = myCommand.ExecuteReader()

                If myData.HasRows < 0 Then
                    MsgBox("There is a mistake. contact the administrator", vbCritical, "Gudio Application")
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    txtUsername.Focus()
                    Call GenerateCaptcha()
                Else
                    MsgBox("congratulations, your data has been saved. please login", vbInformation, "Gudio Application")
                    Me.Hide()
                    frmLogin.Show()
                End If

            End If
        End If
    End Sub


Refresh Button Code
Then add the code to call the generate captcha function above on the refresh button. If the refresh button is clicked, the code will be encrypted again. the code is like this:

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GenerateCaptcha()
    End Sub

Full Code SignUp Form 

The following below is the full code from frmSignUp :
Dim DrawingFont As New Font("Arial", 20)
    Dim CaptchaImage As New Bitmap(140, 40)
    Dim CaptchaGraf As Graphics = Graphics.FromImage(CaptchaImage)
    Dim Alphabet As String = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    Dim CaptchaString, TickRandom As String
    Dim ProcessNumber As Integer
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        txtUsername.Text = ""
        txtCaptcha.Text = ""
        txtConfirmPass.Text = ""
        txtPass.Text = ""
        Me.Hide()
        frmLogin.Show()
    End Sub

    Private Sub frmSignUp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GenerateCaptcha()
        CheckConn("localhost", "root", "", "tutorloginreg", "")
        txtUsername.Focus()
    End Sub

    Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        If txtUsername.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtPass.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtConfirmPass.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtCaptcha.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtConfirmPass.Text <> txtPass.Text Then
            lblNotifikasi.Text = "ERROR : PASSWORD DOES NOT MATCH"
            lblNotifikasi.ForeColor = Color.Red
            txtPass.Text = ""
            txtConfirmPass.Text = ""
            txtPass.Focus()
            Exit Sub
        ElseIf txtCaptcha.Text <> CaptchaString Then
            lblNotifikasi.Text = "ERROR : WRONG CAPTCHA"
            lblNotifikasi.ForeColor = Color.Red
            GenerateCaptcha()
            txtCaptcha.Text = ""
            txtCaptcha.Focus()
        Else
            If txtCaptcha.Text = CaptchaString Then
                Dim myAdapter As New MySqlDataAdapter
                Dim sqlQuery = "INSERT INTO tbUser (no, name, password, registerDate) VALUES ('','" & txtUsername.Text & "', '" & txtConfirmPass.Text & "', '" & System.DateTime.Now() & "')"
                Dim myCommand As New MySqlCommand
                myCommand.Connection = conn
                myCommand.CommandText = sqlQuery
                myAdapter.SelectCommand = myCommand
                Dim myData As MySqlDataReader
                myData = myCommand.ExecuteReader()

                If myData.HasRows < 0 Then
                    MsgBox("There is a mistake. contact the administrator", vbCritical, "Gudio Application")
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    txtUsername.Focus()
                    Call GenerateCaptcha()
                Else
                    MsgBox("congratulations, your data has been saved. please login", vbInformation, "Gudio Application")
                    Me.Hide()
                    frmLogin.Show()
                End If

            End If
        End If
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GenerateCaptcha()
    End Sub

Main Form
This form does not contain a lot of code, only displays the username of the user who is logged in. Type the code as below:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
        conn.Close()
        frmLogin.Show()
    End Sub

make it a habit to type the code using your fingers. Don't just copy and paste. The more often you type the program code, the more you will understand about the program code.

So that's the simple application we made in this tutorial. You can develop this application even better.
If this tutorial is useful for you, please share this article and follow me on social media.

If you want to download the code in this tutorial, you can click the link below:


Okay, see you again next time and with other interesting articles or toturials.

Create Login & Register With Captcha on VB.Net


Create Login & Register With Captcha on VB.Net

Create Login & Register With Captcha On VB.Net - In this article, I will give you a tutorial on Creating a Captcha Login and Register on VB.Net. Many applications nowadays, be it a website or desktop using this captcha method, especially in the "Login" and "Registration" section of the user. What for ? in short, its function is to make sure that the login or register is true human and not a bot. For more details, read on this article to the end, so you can understand about the captcha function. And can make applications using the captcha method.

What is A Captcha?

Captcha literally stands for "Completely Automated Public Turing test to tell Computers and Humans Apart" (quoted from Wikipedia), made to distinguish between machines (bots) and humans. The term Captcha was coined by Luis von Ahn, Manuel Blum, and Nicholas J. Hopper of Carnegie MellonUniversity, and John Langford of IBM in 2000.


why should it be distinguished? because a lot of waste "form criminals" aka SPAM COMMENT programs that can just attack our website at any time without "manners" inserting comments on guestbooks / forums whose contents can be promotions or even links to pornographic sites. that's the possibility that will occur on our website if affected above.

Captcha What Is Used / Functions For?

So basically, the main functions of this Captcha are as follows:
  • Prevent spam comment in general the web / blog owner / and others do not just want the website to look good in appearance, quantity but of course quality, but do not know if the admin itself is spam
  • Prevent spam bots, you know that bots cannot read Captcha, because the string string is fixed. It is impossible for Capthca to change every time it can be read unless the bot comes from the engine blog itself
  • Prevent Flood who wants to be on Flood? except for flood like tp if there are many inappropriate words flood, who wants to try? For you who haven't, Flood is sending data over and over again.
  • Protecting Email / User Registration (webmail only) Once there was a Yahoo incident and Google was attacked by a bot that registered thousands of emails! thousands of fake e-mails.
Okay, go directly to the topic of discussion, here I use mysql database as a database processing. I assume you can connect mysql database with vb.net. If it hasn't, you can read this article:

Read : Connection To VB.Net With MySQL.

In this tutorial, we will use 3 forms, namely as:
  1. User Login
  2. User Registration
  3. Main Form

Design Form Login

First open your visual studio first, then create a form with the name "frmLogin",Please it's up to the way you design it, the important thing is that the object is:
Object Name Property QTY Function
Text Box txtUsername, txtCaptcha, txtPass 3 textbox function to hold user data to log in
Label lblUsername, lblPassword, lblNotif 3 lblNotif is used to record errors when a user clicks the login button
Button btnRefresh, btnLogin, btnRegister 3 as a trigger for the code in the application
PictureBox picUser, picPass, picCaptcha 3 so that the application's appearance is more interesting, and picCaptcha is used to display the captcha code
The result is like this:

Create Login & Register With Captcha on VB.Net


Please create your own imagination to design it.


Design Form Sign Up

After creating the login form, then now create the Sign Up form to register the new user. Create a new form named "frmSignUp" and its objects as in the table below:
Object Name Property QTY Function
Text Box txtUsername, txtCaptcha, txtPass, txtConfirmPass 4 textbox function to hold user data to log in
Label lblUsername, lblPassword, lblNotifikasi, lblConfirmPass 4 lblNotif is used to record errors when a user clicks the Register button
Button btnRefresh, btnLogin, btnRegister 3 as a trigger for the code in the application
PictureBox picUser, picPass, picCaptcha, picConfirmPass 4 so that the application's appearance is more interesting, and picCaptcha is used to display the captcha code
The result is like this:

Create Login & Register With Captcha on VB.Net

Then design the last form, we give the name "frmMain". And make the objects like the table below:
Object Name Property QTY Function
Label lblName, lblMessage 2 lblNotif is used to record errors when a user clicks the Register button
Button btnClose 1 to exit this form
The result is like this:


Create Login & Register With Captcha on VB.Net

Make Captcha Login on VB.Net (Coding)

If you have finished doing the design, proceed to the next step, which is to make a module to connect to the mysql database with vb.net.

Create Database
In this tutorial, we will create a database that is used to store user data and new user data. In this tutorial, I use a MySQL database.

Create Login & Register With Captcha on VB.Net

Connection Module
Click the project menu at the top then select the add module, and type the following code:

Module modConnect
    Public conn As New MySql.Data.MySqlClient.MySqlConnection
    Public Sub CheckConn(ByVal server As String, ByVal user As String, ByVal pass As String, ByVal db As String, ByVal port As String)
        Try
            If conn.State = ConnectionState.Closed Then
                conn.ConnectionString = "DATABASE=" & db & ";SERVER = " & server & ";user id=" & user & ";password=" & pass & ";port=" & port & ";charset=utf8"
                conn.Open()
            End If
        Catch ex As MySql.Data.MySqlClient.MySqlException
            MsgBox("Connection to server failed", MsgBoxStyle.Critical, "Gudio Application")
        End Try
    End Sub

    Public Sub disconnect()
        Try
            conn.Open()
        Catch ex As MySql.Data.MySqlClient.MySqlException
        End Try

    End Sub
End Module

Save the code above with the name "modKoneksi" or it's up to you to use any name.

Login Form
If so, double-click on the login form, and on the top line, import mysql data. Next is the code:
Imports MySql.Data.MySqlClient
If the mysql import option does not yet exist, then the method is, click the project menu and select add reference, then search for MySQL data, and click OK. And then for the variable declaration for the object, please type the following code:

Then add the following line of code to the form_load:

    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GenerateCaptcha()
        CheckConn("localhost", "root", "", "tutorloginreg", "")
    End Sub

Variable Declaration
Type the code below at the top of "frmLogin", the code below functions as a function variable declaration to generate the captcha :

    Dim DrawingFont As New Font("Arial", 20)
    Dim CaptchaImage As New Bitmap(140, 40)
    Dim CaptchaGraf As Graphics = Graphics.FromImage(CaptchaImage)
    Dim Alphabet As String = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    Dim CaptchaString, TickRandom As String
    Dim ProcessNumber As Integer

Generate Captcha Function
If so, then create a function to generate the captcha code, like this:

Private Sub GenerateCaptcha()
        ProcessNumber = My.Computer.Clock.LocalTime.Millisecond
        If ProcessNumber < 521 Then
            ProcessNumber = ProcessNumber \ 10
            CaptchaString = Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString = CStr(My.Computer.Clock.LocalTime.Second \ 6)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.Second
        If ProcessNumber < 30 Then
            ProcessNumber = Math.Abs(ProcessNumber - 8)
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(My.Computer.Clock.LocalTime.Minute \ 6)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.DayOfYear
        If ProcessNumber Mod 2 = 0 Then
            ProcessNumber = ProcessNumber \ 8
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(ProcessNumber \ 37)
        End If
        TickRandom = My.Computer.Clock.TickCount.ToString
        ProcessNumber = Val(TickRandom.Substring(TickRandom.Length - 1, 1))
        If ProcessNumber Mod 2 = 0 Then
            CaptchaString += CStr(ProcessNumber)
        Else
            ProcessNumber = Math.Abs(Int(Math.Cos(Val(TickRandom)) * 51))
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.Hour
        If ProcessNumber Mod 2 = 0 Then
            ProcessNumber = Math.Abs(Int(Math.Sin(Val(My.Computer.Clock.LocalTime.Year)) * 51))
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(ProcessNumber \ 3)
        End If
        For hasher As Integer = 0 To 5
            CaptchaGraf.DrawString(CaptchaString.Substring(hasher, 1), DrawingFont, Brushes.Black, hasher * 20 + hasher + ProcessNumber \ 200, (hasher Mod 3) * (ProcessNumber \ 
        200))
        Next
        picCaptcha.Image = CaptchaImage

Refresh Button Code
Then add the code to call the generate captcha function above on the refresh button. If the refresh button is clicked, the code will be encrypted again. the code is like this:

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GenerateCaptcha()
    End Sub

GenerateCaptcha ()
Login Button Code
Then continue by making a code on the login button. This code to validate user data before logging inType the code below on the login button :

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtUsername.Text = "" Then
            lblNotif.Text = "ERROR : INCOMPLETE DATA"
            lblNotif.ForeColor = Color.Red
            txtUsername.Focus()
            Exit Sub
        ElseIf txtPass.Text = "" Then
            lblNotif.Text = "ERROR : INCOMPLETE DATA"
            lblNotif.ForeColor = Color.Red
            txtPass.Focus()
            Exit Sub
        ElseIf txtCaptcha.Text = "" Then
            lblNotif.Text = "ERROR : THE CAPTCHA HAS NOT BEEN FILLED"
            lblNotif.ForeColor = Color.Red
            txtCaptcha.Focus()
            Exit Sub
        Else
            If txtCaptcha.Text = CaptchaString Then
                conn.Open()
                Dim myAdapter As New MySqlDataAdapter
                Dim sqlQuery = "SELECT * FROM tbUser WHERE name='" + txtUsername.Text + "'"
                Dim myCommand As New MySqlCommand
                myCommand.Connection = conn
                myCommand.CommandText = sqlQuery
                myAdapter.SelectCommand = myCommand
                Dim myData As MySqlDataReader
                myData = myCommand.ExecuteReader()

                If myData.HasRows = 0 Then
                    MsgBox("The username or password you entered is incorrect. It is also possible that your username is not yet registered.!", vbExclamation, "Gudio Application")
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    txtUsername.Focus()
                    Call GenerateCaptcha()
                Else
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    Me.Hide()
                    frmMain.lblName.Text = "Login As : " & txtUsername.Text
                    frmMain.Show()
                End If
            End If
        End If
    End Sub

Register Button Code
After that, continue by making a code on the register button. This code is for registering new users. Type the code below:

    Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        Me.Hide()
        frmSignUp.Show()
    End Sub

Full Code Login Form 
The following below is the full code from frmLogin :

Public Class frmLogin
    Dim DrawingFont As New Font("Arial", 20)
    Dim CaptchaImage As New Bitmap(140, 40)
    Dim CaptchaGraf As Graphics = Graphics.FromImage(CaptchaImage)
    Dim Alphabet As String = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    Dim CaptchaString, TickRandom As String
    Dim ProcessNumber As Integer
    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GenerateCaptcha()
        CheckConn("localhost", "root", "", "tutorloginreg", "")
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GenerateCaptcha()
    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtUsername.Text = "" Then
            lblNotif.Text = "ERROR : INCOMPLETE DATA"
            lblNotif.ForeColor = Color.Red
            txtUsername.Focus()
            Exit Sub
        ElseIf txtPass.Text = "" Then
            lblNotif.Text = "ERROR : INCOMPLETE DATA"
            lblNotif.ForeColor = Color.Red
            txtPass.Focus()
            Exit Sub
        ElseIf txtCaptcha.Text = "" Then
            lblNotif.Text = "ERROR : THE CAPTCHA HAS NOT BEEN FILLED"
            lblNotif.ForeColor = Color.Red
            txtCaptcha.Focus()
            Exit Sub
        Else
            If txtCaptcha.Text = CaptchaString Then
                conn.Open()
                Dim myAdapter As New MySqlDataAdapter
                Dim sqlQuery = "SELECT * FROM tbUser WHERE name='" + txtUsername.Text + "'"
                Dim myCommand As New MySqlCommand
                myCommand.Connection = conn
                myCommand.CommandText = sqlQuery
                myAdapter.SelectCommand = myCommand
                Dim myData As MySqlDataReader
                myData = myCommand.ExecuteReader()

                If myData.HasRows = 0 Then
                    MsgBox("The username or password you entered is incorrect. It is also possible that your username is not yet registered.!", vbExclamation, "Gudio Application")
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    txtUsername.Focus()
                    Call GenerateCaptcha()
                Else
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    Me.Hide()
                    frmMain.lblName.Text = "Login As : " & txtUsername.Text
                    frmMain.Show()
                End If
            End If
        End If
    End Sub

    Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        Me.Hide()
        frmSignUp.Show()
    End Sub
Private Sub GenerateCaptcha()
        ProcessNumber = My.Computer.Clock.LocalTime.Millisecond
        If ProcessNumber < 521 Then
            ProcessNumber = ProcessNumber \ 10
            CaptchaString = Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString = CStr(My.Computer.Clock.LocalTime.Second \ 6)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.Second
        If ProcessNumber < 30 Then
            ProcessNumber = Math.Abs(ProcessNumber - 8)
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(My.Computer.Clock.LocalTime.Minute \ 6)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.DayOfYear
        If ProcessNumber Mod 2 = 0 Then
            ProcessNumber = ProcessNumber \ 8
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(ProcessNumber \ 37)
        End If
        TickRandom = My.Computer.Clock.TickCount.ToString
        ProcessNumber = Val(TickRandom.Substring(TickRandom.Length - 1, 1))
        If ProcessNumber Mod 2 = 0 Then
            CaptchaString += CStr(ProcessNumber)
        Else
            ProcessNumber = Math.Abs(Int(Math.Cos(Val(TickRandom)) * 51))
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        End If
        ProcessNumber = My.Computer.Clock.LocalTime.Hour
        If ProcessNumber Mod 2 = 0 Then
            ProcessNumber = Math.Abs(Int(Math.Sin(Val(My.Computer.Clock.LocalTime.Year)) * 51))
            CaptchaString += Alphabet.Substring(ProcessNumber, 1)
        Else
            CaptchaString += CStr(ProcessNumber \ 3)
        End If
CaptchaGraf.Clear(Color.White)
        For hasher As Integer = 0 To 5
            CaptchaGraf.DrawString(CaptchaString.Substring(hasher, 1), DrawingFont, Brushes.Black, hasher * 20 + hasher + ProcessNumber \ 200, (hasher Mod 3) * (ProcessNumber \ 200))
        Next
        picCaptcha.Image = CaptchaImage
    End Sub

SignUp/Register Form
If you have finished typing the code for frmLogin, then we will now continue to code in frmSignUp. As explained above, frmSignUp is for registering new users.

Form Load
When this frmSignUp appears, the system checks the connection to the database. Type the following code:

    Private Sub frmSignUp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GenerateCaptcha()
        CheckConn("localhost", "root", "", "tutorloginreg", "")
        txtUsername.Focus()
    End Sub

SignUp Button
In this code below, it is used to register new users by validating new user data.



 Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        If txtUsername.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtPass.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtConfirmPass.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtCaptcha.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtConfirmPass.Text <> txtPass.Text Then
            lblNotifikasi.Text = "ERROR : PASSWORD DOES NOT MATCH"
            lblNotifikasi.ForeColor = Color.Red
            txtPass.Text = ""
            txtConfirmPass.Text = ""
            txtPass.Focus()
            Exit Sub
        ElseIf txtCaptcha.Text <> CaptchaString Then
            lblNotifikasi.Text = "ERROR : WRONG CAPTCHA"
            lblNotifikasi.ForeColor = Color.Red
            GenerateCaptcha()
            txtCaptcha.Text = ""
            txtCaptcha.Focus()
        Else
            If txtCaptcha.Text = CaptchaString Then
                Dim myAdapter As New MySqlDataAdapter
                Dim sqlQuery = "INSERT INTO tbUser (no, name, password, registerDate) VALUES ('','" & txtUsername.Text & "', '" & txtConfirmPass.Text & "', '" & System.DateTime.Now() & "')"
                Dim myCommand As New MySqlCommand
                myCommand.Connection = conn
                myCommand.CommandText = sqlQuery
                myAdapter.SelectCommand = myCommand
                Dim myData As MySqlDataReader
                myData = myCommand.ExecuteReader()

                If myData.HasRows < 0 Then
                    MsgBox("There is a mistake. contact the administrator", vbCritical, "Gudio Application")
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    txtUsername.Focus()
                    Call GenerateCaptcha()
                Else
                    MsgBox("congratulations, your data has been saved. please login", vbInformation, "Gudio Application")
                    Me.Hide()
                    frmLogin.Show()
                End If

            End If
        End If
    End Sub


Refresh Button Code
Then add the code to call the generate captcha function above on the refresh button. If the refresh button is clicked, the code will be encrypted again. the code is like this:

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GenerateCaptcha()
    End Sub

Full Code SignUp Form 

The following below is the full code from frmSignUp :
Dim DrawingFont As New Font("Arial", 20)
    Dim CaptchaImage As New Bitmap(140, 40)
    Dim CaptchaGraf As Graphics = Graphics.FromImage(CaptchaImage)
    Dim Alphabet As String = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    Dim CaptchaString, TickRandom As String
    Dim ProcessNumber As Integer
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        txtUsername.Text = ""
        txtCaptcha.Text = ""
        txtConfirmPass.Text = ""
        txtPass.Text = ""
        Me.Hide()
        frmLogin.Show()
    End Sub

    Private Sub frmSignUp_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GenerateCaptcha()
        CheckConn("localhost", "root", "", "tutorloginreg", "")
        txtUsername.Focus()
    End Sub

    Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
        If txtUsername.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtPass.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtConfirmPass.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtCaptcha.Text = "" Then
            lblNotifikasi.Text = "ERROR : INCOMPLETE DATA"
            lblNotifikasi.ForeColor = Color.Red
            Exit Sub
        ElseIf txtConfirmPass.Text <> txtPass.Text Then
            lblNotifikasi.Text = "ERROR : PASSWORD DOES NOT MATCH"
            lblNotifikasi.ForeColor = Color.Red
            txtPass.Text = ""
            txtConfirmPass.Text = ""
            txtPass.Focus()
            Exit Sub
        ElseIf txtCaptcha.Text <> CaptchaString Then
            lblNotifikasi.Text = "ERROR : WRONG CAPTCHA"
            lblNotifikasi.ForeColor = Color.Red
            GenerateCaptcha()
            txtCaptcha.Text = ""
            txtCaptcha.Focus()
        Else
            If txtCaptcha.Text = CaptchaString Then
                Dim myAdapter As New MySqlDataAdapter
                Dim sqlQuery = "INSERT INTO tbUser (no, name, password, registerDate) VALUES ('','" & txtUsername.Text & "', '" & txtConfirmPass.Text & "', '" & System.DateTime.Now() & "')"
                Dim myCommand As New MySqlCommand
                myCommand.Connection = conn
                myCommand.CommandText = sqlQuery
                myAdapter.SelectCommand = myCommand
                Dim myData As MySqlDataReader
                myData = myCommand.ExecuteReader()

                If myData.HasRows < 0 Then
                    MsgBox("There is a mistake. contact the administrator", vbCritical, "Gudio Application")
                    txtUsername.Text = ""
                    txtPass.Text = ""
                    txtCaptcha.Text = ""
                    txtUsername.Focus()
                    Call GenerateCaptcha()
                Else
                    MsgBox("congratulations, your data has been saved. please login", vbInformation, "Gudio Application")
                    Me.Hide()
                    frmLogin.Show()
                End If

            End If
        End If
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        GenerateCaptcha()
    End Sub

Main Form
This form does not contain a lot of code, only displays the username of the user who is logged in. Type the code as below:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
        conn.Close()
        frmLogin.Show()
    End Sub

make it a habit to type the code using your fingers. Don't just copy and paste. The more often you type the program code, the more you will understand about the program code.

So that's the simple application we made in this tutorial. You can develop this application even better.
If this tutorial is useful for you, please share this article and follow me on social media.

If you want to download the code in this tutorial, you can click the link below:


Okay, see you again next time and with other interesting articles or toturials.
iklan related post
Load comments