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.

Rather than just displaying a message like “That username is already taken, please try again.” and having them guess at other usernames, it would be nice to suggest a similar username to them that is definitely available.  Here is how we do it.

To do this, create a database called MyData and 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.

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

<form name="Register" method="post" action="confirm.asp">
Username:  <input type="text" name="Username" size="50"><br>
Password:  <input type="text" name="Password" size="50"><br>
Email:  <input type="text" name="Email" size="50"><br>
<input type="submit" name="Submit" value="Submit">
</form>

 

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 "
     Response.Write "on your browser and try a different username.  We suggest "
     Response.Write "you try the below available username:</p>"
     Response.Write "<b>• " & strUsername2 & "</b>"
     END IF

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

There you have it, a 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”.

< Back to ASP Scripts