Avatar of Murray Brown
Murray Brown
Flag 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
Microsoft AccessVBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Murray Brown

8/22/2022 - Mon
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.
John Tsioumpris

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
ste5an

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
John Tsioumpris

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/
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Subodh Tiwari (Neeraj)

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

Murray Brown

ASKER
Thanks