views:

71

answers:

3

I have a form with a full-name text field and I would like to break the string into a first and last name strings... I'm handling the form in Coldufusion.

What is the most reliable way to achieve this? I'm assuming JavaScript is not an option since, in its absence, the form would generate an error. Any examples would be great.

Thanks

A: 

In coldfusion, after it is submitted, I would split it on a space character.

Ryan Gooler
Do you know the function for doing such a thing, Jippen?
Mel
+1  A: 

If you just want to split on the first space, you can use the list functions. For example, you could get the first word by using

<cfset first = listfirst(fullname, " ")>

List functions all take an optional parameter, delimiters, which lets you specify how the list is delimited. So, in this case, your list is space-delimited.

Remember that not all names are two words. People use middle names, constructs like "De Marco" and "Van DeGraff" and so forth, or multiple first names like "Lisa Ann". But this will get you a good approximation.

Ben Doom
Ben, is it possible to <cfset firstName = listfirst(fullName, " ")> and then subtract it from fullName... thus ending up with the first word as firstName and the rest of the sentence as lastName?
Mel
<cfset lastname = trim(replace(fullname, firstname, "")) /> Think I've got the arguments in the right order... replace the firstname found in fullname with nothing and then trim to remove the otherwise leading space.
Antony
+2  A: 

Found a better solution

<cfset fullName = "foo bar">
<cfset firstName = listFirst(fullName, " ")>
<cfset lastName = listRest(fullName, " ")>
Mel
That's how I'd do it.
Al Everett