Android Question Strong Password

Prosg

Active Member
Licensed User
Longtime User
Hello,

Is someone have a function to check if user create account with a strong password like

  • Minimum 8 characters in length
  • Contains 3/4 of the following items:
    - Uppercase Letters
    - Lowercase Letters
    - Numbers
    - Symbols
Ty
 

KMatle

Expert
Licensed User
Longtime User
It's as easy as you write:

Define some global integers (PW_MIN_LENGTH, PW_MIN_UPPER, PW_MIN_LOWER, ....)

then call a Sub like

B4X:
If CheckPW (MyPW) = "OK" Then
   ...
Else
  "Display error message"
End If
B4X:
Sub CheckPW (PW as String)

If PW.length >= MIN_PW_LENGHT Then....
   Return "OK"
Else
   Return "PW length must be at least " & PW_MIN_Length
End If 

....

and so on...
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Maybe you can also use RegEx matcher:
B4X:
Log(IsValidPwd("aRFI765$m"))   'returns true
Log(IsValidPwd("RFI76@$U"))     'returns false because no lower case

B4X:
Sub IsValidPwd(pwd As String) As Boolean    'for strong password
  Dim m As Matcher
    'At least 8 chrs with: any combination of lower, upper, special chr, digits (at least 1 of each)
    m = Regex.Matcher("(?=^.{8,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$&*]).*$", pwd)   
    If m.Find = False Then Return False
    Return True
End Sub
 
Upvote 0

Prosg

Active Member
Licensed User
Longtime User
If i would be perfect and if i know regular expression i will do this with your function :

B4X:
Sub IsValidPwd(pwd As String, level as int) As Boolean    'for strong password
  Dim m As Matcher
    'At least 8 chrs with: any combination of lower, upper, special chr, digits (at least 1 of each)

Select case lvl

Case 1 ' only 8 chrs
m =  ..........
Case 2 ' 8 chrs / 1 lower 1 upper
m =  ..........
Case 3 8 chrs / 1 lower 1 upper / 1 digits
m =  ..........
Case 4 8 chrs / 1 lower 1 upper / 1 digits / 1 Special Chr
    m = Regex.Matcher("(?=^.{8,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$&*]).*$", pwd) 

End select
    If m.Find = False Then Return False
    Return True
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I think this should do it for you, It took me a while. I tested quite extensively, but please test thoroughly. RegEx is weird.
B4X:
Log(IsValidPwd("weru4cFrA",1))   'false because 9 chrs
    Log(IsValidPwd("weru4cFrA",2))   'false because it has a digit, needs lower and upper
    Log(IsValidPwd("weru4cFr",3))    'true at least 1 lower, 1 upper, 1 digit
    Log(IsValidPwd("weru4cFr$",4))   'true because it has at least 8 chrs / 1 lower 1 upper / 1 digits / 1 Special Chr
B4X:
Sub IsValidPwd(pwd As String, level As Int) As Boolean    'for strong password
    Dim m As Matcher
    Select  level
        Case 1 ' only 8 chrs
          m = Regex.Matcher("(?=^.{8}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$&*]).*$", pwd)
        Case 2 'at least 8 chrs / 1 lower 1 upper
            m = Regex.Matcher("(?=^.{8,}$)(?=.*[A-Z])(?=.*[a-z])(?=.[^<>%$#]*$)(^\D*$).*$", pwd)
        Case 3 'at least 8 chrs / 1 lower 1 upper / 1 digits
            m = Regex.Matcher("(?=^.{8,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.[^<>%$#]*$).*$", pwd)
        Case 4 'at least 8 chrs / 1 lower 1 upper / 1 digits / 1 Special Chr
           m = Regex.Matcher("(?=^.{8,}$)(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$&*]).*$", pwd)
    End Select
    If m.Find = False Then Return False
    Return True
End Sub
 
Upvote 0
Top