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!
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!