Suggest Usernames

Snitz™ Forums 2000
https://forum.snitz.com/forumTopic/Posts/68450?pagenum=1
05 November 2025, 07:04

Topic


mafifi
Suggest Usernames
27 March 2009, 17:39


I'm looking for a code snippet that will allow the visitor to my member’s only web site to enter a username upon first time registration on the site. I want the code to check the database (SQL) and if the username is taken, I want the code to suggest usernames based on the string they entered originally, and then add some numbers at the end. So if John Doe applies for an account and request 'JohnDoe' as the username, then I want to suggest 'JohDoe1' or 'JohnDoe2' or 'JohnDoe3'. I am assuming that this will have to be in ASP and Ajax.

 

Replies ...


mafifi
30 March 2009, 20:59


I am half way there. This code will insert the username in the database and if the name is taken it will take the string entered by the user and adds (1) and so on. I still need to work on the AJAX part.
Code:


<%
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "MyDSNConnection"
objConn.Open

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
Response.write mySQL

IF objRS.EOF THEN

objRS.AddNew
objRS("fUsername") = strUsername
objRS("fPassword") = strPassword
objRS("fEmail") = strEmail
objRS("fDateEntered") = Date()
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 & "'"
'Response.write mySQL2
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

objRS2.Close
Set objRS2 = 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:</p>"
Response.Write "<b>• " & strUsername & "</b>"
END IF

ELSE
Response.Write "Please click Back on your browser and complete the required form fields"
END IF
%>


Etymon
30 March 2009, 21:50


Thank you Maffi!
Carefree
30 March 2009, 23:30


If you're wanting to incorporate this into "register.asp", here's a Reader's Digest Condensed version:
Code:

if strMode = "Register" then
strRFName=ChkString(Trim(Request.Form("Name")), "SQLString")
errname=1
ig=0
do until errname=0
strSQL = "SELECT M_NAME FROM " & strMemberTablePrefix & "MEMBERS WHERE M_NAME = '" & strRFName & "'"
set rsCheck=my_Conn.Execute(strSql)
if not rsCheck.EOF then
ig=ig+1
strRFName=strRFName+cStr(ig)
Err_Msg = Err_Msg & "<li>Requested user name is already in use.<br>Suggested available alternative is:<i>""" & strRFName & """</i></li>"
else
errname=0
end if
Loop
rsCheck.Close
Set rsCheck=Nothing
end if
© 2000-2021 Snitz™ Communications