Results 1 to 5 of 5

Thread: [RESOLVED] Password Validation

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    27

    Resolved [RESOLVED] Password Validation

    Hey, currently trying to figure out some basic passwork validation for a text box. The first character in the password needs to be a CAPITAL and the last character needs to be a NUMBER. This is what I have so far.

    Code:
    Private Sub cmdAddUser_Click()
    
    passtest = txtPassword.Text
    
    fPass = Mid(passtest, 1, 1)
    PassLen = Len(passtest)
    lPass = Mid(passtest, PassLen, 1)
    IntCode = Asc(fPass)
    LastCode = Asc(lPass)
    
    If IntCode >= 65 And IntCode <= 90 Then
        If LastCode >= 48 And LastCode <= 57 Then
            With rs
                .AddNew
                !UserName = txtUser.Text
                !Password = txtPassword.Text
                .Update
            End With
                MsgBox "The new User as been successfully added to the system database"
                txtUser.Text = ""
                txtPassword.Text = ""
                txtUser.SetFocus
            Else
            MsgBox "The password must contain a Capital letter as its first character, and a Number as its last character", vbCritical + vbOKOnly, "Error"
        End If
    End If
    
    
    End Sub
    Any advice would be much appreciated.

    Cheers,

    Damien

  2. #2
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Password Validation

    I don't see any problems with the code, getting an error? Perhaps just style preference
    but I would replace
    fPass = Mid(passtest, 1, 1) with fPass = Left$(passtest, 1)
    and
    lPass = Mid(passtest, PassLen, 1) with fPass - Right$(passtest, 1)

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Password Validation

    Quote Originally Posted by VBClassicRocks View Post
    lPass = Mid(passtest, PassLen, 1) with fPass - Right$(passtest, 1)
    I think, you mean this:
    Code:
    lPass = Right$(passtest, 1)


    @b0rn2fl1: Some additional suggestions:
    1. Trim off extra spaces
      Code:
      passtest = trim$(txtPassword.Text)
    2. And also, try to declare all variables. Otherwise, it will end in errors at some point, because VB will consider undeclared variables as Variants

    ..

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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

    Re: Password Validation

    You need an Else clause on the first If statement. Currently you're only reporting a problem if the last character is not a Number.
    Code:
    If IntCode >= 65 And IntCode <= 90 Then
        If LastCode >= 48 And LastCode <= 57 Then
            With rs
                .AddNew
                !UserName = txtUser.Text
                !Password = txtPassword.Text
                .Update
            End With
            MsgBox "The new User as been successfully added to the system database"
            txtUser.Text = ""
            txtPassword.Text = ""
            txtUser.SetFocus
        Else
            MsgBox "The Last character of the Password must be a Number", vbCritical + vbOKOnly, "Error"
        End If
    Else
        MsgBox "The First character of the Password must be a Capital Letter",vbCritical + vbOKOnly,"Error"
    End If
    Also,and I'm sure the purists will not agree, why not say what you mean?

    Rather than use Asc (most of us understand letters and numbers, not so many understand Asc)
    Code:
    Dim intCode As String
    Dim LastCode As String
    
    intCode = Mid$(passtest, 1, 1)
    LastCode = Mid$(passtest, Len(passtest), 1)
    If IntCode >= "A" And IntCode <= "Z" Then
        If LastCode >= "0" And LastCode <= "9" Then
    It might 'cost' some time in compilation but it might also save 'cost' when reading the code in a years time!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    27

    Thumbs up Re: Password Validation

    Awesome. Thanks very much for your help. Much appreciated

Posting Permissions

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



Click Here to Expand Forum to Full Width