0

can anyone help me with code to auto carete a username.
this should tahe the 4 letters of the user's name and assign a number from a sequence in a database.
basically the username should be like "comp001" but it should verify if it exists in the database. please help. im new to coldfusion

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by cmhampton
0

Here's something to start with. To get the first four characters of the user's name, use the LEFT function. Assuming you have a text box on the form called 'txtName':

<cfset username = LEFT(form.txtName, 4) />

If the name entered in that field was 'mkwebnovice', this would give you 'mkwe'. Next, you can use the MAX function in SQL (provided you are using SQL) to get the highest ID record in your user's table. I am of course assuming that you have a users table with an auto incrementing id field. When you do your insert statement, use this function and combine it with the result of the LEFT function above.

<cfquery datasource="whatever your datasource is" name="qryGetMaxUserID">
  DECLARE @userID varchar(10);
  SELECT @userID = CAST((SELECT MAX(userID) + 1 AS newUserID FROM users) AS varchar(10));

  INSERT
    users
    (
       username
    )
VALUES
    (
      '#username#' + @userID
    )
</cfquery>

If you a user table and the last userID is 20, this will result in a username of 'mkwe21'.

I hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.