Friday, April 15, 2022

IsValidPasswordString Function

'Following function will verify if the password string contains following characters or not?

Rem : List of Characters Group - ASCII
Rem : Group 1 : A-Z : 65-90
Rem : Group 2 : a-z : 97-122
Rem : Group 3 : 0-9 : 48-57
Rem : Group 4 : Special Characters Group 1 : 33-47
Rem : Group 5 : Special Characters Group 2 : 58-64
Rem : Group 6 : Special Characters Group 3 : 91-96
Rem : Group 7 : Special Characters Group 4 : 123-126

'Criterias of checking
'Minimum 8 Length
'At least One Capital
'At least One Small Character
'At least One Numeric
'At least One Special Character

Public Function IsValidPasswordString(ByVal passwordString As String) As Boolean
    
    'Variable declarations
    Dim txt                     As String
    Dim passwordCharacters()    As String
    Dim passwordCharacter       As Variant
    Dim testCapital             As Boolean
    Dim testLower               As Boolean
    Dim testNumber              As Boolean
    Dim testSpecialCharacter    As Boolean
    
    'Check for the Minimum Length Criteria
    If VBA.Len(passwordString) >= 8 Then
        IsValidPasswordString = True
    Else
        IsValidPasswordString = False
        Exit Function
    End If
    
    'Check rest of the conditions
    
    'Get characters in array
    txt = VBA.StrConv(passwordString, vbUnicode)
    passwordCharacters = VBA.Split(VBA.Left(txt, Len(txt) - 1), VBA.Chr$(0))
    
    'Get testified
    For Each passwordCharacter In passwordCharacters
        Select Case VBA.AscW(passwordCharacter)
            Case 65 To 90                                   'A-Z : 65-90
                testCapital = True
            Case 97 To 122                                  'a-z
                testLower = True
            Case 48 To 57                                   '0-9
                testNumber = True
            Case 33 To 47, 58 To 64, 91 To 96, 123 To 126   'Special Characters
                testSpecialCharacter = True
        End Select
    Next passwordCharacter
    
    'final conclusion
    If (testCapital And testLower And testNumber And testSpecialCharacter) Then
        IsValidPasswordString = True
    Else
        IsValidPasswordString = False
    End If
    
End Function

No comments:

Post a Comment

IsValidPasswordString Function

'Following function will verify if the password string contains following characters or not? Rem : List of Characters Group - ASCII Rem ...