Validate Password Policy Using Regular Expression

Posted: October 14, 2010 in ASP.Net

To validate a password string so that it musst be 8 to 10 characters and both letters & numbers, you can try the following:
ASP.Net (Code Behind)

' Function to check that the password policy is met
Protected Function ValidPassword(ByVal PasswordString As String) As Boolean
    ' Declare variables and check for string pattern
    Dim StringPattern As String = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$"
    Dim StringMatch As Match = Regex.Match(PasswordString.Trim(), StringPattern, RegexOptions.IgnoreCase)
    If StringMatch.Success Then
        Return True
    Else
        Return False
    End If
End Function
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s