How to validate password with regular expression which requires single-byte alphanumeric characters and symbols?

Pocket

It’s needed to enter password which requires a character, a number and a symbol at least, respectively. I’d like to describe how to validate password with VBScript®. It’s assumed that the length of password is 8 or greater.

EnterPassword

In 23 line, the constraint is shown. In 22 line with comment, it’s shown that constrains password to require both a single-byte character and number at least and 8 or greater character length.

Option Explicit

Private Sub CommandButton1_Click()
    With TextBox1
        If Not CheckPassword(.Text) Then
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
            Exit Sub
        Else
            
        End If
    End With
    Unload Me
End Sub

Function CheckPassword(InputString As String) As Boolean
    Dim myReg   As Object
    CheckPassword = False
    Set myReg = CreateObject("VBScript.RegExp")
    With myReg
       '.Pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,})$"
        .Pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)(?!^[!-/:-@[-`{-~]*$)(?!^[a-zA-Z0-9]*$)(?!^[!-@[-`{-~]*$)(?!^[!-/:-~]*$)^([!-~]{8,})$"
        .IgnoreCase = False
        .Global = True
    End With
    If myReg.Test(InputString) Then
        CheckPassword = True
    End If
    Set myReg = Nothing
End Function

Private Sub UserForm_Initialize()
    With TextBox1
        .IMEMode = fmIMEModeDisable
        .PasswordChar = "*"
    End With
End Sub

You might need description here. The pattern (?!pattern) means such negative lookahead as EXCEPT operator effects in SQL. To seek area three circles overlap, it’s needed to remove areas around. After filtering out not required patterns with negative lookahead, it validates length of the password. The number of required pattern, which verifies n types of letter, is 2n – 2.

Not Needed Negative Lookahead Pattern
Number (?!^[0-9]*$)
Character (?!^[a-zA-Z]*$)
Symbol (?!^[!-/:-@[-`{-~]*$)
Character and number (?!^[a-zA-Z0-9]*$)
Number and symbol (?!^[!-@[-`{-~]*$)
Character and symbol (?!^[!-/:-~]*$)

PasswordValidation

References:
Regular Expression Language – Quick Reference
How To: Use Regular Expressions to Constrain Input in ASP.NET
ASCII character code list (0-127)
Userform of Excel VBA as user interface

Pocket

投稿者: admin

趣味:写真撮影とデータベース. カメラ:TOYO FIELD, Hasselblad 500C/M, Leica M6. SQL Server 2008 R2, MySQL, Microsoft Access.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です