Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VBA Validate password string

Hi
I have to write validation code for a password string
It must be at least 8 long , have at least one upper case and one lower case letter, at least one number and at least one symbol.
What VBA code would I use to do this?
Thanks
Avatar of Daniel Pineault
Daniel Pineault

There are different ways of doing this: looping through character by character or using Instr()...  Take a look at

https://access-programmers.co.uk/forums/showthread.php?t=283490
https://stackoverflow.com/questions/40133375/vba-validate-if-my-password-contais-1-number-1-ucase-and-1-lcase
each illustrate one of these approaches.
Here is an example for symbol checking
Public Function checkSymbol(InputString As String) As Boolean
Dim blnChecked As Boolean
blnChecked = False
For i = 1 To Len(InputString)
If Asc(Mid(InputString, i, 1)) >= 32 And Asc(Mid(InputString, i, 1)) <= 47 Then
blnChecked = True
Exit For
End If

Next
checkSymbol = blnChecked
End Function

Open in new window

SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Revised my code because i skipped some symbols :)
Public Function checkSymbol(InputString As String) As Boolean
Dim i As Integer
Dim blnChecked As Boolean
blnChecked = False
For i = 1 To Len(InputString)
If (Asc(Mid(InputString, i, 1)) >= 32 And Asc(Mid(InputString, i, 1)) <= 47) _
Or (Asc(Mid(InputString, i, 1)) >= 58 And Asc(Mid(InputString, i, 1)) <= 64) _
Or (Asc(Mid(InputString, i, 1)) >= 91 And Asc(Mid(InputString, i, 1)) <= 96) _
Or (Asc(Mid(InputString, i, 1)) >= 123 And Asc(Mid(InputString, i, 1)) <= 126) _
Then
blnChecked = True
Exit For
End If

Open in new window


I used that for ASCII codes : https://theasciicode.com.ar/
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This Function will validate a password with at least...
One upper case letter
One lower case letter
One digit
One symbol (any)
And if the Password is at least 8 characters long and have max 15 characters.

Function ValidPassword(ByVal str As String) As Boolean

With CreateObject("VBScript.RegExp")
    .Global = False
    .Pattern = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W)[\w\d\W]{8,15}$"
    ValidPassword = .Test(str)
End With

End Function

Open in new window


The following code validates different passwords as below...

Sub TestPasswords()
Dim pwArr As Variant
Dim pw As Variant

pwArr = Array("Aa8*DEFGHIJ", "murray_ee_2019", "Murray_EE*2019", "HELLO123_*", "hello world!", "-*/+Aa123  ", "aA123_#+", "~@#%^&()[]Aa1")

For Each pw In pwArr
    If ValidPassword(pw) Then
        MsgBox "'" & pw & "' is good Password."
    Else
        MsgBox "'" & pw & "' is bad password."
    End If
Next pw
End Sub

Open in new window

Avatar of Murray Brown

ASKER

Thanks