Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically generate email addresses...

Status
Not open for further replies.

Deam

Programmer
Oct 10, 2000
68
0
0
US
I am working on a web application where I hire someone email address should automatically get created.
I need to create a java script page that would have a function to basically create email addresses on the fly for the users.
So 4 employees get hired in the following order - John Smith, John Smitty, John Small, John Smooth.
John Smith would get johns@whatever.com
John Smitty would get johnsm@whatever.com
John Small would get johnsma@whatever.com
John Smooth would get johnsmo@whatever.com

An additional letter is taken from the last name until there is a unique ID. Since the ID’s are kept indefinitely, if any of the users above leave the company a unique ID would still reference the old ID’s in order to create a new one. If a person got hired with a name of John Smaagaard he would get an ID of johnsmaa.


Any help is appreciated....
 
while what you want do is algorithmically possible it will be non-trivial and could well become too cumbersome to run for some queries.
If you had 50 employees called John S... the query may well have to run 50 iterations of the match check. And what if you hire 2 john smiths ?



Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Hmmm, is this data being kept in a database, Active Directory, ?

At first glance I agreed with Chris, but I think there may be an easier solution if you havethe ability to do select sql queries from the existing data. Well, acually you only need one SELECT query to do the job...

-T

barcode_1.gif
 
The data is in a sql server database. If all the characters are a match then it should add a 1 or 2 or 3 at the end.
 
Check out snowboardr's thread, he is looking or something similar...or at least the answer I provided him with wa the one I was originally thinking of for this one :)

thread333-929563

In your case you would want to buildthe list with only the names to theright of the @ in the address, and would probably want to pass their last name as an additional argument to the GetUniqueNames function so that instead of using incremental numbers you could use a Left(lastNameVar,ctr).

To solve the case where you already have a johns, johnsm, johnsmi, johnsmit, johnsmith you could do a length check on the last name before doing the left. If the ctr is greater then the length then use initial & lastNameVar & (ctr - len(lastName) to give you johnsmith1, johnsmith2, johnsmith3, etc.

So basically something like:
Code:
'GetUniqueName returns a name that is not in the passed userListing. Since we don't want to reuse old names we can safely ignore the case that shorter names (johnsmi) will match against things like johnsmit, since those earlier names shoul dhave already been in use.
Function GetUniqueName(firstName, lastName, userListing, delim)
   Dim t_name, t_ctr
   t_ctr = 1
   'get the first possible name
   t_name = getName(firstName,lastName,t_ctr)

   'Keep incrementing through possible names until we find one that doesn't have a match
   Do While InStr(userListing,delim & t_name & delim)
      t_ctr = t_ctr + 1
      t_name = getName(firstName,lastName,t_ctr)
   Loop

   GetUniqueName = t_name
End Function

'This function will build a name based on a first name, lastname, and counter
'   When the counter is larger then the num of char's in the last name, it attaches the last name and it's remainder as a counter
Function getName(firstN, lastN, num)
   If len(lastN) >= num Then
      getName = firstN & Left(lastN,num)
   Else
      getName = firstN & lastN & num - len(lastN)
   End If
End Function

So basically between the code I posted in snowboardr's thread to select the records and build a listing and this code right here, you should be able to get somethin going that only requires a single call to the db to check/build a name and then a final call after you have the name to add it to the db.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top