Password which consists capital letter, number, small letter-VBForums
Results 1 to 17 of 17

Thread: Password which consists capital letter, number, small letter

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    53

    Password which consists capital letter, number, small letter

    Hi,

    I want to have a password which consists capital letters, small letters, numbers and special characters. How can I make it?

    Thanks for replying

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Password which consists capital letter, number, small letter

    On the assumption it has to be 8 characters or more in length and must have at least: one Capital Letter, one Lower Case Letter, one Nmber and one Special Character:
    Code:
    Private Sub cmdChekPWD_Click()
    Dim bytFlag As Byte
    Dim intI As Integer
    Dim strC As String
    If Len(txtPassword.Text) >= 8 Then
        For intI = 1 To Len(txtPassword.Text)
            strC = Mid$(txtPassword.Text, intI, 1)
            If strC >= "!" And strC <= "\" Then
                bytFlag = bytFlag Or &H8
            End If
            If strC >= "0" And strC <= "9" Then
                bytFlag = bytFlag Or &H4
            End If
            If strC >= ":" And strC <= "@" Then
                bytFlag = bytFlag Or &H8
            End If
            If strC >= "A" And strC <= "Z" Then
                bytFlag = bytFlag Or &H2
            End If
            If strC >= "a" And strC <= "z" Then
                bytFlag = bytFlag Or &H1
            End If
        Next intI
    End If
    If bytFlag <> &HF Then
        MsgBox "Invalid Password"
    End If
    End Sub

  3. #3
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    2,372

    Re: Password which consists capital letter, number, small letter

    "must have at least: one Capital Letter, one Lower Case Letter, one Number and one Special Character"

    Unless I misunderstand the requirements it looks like Axxxxx111 passes the test but does not have any special characters.

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Password which consists capital letter, number, small letter

    Try this:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        If Not CheckPassword("abcdefg123") Then
            MsgBox "Invalid password - it must contain at least 1 capital letter, 1 lower and 1 numeric character.", _
                   vbExclamation, "Invalid Password"
            Exit Sub
        Else
            'proceeed with normal processing...
            Debug.Print "password is ok..."
        End If
    End Sub
    
    Private Function CheckPassword(sPwd As String) As Boolean
    Dim i As Integer
    Dim bCapital As Boolean
    Dim bLower As Boolean
    Dim bNumberic As Boolean
    Dim iAscii As Integer
    
        For i = 1 To Len(sPwd)
            iAscii = Asc(VBA.Mid$(sPwd, i, 1))
            If Not bCapital Then
                bCapital = CBool(iAscii >= 65 And iAscii <= 90)
            End If
            If Not bLower Then
                bLower = CBool(iAscii >= 97 And iAscii <= 122)
            End If
            bNumberic = IsNumeric(VBA.Mid$(sPwd, i, 1))
            
            If bCapital And bLower And bNumberic Then Exit For
        Next i
        
        CheckPassword = (bCapital And bLower And bNumberic)
    
    End Function

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    3,972

    Re: Password which consists capital letter, number, small letter

    Regular expression can be used here also.
    Code:
    Private Function ValidatePassword(ByVal sPass) As Boolean
    Dim regEx
        
        Set regEx = CreateObject("vbscript.regexp")
        
        'At least 8 characters
        'At least 1 number
        'At least 1 lowercase letter
        'At least 1 uppercase letter
        'At least 1 special character. Special charaters include !@#$&#37;^&+=
        regEx.Pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$"
    
        ValidatePassword = regEx.Test(sPass)
        
        Set regEx = Nothing
    End Function

  6. #6
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,197

    Re: Password which consists capital letter, number, small letter

    do you want to make one ( function generator(byval size as integer, byval specials as string)as string)?

    or just test one (function Test(by val pwd as string) as bool )?

    the use of special characters can be a problem for systems so..

    can you provide an allowable set for the system/user to use?

    oh yes its not just the girls who think size matters... so...

    how many digits?

    here to talk

  7. #7
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,197

    Re: Password which consists capital letter, number, small letter

    nice one MarkT

    I was going to do something similar

    but can you explain the regular expression expression for the benefit of the forum

    its a little too esoteric for some of them

    here to talk

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    53

    Re: Password which consists capital letter, number, small letter

    Firstly, thanks for replies..

    Actually I wanted the user has to determine a password which consists at least; one capital letter, one small letter, one number and one special character as you said..

    Doogle, it works.. Could you explain its idea?
    RhinoBull, it always says "Invalid password - it must contain at least 1 capital letter, 1 lower and 1 numeric character."
    MarkT, I didn't understand what you meant..

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Password which consists capital letter, number, small letter

    Quote Originally Posted by _MeRKeZ_ View Post
    RhinoBull, it always says "Invalid password - it must contain at least 1 capital letter, 1 lower and 1 numeric character."..
    That's because I used "abcdefg123" as a test password - change it to whatever (perhaps to "Abcdefg123") to see how it works.
    Also, substitute hadcoded string with perhaps txtPassword.Text to make it dynamic.

  10. #10
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    2,372

    Re: Password which consists capital letter, number, small letter

    "Doogle, it works.. Could you explain its idea?"

    Did you try Axxxxx111

  11. #11
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    3,972

    Re: Password which consists capital letter, number, small letter

    Quote Originally Posted by _MeRKeZ_ View Post
    MarkT, I didn't understand what you meant..
    What didn't you understand? The usage would be
    Code:
    Private Function ValidatePassword(ByVal sPass) As Boolean
    Dim regEx
        
        Set regEx = CreateObject("vbscript.regexp")
        
        'At least 8 characters
        'At least 1 number
        'At least 1 lowercase letter
        'At least 1 uppercase letter
        'At least 1 special character. Special charaters include !@#$%^&+=
        regEx.Pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$"
    
        ValidatePassword = regEx.Test(sPass)
        
        Set regEx = Nothing
    End Function
    
    Private Sub Command1_Click()
        MsgBox "Valid = " & ValidatePassword(Text1.Text)
    End Sub

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Password which consists capital letter, number, small letter

    Quote Originally Posted by TysonLPrice View Post
    it looks like Axxxxx111 passes the test but does not have any special characters.
    Yup, there's an error in my code.
    Code:
            If strC >= "!" And strC <= "\" Then
    should be
    Code:
            If strC >= "!" And strC <= "/" Then
    Apart from that, I think it solves OP's original problem (although it's probably not the best solution)

    BTW It works by checking each character in txtPassword, if it's a 'special character' between '!' and '/' it sets Bit 3 of bytFlag, if it's between '0' and '9' it sets Bit 2 of bytFlag, if it's between ':' and '@' it sets Bit 3 of bytFlag, if it's between 'A' and 'Z' it sets Bit 1 of bytFlag, if it's between 'a' and 'z' it sets bit 0 of bytFlag.

    So after all the characters have been examined, if there was at least one of each (Capital, Lower Case, Number, Special Character) bytFlag will equal &h0F.

    (Bit0 = 1, Bit1 = 2, Bit2 = 4, Bit3 = 8, add them all up and it's 15 = &hF)
    Last edited by Doogle; Dec 27th, 2011 at 12:42 PM.

  13. #13
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,197

    Re: Password which consists capital letter, number, small letter

    thats what I like to see most of you telling what it does and not just telling what it is

    If this continues the newbies might learn a thing or two

    long may it continue...

    but what does, for the benefit of the op and other forum users

    the
    Code:
    regEx.Pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$&#37;^&+=]).*$"
    mean

    the structure and how to read it would help everyone to get theri heads around regex

    for many it is more than alien territory

    here to ... (well i am just here)

  14. #14
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    2,372

    Re: Password which consists capital letter, number, small letter

    Quote Originally Posted by incidentals View Post
    thats what I like to see most of you telling what it does and not just telling what it is

    If this continues the newbies might learn a thing or two

    long may it continue...

    but what does, for the benefit of the op and other forum users

    the
    Code:
    regEx.Pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&+=]).*$"
    mean

    the structure and how to read it would help everyone to get theri heads around regex

    for many it is more than alien territory

    here to ... (well i am just here)
    Regular expressions aren't something, at least to me, you can just easily explain. You need to know what kind of animal it is and the basic concepts. Look here:

    http://www.regular-expressions.info/

  15. #15
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    3,972

    Re: Password which consists capital letter, number, small letter

    Yea, what TysonLPrice said.

    Here is another link that will probably confuse you more as to what the expression is doing. http://www.regular-expressions.info/reference.html

    Basically you can break it apart for each of the checks you need

    The check for least at 8 characters
    The check for at least 1 number
    The check for at least 1 lowercase letter
    The check for at least 1 uppercase letter
    The check for at least 1 special character. Special charaters include !@#$%^&+=

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

    To be honest most of the time if I need a complex expression I will just google for it.

  16. #16
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,197

    Re: Password which consists capital letter, number, small letter

    the reference to the regex expression is a good idea

    its the sheer alien-ness of them that I think the newbies will find hard if not horrifying

    its all well and good to say "rolled up bits of newspaper 1 inch wide will stop the elephants from coming"
    and to be "proved right" because they havent arrived yet!

    its another to understand why the process works and how to use it fruitfully in the future against the rhinos

    its the how and why they need ( newbies )

    the answers alone are not going to broaden their understanding

    there I go ranting again... I'd better off and check my sugars

    here to talk
    Last edited by incidentals; Dec 27th, 2011 at 12:46 PM.

  17. #17

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    53

    Re: Password which consists capital letter, number, small letter

    Thanks for your replies, I think it's gonna work anymore

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.