Generating Passwords

This function can be used to have a random password generated. It allows you to specify valid characters as well as specifying number of characters.

<%
 Function Password_GenPass( nNoChars, sValidChars )
 ' nNoChars = length of generated password
 ' sValidChars = valid characters. If zerolength-string ( "" )then
 ' default is used: A-Z AND a-z AND 0-9
 
Const szDefault = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789"
 Dim nCount
 Dim sRet
 Dim nNumber
 Dim nLength
 
Randomize 'init random
 
If sValidChars = "" Then
 sValidChars = szDefault
 End If
 nLength = Len( sValidChars )
 
For nCount = 1 To nNoChars
 nNumber = Int((nLength * Rnd) + 1)
 sRet = sRet & Mid( sValidChars, nNumber, 1 )
 Next
 Password_GenPass = sRet
 End Function
 %>


Call it like this to generate a 10 letter long password: 

<%Response.Write "Your password: " & Password_GenPass( 10, "" ) %>

Or if you want to just use certain characters: 

<%Response.Write "Your password: " & Password_GenPass( 5, "ABCabc1" ) %>

This will generate a random password only using the characters ABCabc1, therefore it give might result like: Your password: CabCB  

The Result


Your password: 0B6bOcqVtD
5 Constrained Characters: Your password: a1Cca

asp generate passwords ASP ADO VBScript DOM tutorial authoring programminghowto reference examples samples source code demos tips


Back To Top
© 1998 - 2017 psacake.com | My3C's

Version 7.2 | Advertise on this site