The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 2 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
VBA Question - Password Validation
Â
The first uppercase letter is followed by more characters (all uppercase alphabets) and/or digits; however, the number of digits cannot be more than 2. (Note: the number of digits can be zero, but not more than two).
Â
I cannot get my head around how to validate that the input contain 2 or less numeric values. Could someone please give me a hand? My code is provided below.
Â
Sub Password()
Dim pass1 As String
  Dim pass2 As String
  Dim userinput As String
  Dim isPasswordUnique As Boolean
  Dim passwordCells As Range
  Dim pos28 As Integer
  Dim midChar As String
  Dim isValid As Boolean
  ' Ask for valid password until unique.
  Do
    ' Ask for password until clears validation.
    Do
      isValid = True
      userinput = InputBox("Please enter a password that meets this criteria:" & vbNewLine & vbNewLine & _
      "First character must be a capital letter." & vbNewLine & _
      "Password must be 8 chacters in length." & vbNewLine & _
      "All characters must be capital letters or digits." & vbNewLine & _
      "No more than 2 numeric digits.", "Password" & vbNewLine)
      ' Blank entry check.
      If userinput <> "" Then
        pass1 = userinput
        ' Length check.
        If Len(pass1) <> 8 Then
          isValid = False
        ' First character capital check.
        ElseIf Not (Left(pass1, 1) >= "A" And Left(pass1, 1) <= "Z") Then
          isValid = False
Â
        ' Other characters check.
        Else
          For pos28 = 2 To 8
            midChar = Mid(pass1, pos28, 1)
            If Not ((midChar >= "A" And midChar <= "Z") Or _
                (midChar >= "0" And midChar <= "9")) Then
              isValid = False
              Exit For
            End If
          Next
        End If
      End If
      If Not isValid Then
        MsgBox "The password is not valid.", vbInformation, "Invalid"
      End If
    Loop Until userinput <> "" And isValid
    Set passwordCells = ActiveWorkbook.Worksheets("Password").Range("A1")
    ' If password valid check against password list
    isPasswordUnique = True
    Do Until passwordCells = ""
      If pass1 = passwordCells.Value Then
        isPasswordUnique = False
        Exit Do
      End If
      ' Next cell
      Set passwordCells = passwordCells.Offset(1, 0)
    Loop
    If Not isPasswordUnique Then
      MsgBox "The password you entered, " & pass1 & ", is valid, but, " _
        & "already in use. Try another password.", vbInformation, "Password in use."
    Else
      pass2 = InputBox("Please reenter your password for verification.")
      If pass2 <> pass1 Then
        MsgBox "The passwords do not match. Please try again.", vbInformation, "No verification"
      Else
        MsgBox "Congrats! Your new password is " & pass1 & ".", _
          vbInformation, "Password Set"
        ' Add new password to password list.
        ActiveWorkbook.Worksheets("Password").Range("A1").End(xlDown).Offset(1, 0) = pass1
      End If
    End If
  Loop Until isPasswordUnique And pass2 = pass1
Â
End Sub
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly