Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

checking existence of certain characters in a string

Hi Experts,

I'm creating a table of users passwords and would like to ensure each password contains at least one digit, one uppercase and one lowercase letter, how do I check for that within a string?
Avatar of bfuchs
bfuchs
Flag of United States of America image

ASKER

Actually I did find something on the web..

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/walkthrough-validating-that-passwords-are-complex

However I'm having problems deploying it..(see attached).

Perhaps I need to add some special references?

Thanks,
Ben
Untitled.png
did you add reference as

Imports System.Text.RegularExpressions

Open in new window

Avatar of bfuchs

ASKER

Hi Huseyin,

Not sure how to go about that, as I usually add references thru the tools menu, see attached.

Thanks,
Ben
Untitled.png
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada 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
Avatar of bfuchs

ASKER

first, add reference to "Microsoft VBScript Regular Expressions" library
As you can see on attached, I have that already.
FYI- This is to be used within an Access ADP project/MDB file as well, can you guide me?

Thanks,
Ben
try this

Dim rex As RegExp
Set rex = New RegExp
rex.Pattern = "^(?=.*[A-Za-z])(?=.*\d)(?=.*[\_+$@$!%*'#?&])[A-Za-z\d\_$+@$!%*'#?&]{8,}$$"
ValidatePassword=rex.Test(pwd)

Open in new window


use this

^(?=.*[A-Za-z])(?=.*\d)(?=.*[\_+$@$!%*'#?&])[A-Za-z\d\_$+@$!%*'#?&]{8,}$

Open in new window


https://regex101.com/

to test...

8 at the end is min length...
min 1 upper, min  1 lower, min 1 special character needed...
in this forum,

https://stackoverflow.com/questions/19605150/regex-for-password-must-be-contain-at-least-eight-characters-at-least-one-numbe

you can find lots of regex expression for different purposes...
just grab one that fits to your needs...
test it @ regex101.com
then use it in your function...
Avatar of bfuchs

ASKER

OK following your initial way I got it to work for a single occurance, as follows
Public Function ValidatePassword(ByVal pwd As String, Optional ByVal minLength As Integer = 8, Optional ByVal numUpper As Integer = 2, Optional ByVal numLower As Integer = 2, Optional ByVal numNumbers As Integer = 2, Optional ByVal numSpecial As Integer = 2) As Boolean

    ' Replace [A-Z] with \p{Lu}, to allow for Unicode uppercase letters.
    Dim rex As RegExp
Set rex = New RegExp
rex.Pattern = "[^A-Z]"
If Not rex.Test(pwd) Then
    ValidatePasswrod = False
    Exit Function
End If
rex.Pattern = "[a-z]"
If Not rex.Test(pwd) Then
    ValidatePasswrod = False
    Exit Function
End If
rex.Pattern = "[0-9]"
If Not rex.Test(pwd) Then
    ValidatePasswrod = False
    Exit Function
End If

ValidatePassword = True

Open in new window

Now how do I convert the following?
'''    If upper.Matches(pwd).Count < numUpper Then Return False

Open in new window


Thanks,
Ben
with above structure, use these...

rex.Pattern = "[a-z]" ' use one of below
If Not rex.Test(pwd) Then
    ValidatePasswrod = False
    Exit Function
End If

Open in new window


1 upper: [A-Z]
1 lower: [a-z]
1 number: [0-9]
1 special listed: [\_+$@$!%*'#?&]

Open in new window


or all in one shot like ID: 42202572
Avatar of bfuchs

ASKER

Not sure I got it.

Basically I need to know how do I get a number of occurrences, lets say we need 2 Upper Case letters, how do I check for that?

Thanks,
Ben
you should use this. 2 is the number of occurrence...

^(.*?[A-Z]){2,}

Open in new window


and change [A-Z] with [a-z], [0-9], and list of special chars to test the others...
Avatar of bfuchs

ASKER

Great, this seems to work!
Just wondering, besides the 2 and the A-Z, what is the meaning of the other characters?

Thanks,
Ben
Avatar of bfuchs

ASKER

Thank you Huseyin!!
just go to https://regex101.com/
put some regex here and test it...
it will explain everything there...
or using vb function and loop :)

dim U as string = "ABC...Z"
dim L as string = "abc...z"
dim N as string = "01..9"
dim S as string = "?=-!..."

dim ux, lx, nx, sx as int
for i=0 to Lengh(pwd)
  dim c as string = substring(pwd,i,1)
  if U.indexOf(c)>=0 then ux=ux+1
  if L.indexOf(c)>=0 then lx=lx+1
  if N.indexOf(c)>=0 then nx=nx+1
  if S.indexOf(c)>=0 then sx=sx+1
next

if ux<2 then ...
if lx<2 then ...
if nx<1 then ...
if sx<1 then ...

Open in new window


* not tested... shows just the logic...
The prior comment is VB.Net code.  You would have to change it to run in a VBA environment.
This comment submitted post-closure.
Here is a VBA-specific function that does not require a reference.
Function IsValidPwd(ByVal parmPwd As String) As Boolean
    'The Regex pattern enforces the following password rules:
    '1. must contain two or more upper case letters
    '2. must contain one or more lower case letters
    '3. must contain one or more numeric digits
    '4. must contain one or more non-alphanumeric/non-space characters (special chars)
    '5. password must be 8 or more characters long
    
    Static oRE As Object

    If oRE Is Nothing Then
        Set oRE = CreateObject("vbscript.regexp")
        oRE.Global = True
        oRE.Pattern = "^(?=(?:.*[A-Z]){2,})(?=(?:.*[a-z]){1,})(?=(?:.*\d){1,})(?=(?:.*[^ 0-9A-Za-z]){1,}).{8,}$"
    End If

    IsValidPwd = oRE.test(parmPwd)

End Function

Open in new window

If you are dealing with a nullable password field, then concatenate an empty string to the password field value when invoking this function.
Avatar of bfuchs

ASKER

@aikimark
Here is a VBA-specific function that does not require a reference.
Thanks for that tip, will probably use that.
Ben