Skip to main content

Password Validation Using Visual Basic

The program allows a user login up to three (3) times. After three unsuccessful attempts, the program exits.

Properties of the Login Form:

Login button

Name: cmdValid

Clear button

Name: cmdClear

Cancel button

Name: cmdExit


Name: txtPassword
Tag: [Enter the password as the value of this property]
PasswordChar: *

Label (at the bottom)

Name: lblNotify
Name: "You only have 3 login attempts"

The code:

  1. Public Class frmPassword
  2. Dim ErrorCount As Integer
  4. Private Sub frmPassword_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  5. ErrorCount = 0
  6. End Sub
  8. Private Sub cmdExit_Click(sender As System.Object, e As System.EventArgs) Handles cmdExit.Click
  9. Me.Close() 'Handles manual exit from the program by the user
  10. End Sub
  12. Private Sub cmdValid_Click(sender As System.Object, e As System.EventArgs) Handles cmdValid.Click
  14. 'Checks to see if the value entered in the textbox field is the same as that saved in the 'tag property
  15. If txtPassword.Text = txtPassword.Tag Then
  16. MessageBox.Show("You have logged in successfully", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 'If the password entered matches the 'tag property, a message box will be displayed to the user informing them that the login 'was successful.
  17. txtPassword.Clear() 'Clears the characters in the textbox
  18. Else
  19. 'Everytime login is not successful, the error count is incremented by 1
  20. ErrorCount = ErrorCount + 1
  22. 'Allows the user have another attempt at login
  23. MessageBox.Show("Incorrect password", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
  25. txtPassword.Clear()
  27. 'Positions the cursor in the textbox
  28. txtPassword.Focus()
  29. End If
  31. 'If login was not successful at the first time, the value of the lblNotify is changed to 'alert the user that they have two more attempts left
  32. If (ErrorCount = 1) Then
  33. lblNotify.Text() = "You have 2 login attempts left"
  35. 'If login was not successful for the second time, the value of the lblNotify is changed to alert the user that they have one more attempts left
  37. ElseIf (ErrorCount = 2) Then
  38. lblNotify.Text() = "You have 1 login attempt left"
  40. 'If login was not successful for the third time, a message box appears alerting the user 'that they have no more login attempts left
  41. ElseIf (ErrorCount = 3) Then
  42. MessageBox.Show(" You have exceeded the maximum login attempts. System is now exiting. ", " Error! ", MessageBoxButtons.OK, MessageBoxIcon.Error)
  44. 'The system will then exit after the message box is closed
  45. Application.Exit()
  46. End If
  47. End Sub
  49. Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
  50. txtPassword.Clear() 'Clears the characters in the textbox field in case the user 'realizes they made a typo while typing in the password before clicking on the login 'button.
  51. End Sub
  52. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted byCatherine Cuyos (not verified)on Thu, 08/07/2014 - 13:54

Nakakainis ayaw magrun sa MSVisual Basic 2010

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.