Password Validation Using Visual Basic
Submitted by thuhChief on Thursday, August 1, 2013 - 13:27.
The program allows a user login up to three (3) times. After three unsuccessful attempts, the program exits.
Properties of the Login Form:
Login buttonName: cmdValidClear button
Name: cmdClearCancel button
Name: cmdExitTextbox
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:
- Public Class frmPassword
- Dim ErrorCount As Integer
- Private Sub frmPassword_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- ErrorCount = 0
- End Sub
- Private Sub cmdExit_Click(sender As System.Object, e As System.EventArgs) Handles cmdExit.Click
- End Sub
- Private Sub cmdValid_Click(sender As System.Object, e As System.EventArgs) Handles cmdValid.Click
- 'Checks to see if the value entered in the textbox field is the same as that saved in the 'tag property
- If txtPassword.Text = txtPassword.Tag Then
- 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.
- txtPassword.Clear() 'Clears the characters in the textbox
- Else
- 'Everytime login is not successful, the error count is incremented by 1
- ErrorCount = ErrorCount + 1
- 'Allows the user have another attempt at login
- MessageBox.Show("Incorrect password", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
- txtPassword.Clear()
- 'Positions the cursor in the textbox
- txtPassword.Focus()
- End If
- '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
- If (ErrorCount = 1) Then
- lblNotify.Text() = "You have 2 login attempts left"
- '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
- ElseIf (ErrorCount = 2) Then
- lblNotify.Text() = "You have 1 login attempt left"
- 'If login was not successful for the third time, a message box appears alerting the user 'that they have no more login attempts left
- ElseIf (ErrorCount = 3) Then
- MessageBox.Show(" You have exceeded the maximum login attempts. System is now exiting. ", " Error! ", MessageBoxButtons.OK, MessageBoxIcon.Error)
- 'The system will then exit after the message box is closed
- Application.Exit()
- End If
- End Sub
- Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
- 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.
- End Sub
- End Class
Comments
Add new comment
- Add new comment
- 3356 views