Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Split and join string

Avatar

Former Community Member

Hi all.

I have a form with a textbox for the full name and I whant to uppercase the fist letter in the first and last name.

I have a function for that and all works well, the problem comes when there is a hypen "-" in the name. I have the split part working but on the join side I cant get it to work.

function ucFirstAllWords( str )

{

          var pieces = str.split(" "); //use this for space and "-" split (/[ \/-]/);

    for ( var i = 0; i < pieces.length; i++ )

    {

        var j = pieces[i].charAt(0).toUpperCase();

        pieces[i] = j + pieces[i].substr(1);

    }

    return pieces.join(" ");

}

If I use this code and slpit on both space and hypen all get join with space, I have tyed to make a function in function to capture the first hyphen but that didn't work probably doing it wrong

Does anyone have any idea how to solve it?

I'm using LC 8.0

Thanks.

//Fredrik Nordstrand, Sweden

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Hi Vjay.

I want "john doe" to be "John Doe" and "ann-lee rose bush" to be "Ann-Lee Rose Bush"

Do I make any sense?

The scrip woks fine but not with a name that contains a hypen.

If I use the split to include the hypen then "ann-lee" becomes "Ann Lee" and thats not right.

View solution in original post

4 Replies

Avatar

Level 5

I gave "john-smith" for this im getting result as "John-smith".It is working as i expected. Is that what u wanted?

Vjay

Avatar

Correct answer by
Former Community Member

Hi Vjay.

I want "john doe" to be "John Doe" and "ann-lee rose bush" to be "Ann-Lee Rose Bush"

Do I make any sense?

The scrip woks fine but not with a name that contains a hypen.

If I use the split to include the hypen then "ann-lee" becomes "Ann Lee" and thats not right.

Avatar

Level 10

Put this code on the exit event of the text field:

this.rawValue = this.rawValue.replace(/\b([a-z])/g, function (_, initial) {return initial.toUpperCase();});

This was originally posted by Srini in this thread:

http://forums.adobe.com/message/3953615#3953615

Avatar

Former Community Member

Thanks Jono, works great .

Very useful oneliner

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----