password validation

x18access

Registered User.
Local time
Today, 05:26
Joined
Jan 5, 2014
Messages
14
could someone smart enough either add annotations, or edit this code, so that it tells the user WHICH error has been made.
I can't figure it out
thank you very much!
I am looking at the public function routine, that validates the password entry. I want to know how i can make a message pop up with the specific error the user has made on entry.
regards!



Public Function ValidatePwd(varPassword As Variant) As Boolean
Dim blnValid As Boolean
Dim blnValidCriteria As Boolean
Dim intChar As Integer

blnValid = Len("" & varPassword) >= 4 And Len("" & varPassword) <= 12
If blnValid Then
blnValidCriteria = False
For intChar = 1 To Len("" & varPassword)
If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVW", Mid(varPassword, intChar, 1), vbBinaryCompare) > 0 Then
blnValidCriteria = True
Exit For
End If
Next
blnValid = blnValidCriteria
End If
If blnValid Then
blnValidCriteria = False
MsgBox "Please enter an upper case letter!"
For intChar = 1 To Len("" & varPassword)
If InStr(1, "abcdefghijklmnopqrstuvw", Mid(varPassword, intChar, 1), vbBinaryCompare) > 0 Then
blnValidCriteria = True
Exit For
End If
Next
blnValid = blnValidCriteria
End If
If blnValid Then
blnValidCriteria = False
MsgBox "Please enter a lower case letter"
For intChar = 1 To Len("" & varPassword)
If InStr(1, "0123456789", Mid(varPassword, intChar, 1), vbBinaryCompare) > 0 Then
blnValidCriteria = True
Exit For
End If
Next
blnValid = blnValidCriteria
End If
ValidatePwd = blnValid
End Function


Private Sub txtPassword_BeforeUpdate(Cancel As Integer)
Cancel = Not ValidatePwd(Me.txtPassword)
If Cancel Then
MsgBox Me.txtPassword.ValidationText, vbInformation
End If
End Sub
 
It is annoying to try to understand a code presented as yours.
Use code tags (the # button) in order to show the code as you see.

My next step is to analyze this code.

Code:
Private Sub txtPassword_BeforeUpdate(Cancel As Integer)
    Cancel = Not ValidatePwd(Me.txtPassword)
    If Cancel Then
        MsgBox Me.txtPassword.ValidationText, vbInformation
    End If
End Sub

Public Function ValidatePwd(varPassword As Variant) As Boolean
Dim blnValid As Boolean
Dim blnValidCriteria As Boolean
Dim intChar As Integer

    blnValid = Len("" & varPassword) >= 4 And Len("" & varPassword) <= 12
    
    If blnValid Then
        blnValidCriteria = False
        For intChar = 1 To Len("" & varPassword)
            If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVW", Mid(varPassword, intChar, 1), vbBinaryCompare) > 0 Then
                blnValidCriteria = True
        Exit For
            End If
        Next intChar
        blnValid = blnValidCriteria
    End If
    
    If blnValid Then
        blnValidCriteria = False
        MsgBox "Please enter an upper case letter!"
        For intChar = 1 To Len("" & varPassword)
            If InStr(1, "abcdefghijklmnopqrstuvw", Mid(varPassword, intChar, 1), vbBinaryCompare) > 0 Then
                blnValidCriteria = True
        Exit For
            End If
        Next intChar
        blnValid = blnValidCriteria
    End If
    
    If blnValid Then
        blnValidCriteria = False
        MsgBox "Please enter a lower case letter"
        For intChar = 1 To Len("" & varPassword)
            If InStr(1, "0123456789", Mid(varPassword, intChar, 1), vbBinaryCompare) > 0 Then
                blnValidCriteria = True
        Exit For
            End If
        Next intChar
        
        blnValid = blnValidCriteria
    End If
    
    ValidatePwd = blnValid
End Function
 
... so, try this:
Code:
Private Sub txtPassword_BeforeUpdate(Cancel As Integer)
    Cancel = Not ValidatePwd(Me.txtPassword)
End Sub

Public Function ValidatePwd(varPassword As Variant) As Boolean
Dim intChar As Integer
    
    ValidatePwd = False
    
    If Not (Len("" & varPassword) >= 4 And Len("" & varPassword) <= 12) Then
        MsgBox ("Len psw should be at least 4 letters but no more 12 letters")
Exit Function
    End If
    
    If (UCase(varPassword) = varPassword) Or (LCase(varPassword) = varPassword) Then
        MsgBox ("The password should contain letters in upper case AND in lower case")
Exit Function
    End If
    
    For intChar = 1 To Len(varPassword)
        If InStr(1, "0123456789", Mid(varPassword, intChar, 1), vbBinaryCompare) > 0 Then
            ValidatePwd = True
Exit Function
        End If
    Next intChar
    MsgBox ("The password should contain at least one number")
    
End Function
 
Hey Mihail

thanks for the great help.

The system seems to return an error for the upper case, lowercase part. No MATTER what the input. Could anyone help with this?

thank you very much


Regards
 
The system seems to return an error for the upper case, lowercase part. No MATTER what the input. Could anyone help with this?
What mean "seems" ? Have you an error or not ?

No. For sure no one can help if you don't show (explain) us what happened.

If you have an error, show us the error number and the error description.

I think that is a matter of references, but I can't be sure.
 
Mihail, apologies for the lack of clarity.

the error, whatever password i type in is :
"The password should contain letters in upper case AND in lower case"


This makes me believe the function is not using the piece of code to validate this.
Why would their be an error here?

regards
 
The problem is this line:

Code:
If (UCase(varPassword) = varPassword) Or (LCase(varPassword) = varPassword) Then

By default VBA is case insensitive. Both these tests will always return True.

That is why the original code uses:
Code:
If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVW", Mid(varPassword, intChar, 1), vbBinaryCompare) > 0 Then
The case sensiitve comparison is forced with the last parameter, vbBinaryCompare.
 
The problem line of Mihail's code could be replaced with:
Code:
 If (InStr(varPassword, UCase(varPassword), vbCompareBinary) Or InStr(varPassword, LCase(varPassword), vbCompareBinary) Then
 
The problem line of Mihail's code could be replaced with:
Code:
 If (InStr(varPassword, UCase(varPassword), vbCompareBinary) Or InStr(varPassword, LCase(varPassword), vbCompareBinary) Then
Ups. Thank you Galaxiom. I don't tried but I'm sure you are right.

@x18
Sorry.
This was an "in air" code (not tested)
 
I would really encourage you to take a closer look at my function at the link. It doesn't set the maximum length but could be very easily changed to include it.

It is a quite sophisticated and highly configurable implementation and worth taking the time to understand the code. Just read the inbuilt documentation.

It looks complicated at first because of the logical AND and OR but it is really quite simple once you understand the binary construction of the summable arguments.
 

Users who are viewing this thread

Back
Top Bottom