Create User ID, Registering New User on Website

Instead of having an admin create the users manually through a form, I want the new user to be able to get to the site login and register for a new login ID themselves.  The login ID is a specific format so they can't just go in and create their own ID.  I will have a link there for them to click 'New User' which will bring them to a form to fill out with validation so the form and the values are submitted properly.  The format for the log in ID is as follows:  hid[first letter of first name][first letter of middle name, if none provided it defaults to X][first letter of last name][randomly generated 4 character string].  For instance, in my case, it would be hidlrlzpn1.  The form obviously is going to have fields for First Name, Middle Initial, Last Name, email address, and password.

Is there a good example out there to create this type of ID?  Or can anyone help with the creation of this auto ID?  It will be loaded into the user table with the following parameters:  ID, FNM, MIN, LNM, EML, PSW.
Lee R Liddick JrReporting AnalystAsked:
Who is Participating?
 
_agx_Commented:
You need to put it inside a loop. Just repeat the check until it succeeds OR it exceeds the max number of retries.

I'd also put a UNIQUE constraint on that table column to ensure duplicates *can't* be entered period.

<cfset maxRetry = 10>
<cfset retryCount = 0>
<cfset success = false>

<cfloop condition="NOT success and retryCount lt maxRetry">
        <!--- generate a random username code here ...--->
      <cfset userName = "..generate the username code..">
      
      <!--- check if it's in use ---->
       <cfquery name="chkID">SELECT .... </cfquery>

      <!--- If it's not already used ..  ---->
      <cfif chkID.recordCount eq 0>
            success. go ahead and insert it
            <cfset success = true>
      </cfif>

        <!--- this prevents an infinite loop --->
      <cfset retryCount++>
</cfloop>

<cfif success>
        login sucessfully created
<cfelse>
        max retries exceeded. unable to create login
</cfif>
0
 
_agx_Commented:
FirstLetter + FirstLetter + FirstLetter + 4 Letters = 7 chars. The example given ie "hidlrlzpn1" is longer. Is some part of the formula missing?
0
 
_agx_Commented:
Here's an example using the 7 char specs.  It uses the getRandString function from cflib.org


<!--- sample form fields --->
<cfset form.firstName = "Bob">
<cfset form.middleInitial = "">
<cfset form.lastName = "Smith">


<!--- if the middle initial was supplied --->
<cfif len(trim(form.middleInitial))>
      <cfset userName = left(form.firstName, 1) & left(form.middleInitial, 1) & left(form.lastName, 1)>
<cfelse>
      <cfset userName = left(form.firstName, 1) & "X" & left(form.lastName, 1)>
</cfif>

<!--- append random chars --->
<cfset userName = userName & getRandString(4)>

<!--- results --->
<cfoutput> final userName = #userName#</cfoutput>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Lee R Liddick JrReporting AnalystAuthor Commented:
All ID's start with the letters 'hid'...so the total of the login string would be 10.
0
 
_agx_Commented:
(Duh!) Okay, just prepend "hid" at the end:

<!--- generate name portion --->
<cfif len(trim(form.middleInitial))>
      <cfset namePortion = left(form.firstName, 1) & left(form.middleInitial, 1) & left(form.lastName, 1)>
<cfelse>
      <cfset namePortion = left(form.firstName, 1) & "X" & left(form.lastName, 1)>
</cfif>
<!--- generate final id --->
<cfset userName = "hid" & namePortion & getRandString(4)>
<!--- results --->
<cfoutput> final userName = #userName#</cfoutput>
0
 
Lee R Liddick JrReporting AnalystAuthor Commented:
so its a getrandstring call that will get me this.  Okay.  So once it creates, I need to bounce it against the current user table to make sure there are no duplicates (I can't imagine that there ever would be) so I can generate the ID, then do a validation query against the user table...if there is a match, how do I loop it back to regenerate another ID?

<!--- generate name portion --->
<cfif len(trim(form.MIN))>
      <cfset namePortion = left(form.FNM, 1) & left(form.MIN, 1) & left(form.LNM, 1)>
<cfelse>
      <cfset namePortion = left(form.FNM, 1) & "X" & left(form.LNM, 1)>
</cfif>
<!--- generate final id --->
<cfset userName = "hid" & namePortion & getRandString(4)>

<!--- get current IDs --->
<cfquery name="chkID">
select ID
from rfw_users
where ID = #userName#
</cfquery>

So obviously, if the query returns 1 (there is a duplicate and I can't do the insert query) so I need to regenerate another ID, so how can I loop it back through if the result is 1 after my check?
0
 
Lee R Liddick JrReporting AnalystAuthor Commented:
I figured it would be a loop, wasn't sure how to actually do it.  You are a lifesaver once again agx!  Thank you...
0
 
_agx_Commented:
Always a pleasure to help out :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.