Validate a Password's Format - March 2010

If you download this tip please consider making a modest donation.

The application I'm currently working on accepts passwords. I wrote some code to validate the entered passwords format. I encapsulated the code in a class module called cPassword.vb. The following edits are performed:

  • Must not be blank.
  • Must not contain spaces.
  • Can't be to short.
  • Can't be too long.
See 'Generate a Random Password - November 2009' for VB code that generates a password.

Demonstration Web Application

I wrote a VB.Net VS2008 web application to demonstrate the code. Some screen shots:


The Password is OK

 


The Password is 'blank'


The Password contains spaces.


The Password is too short.


The Password is too long.

Methods and Properties

Methods   Returns
Valid(sPassword)   Returns True if passes the tests.
Invalid(sPassword)   Returns True if fails the tests.
Properties Default Description
iMinLength 6 The minimum length.
iMaxLength 15 The maximum length.
sErrorMessage   Contains an error message if it fails.
Contains the OK message if it passes.

Sample Calling Code

Dim oPassword As New cPassword
With
oPassword
    If .Valid(sPassword) Then
        oT.MessageGood(Me.TableMain, .sErrorMessage)
    Else
       
oT.MessageGood(Me.TableMain, .sErrorMessage)
    End If
End
With

Download VB.Net Code

Download - Source Zipped File Size
VS2008 VB Solution PasswordValid.zip 28K

Previous Tip   Tip Index   Next Tip

Home

Updated 2011/04/16