Wednesday, 30 November 2011

SUGGEST USERNAME

If you have a members only website where users can register a unique username and become members, one common inconvenience for your users is that they will try to register a username that has already been registered by someone else. Well, rather than just displaying a message like "That username is already taken, please try a different username." and keep them guessing at usernames, wouldn't it be nice to suggest a similar username to them that is definitely available? Sure it would. Here is how we do it.

The first thing you need to do is create a database called MyDatabase. Then create a table called tblUsers with these fields:
ID - autonumber
fUsername - text field
fPassword - text field
fEmail - text field
fDateEntered - date/time field

Now, enter these usernames into your database: bob, bob1, bob2. The password and email fields are here just as formalities for this exercise.

Next, create a page called register.asp page with the code below

Username:
Password:
Email:


Next, create a page called confirm.asp with the code below:

<% DIM strUsername, strPassword, strEmail strUsername = Request.Form("Username") strPassword = Request.Form("Password") strEmail = Request.Form("Email") IF strUsername <> "" AND strPassword <> "" AND strEmail <> "" THEN

DIM mySQL, objRS
mySQL = "SELECT * FROM tblMembers WHERE fUsername = ' " & strUsername & " ' "
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn, adOpenKeyset, adLockPessimistic, adCmdText

IF objRS.EOF THEN
objRS.AddNew
objRS("fUsername")
objRS("fPassword")
objRS("fEmail")
objRS.Update
objRS.Close
Set objRS = Nothing
Response.Write "You have been successfully registered as: " & strUsername

ELSE

DIM X, strTempUsername, intCount, mySQL2, objRS2
DO UNTIL X=True
intCount = intCount + 1
strTempUsername = strUsername & intCount
strUsername = strTempUsername

mySQL2 = "SELECT * FROM tblMembers WHERE fUsername = ' " & strUsername & " ' "
Set objRS2 = Server.CreateObject("ADODB.Recordset")
objRS2.Open mySQL2, objConn, adOpenKeyset, adLockPessimistic, adCmdText

IF objRS2.EOF THEN
X=True
ELSE
intCount = intCount
END IF

LOOP

objRSa2.Close
Set objRSa2 = Nothing

Response.Write "That username has already been registered. Please click Back on your browser and try a different username. "
Response.Write "We suggest you try the below available username:

"
Response.Write "• " & strUsername2 & ""
END IF

ELSE
Response.Write "Please click Back on your browser and complete all three fields"
END IF
%>

That's it! Now, you have a fully automated allbeit simple username suggestion tool. Try testing it. If a user tries to enter "bob", the script will suggest "bob12". If a user tries to enter "bob1", the script will suggest "bob11". Happy registering!

http://www.aspwebpro.com/aspscripts/websitetools/suggestusername.asp

No comments:

Post a Comment