Home Contact Sitemap Todo Money Money

AKH Online, Inc

Accomplished Internauts

ColdFusion "FormatName" function

ColdFusion "FormatName" function

CFML engines have tons of formatting features, to help us display dates, times and currency to match the customs of our clients and their locales.

But sometimes you'll have an application in which different clients would like their user, customer or patient names formatted differently - and there isn't a native "FormatName" function out of the box. Trent Richardson made one of his own, but it accepts the complete name as a parameter. I needed one which took first, middle and last name separately. Plus, I wanted to follow the familiar "format string" concept. So I made one of my own.

The rules for my format string are as follows:

f = first initial ff = full first name FF = force uppercase full name F. uppercase initial w/dot

Just apply the same rule to M and L (or m & l) for middle and last names.

Examples:

formatName('Alan','Kookyman','Holden', 'ff m. ll') would produce: Alan K. Holden

formatName('Alan','Kookyman','Holden', 'LL, f . m.') would produce: HOLDEN, A. K.

It's pretty lazy when it comes to other punctuation.

 

Here's the current version of the nameFormat function:

 

<cffunction name="formatName" returntype="String" access="public" hint="Arranges first, middle and last name according to format template string. akhonline.com - cool guy">
    <cfargument name="fName" type="string" required="true" hint="First Name" />
    <cfargument name="mName" type="string" required="true" hint="Middle Name" />
    <cfargument name="lName" type="string" required="true" hint="Last Name" />
    <cfargument name="template" type="string" required="true" hint="Format string: f.=first init w dot, ff=full first name, FF=all caps, ll,=last name w comma and so on. Blank for default of 'fName lName'." />
    <cfset var returnString = '' />
    <cfif NOT len(arguments.template)>
        <cfset returnString = arguments.fName & ' ' & arguments.lName />
    <cfelse>
        <cfloop list="#arguments.template#" index="part" delimiters=" ">
            <cfset chunk = '' />
            <!--- get whole chunk - upper or lower --->
            <cfif findNoCase('f',part)>
                <cfset chunk = arguments.fName />
                <cfif find('F',part)>
                    <cfset chunk = ucase(arguments.fName) />
                </cfif>
            </cfif>
            <cfif findNoCase('m',part)>
                <cfset chunk = arguments.mName />
                <cfif find('M',part)>
                    <cfset chunk = ucase(arguments.mName) />
                </cfif>
            </cfif>
            <cfif findNoCase('l',part)>
                <cfset chunk = arguments.lName />
                <cfif find('L',part)>
                    <cfset chunk = ucase(arguments.lName) />
                </cfif>
            </cfif>

            <!--- is there punctuation in the part? --->
            <cfset punct = rereplace(part,"[a-zA-Z]","","ALL") />
            
            <!--- do we trim the chunk? --->
            <cfif len(rereplace(part,"[^a-zA-Z]","","ALL")) eq 1>
                <cfset chunk = left(chunk,1) />
            </cfif>
            
            <cfset returnString = listAppend(returnString,chunk & punct,' ') />

        </cfloop>
    </cfif>
    <cfreturn returnString>
</cffunction>

 

Hope this helps you.

My Details
Subscription Settings

Hear about status updates or new posts.