PDA

View Full Version : Password creation



mirko
01-29-2008, 07:13 PM
I am trying to create a program that asks for a password and checks if the password is 8 characters long, the first character is an uppercase letter and the rest are either uppercase letters or digits.
I have some nested if statements and a loop but can't figure out what I am doing wrong.

Here is what I have:

Sub getPasswords()

Dim isValid As Boolean
Dim isNewPassword As Boolean
Dim response As String
Dim password1 As String
Dim password2 As String
Dim i As Integer

' Ask for the password and embed in a Do loop
isValid = False
Do
password1 = InputBox("Enter a password that is 8 characters long, starts with an uppercase letter and" & _
" consists only of uppercase letters and digits")

' Check for the length of the password first and proceed to next IF statement if it is true
If Len(password1) = 8 Then
'Check if the first character is Uppercase and proceed to next IF statement if it is true
If Left(password1, 1) = UCase(Left(password1, 1)) Then
'Check whether the other characters are upper case or numbers
For i = 2 To 8
If Left(password1, i) = UCase(Left(password1, i)) Or IsNumeric(Left(password1, i)) = True Then
i = i + 1
Else
MsgBox "This is not a valid password"
Exit For
End If
Next
isValid = True
End If

End If
i = i + 1
If isValid = False Then MsgBox ("This is not a valid password")
Loop Until isValid
MsgBox ("Congs")

End Sub


I appreciate your help!

herzberg
01-29-2008, 09:17 PM
When you need to advance through a text string, the function to use is Mid(), and not Left(). The increment you used in your code for Left() only increases the length of the string returned by the function, and not advancing to the next character.

Below is what I would do. Oh, I have a (bad) habit of putting unkind messages in the message boxes. :*) So like you'll have to amend them to suit your needs.

Sub getPasswords()

Dim isValid As Boolean
Dim isNewPassword As Boolean
Dim response As String
Dim password1 As String
Dim password2 As String
Dim i As Integer

'Additional variables
Dim InputLength As Long

' Ask for the password and embed in a Do loop
isValid = False

StartLoop:
Do While isValid = False

password1 = InputBox("Enter a password that is 8 characters long, starts with an uppercase letter and" & _
" consists only of uppercase letters and digits")

InputLength = Len(password1)

' Check for the length of the password first and proceed to next IF statement if it is true
If InputLength = 8 Then

'Check if the first character is Uppercase and proceed to next IF statement if it is true
If Left(password1, 1) = UCase(Left(password1, 1)) Then

'Check whether the other characters are upper case or numbers
For i = 2 To 8
'Amended the check below and included a restart for the loop
If Mid(password1, i, 1) = UCase(Mid(password1, i, 1)) Or _
IsNumeric(Mid(password1, i, 1)) = True Then
isValid = True
Else
MsgBox "You obviously do not understand instructions."
GoTo StartLoop
End If
Next

Else
MsgBox "You obviously do not understand instructions."
End If

Else
MsgBox "You obviously do not understand instructions."
End If
Loop

If isValid = True Then MsgBox "Password accepted"

End Sub

mikerickson
01-30-2008, 07:27 AM
If password1 Like "[A-Z]" & Application.Rept("[a-z0-9A-Z]", 7) Then
MsgBox "good password"
Else
MsgBox "bad password"
End If