J_Smith
Jo_Smith
Joe_Smith
???
???
Another is what happens if you have someone with an extremely long last name? A friend of mine has a 15 letter last name.
A lot of organizations have a similar issue and their solution has been to implement the following business rule:
First Letter of First Name + Up to first 7 letters of last name + numeric value (if needed)
So you would end up with something like this:
JSmith
JSmith1
JSmith2
Anyways here is my solution:
<cfparam name="posted" default="FALSE">
<cfif isDefined('form.submit')>
<cfset posted = TRUE>
</cfif>
<cfif posted>
<!--- AS REQUESTED --->
<!--- QUERY YOUR DATABASE TO FIND OUT IF THERE ALREADY IS A USER WITH
A SIMILAR NAME STRUCTURE --->
<cfquery name="getUserName" datasource="#request.dsn#">
SELECT *
FROM EMPLOYEES
WHERE First_Name LIKE '#FORM.FirstName#%'
AND Last_Name = '#FORM.LastName#'
</cfquery>
<!--- CREATE A NEW USER NAME WITHIN THE BOUNDRIES OF THE BUSINESS LOGIC --->
<cfset newUserName2 = left(#FORM.firstName#, #getUserName.RecordCount#) & '_' & left(#FORM.lastName#, 7)>
New User Name: <cfdump var="#newUserName2#"><br>
<!--- RECOMENDED WAY --->
<!--- CREATE A NEW USER NAME WITHIN THE BOUNDRIES OF THE BUSINESS LOGIC --->
<cfset newUserName = left(#FORM.firstName#, 1) & left(#FORM.lastName#, 7)>
<!--- QUERY YOUR DATABASE TO FIND OUT IF THERE ALREADY IS A USER WITH
A SIMILAR NAME STRUCTURE --->
<cfquery name="getUserName" datasource="#request.dsn#">
SELECT *
FROM EMPLOYEES
WHERE First_Name LIKE '#FORM.FirstName#%'
AND Last_Name = '#FORM.LastName#'
</cfquery>
<!--- debugging for your query
<cfdump var="#getUserName#">
--->
<!--- IF THERE ALREADY IS A ACCOUNT NAME THAT MATCHES THE CRITERIA WE HAVE USED
ADD A NUMBER TO THE END OF THE ACCOUNT NAME --->
<cfif getUserName.recordCount GT 0>
<cfset newUserName = newUserName & (getUserName.recordCount + 1)>
</cfif>
<!--- CHECK TO BE SURE THAT THE GENERATED USERNAME DOES NOT CONFLICT WITH
ONE THAT IS ALREADY IN THE SYSTEM --->
<cfquery name="verifyUserName" datasource="#request.dsn#">
SELECT *
FROM EMPLOYEES
WHERE account_name = '#newUserName#'
</cfquery>
<!--- IF THERE IS DROP AN ERROR! --->
<cfif verifyUserName.recordCount GT 0>
<cfabort showerror="there was a problem generating the account name">
</cfif>
New User Name: <cfdump var="#newUserName#">
<cfelse>
<form name="createUser" method="post">
First Name: <input type="text" name="firstName" /><br />
Last Name: <input type="text" name="lastName" /><br />
<input type="submit" name="submit" />
</form>
</cfif>
