Murray Brown
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
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Revised my code because i skipped some symbols :)
I used that for ASCII codes : https://theasciicode.com.ar/
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
I used that for ASCII codes : https://theasciicode.com.ar/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
The following code validates different passwords as below...
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
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
ASKER
Thanks
https://access-programmers
https://stackoverflow.com/
each illustrate one of these approaches.