The purpose of this is to show you how you can create a "suggest a username" script. This script will suggest some new choices of usernames if the one the user enters is taken.
The source code for the random function we will be using:
<%
Function
GenPass()
Dim
fs
Dim
strTemp
Set
fs = CreateObject("Scripting.FileSystemObject")
' Get just the filename part of
the temp name path
strTemp = fs.GetBaseName(fs.GetTempName)
'Hack off the 'rad'
strTemp = Right(strTemp, Len(strTemp)
- 3)
Set
fs = Nothing
GenPass=strTemp
End Function
%>
How this function works
This function works by accessing the name of a randomly generated temporary file. Files normally would look like this radXC93D.tmp. The function simply hacks off the "rad" and the ".tmp" to retain the 5 random characters
Lets get down to business
Let assume for this project that I have an html page where new users select a username and password to register for my application. For simplicities sake I will skip all of the normal error checking I would do so we can focus on the core concepts of this article. The following code will be what we use to suggest usernames to the user if they are already taken.
First we'll take a look at the code and then you will see how it works
We'll assume we declared a connection to our database called "Conn"
Dim
UserName,Suggestions,X,N,MaxN,Temp ''Declare needed
variables
Dim RS,sql
UserName=Request.Form("UserName")
sql="SELECT * FROM Users WHERE UserName='" & UserName &
"';"
Set RS=Conn.Execute(sql)
If RS.EOF then
''They picked a unique user name.
It is safe for you to add them to the database
else
Suggestions=4 ''Set this equal to
the number of suggestions
'''you wish to make
X=0 ''the current number of
suggestions made
N=0 ''Loop iterations
MaxN=1000 ''Maximum loop
iterations
What has been done until this point: Response.Write "<b>The UserName
""" & UserName & """ is taken."
In the database I used for the live example "nplst" is a user. The script output would look like this:
Response.Write " May I suggest on of the following
UserNames?</b><br>"
Do Until
X=Suggestions
Temp=UserName &
Right(GenPass,2) ''only use 2 didgits of the random
password
'''for
our new suggestion
sql="SELECT * FROM Users
WHERE UserName='" & Temp & "';"
Set
RS=Conn.Execute(sql)
if RS.EOF then
''If
we didn't find the password in the database it is unique.
''So
suggest it!
Response.Write(Temp & "<br>")
X=X+1 '''bump up the current
suggestion count
end if
N=N+1 ''We just went through the loop so bump
up our loop counter
If N=MaxN then
''Ooops looks like we couldn't find the the number of
unique
''usernames we specified at the beginning of this script
Response.Write("<br>Couldn't find " & Suggestions &
" unique")
Response.Write(" UserNames! Breaking from
loop.....<br><br>")
X=Suggestions
end if
Loop
end if
-----------------------------------------------------------------
The UserName "nplst" is taken. May I suggest on of the following UserNames?
nplst4F
nplstC1
nplstF1
nplst3F
-----------------------------------------------------------------
This script can be easily modified to create random passwords for your visitors. These passwords should be emailed to the person whom registered.
What are the advantages of doing this?
Quite simply it helps to cut down on bogus e-mail addresses in your database. If you allow people to enter their own passwords up front you will begin to see a lot of addresses like "booger@snot.com" and "asdf@asdf.com" If they want access to your protected area they will need to supply a valid address with which
to recieve your generated password. The default length for passwords from GenPass is 5. If you wanted to create a 10 character password you would simply say
Pass=GenPass & GenPass
or an 8 character pass
Pass =Right(GenPass & GenPass,8)
username password function