Skip to main content
Participant
May 5, 2017 9:33 PM UTC
Answered

capitalize first charcter sentence

how would you force capitalization of the first letter of the first word in each sentence in a multi line text box.  all other words in the sentence should be lower case

This topic is closed to new replies. Start a new post to keep the conversation going.
Joel Geraci
Joel GeraciCorrect answer
Community Expert
May 9, 2017 5:41 PM UTC

Add the following code to the custom validation script of the field. The code will split the paragraph into sentences as long as the separator is a period followed by at least one space. It then strips and leading spaces in case people double space after a period (yuck) and then converts the first character to uppercase... it doesn't bother to check. It then reassembles the sentences into a paragraph where the sentences are separated by a period and a single space.

The conversion happens when the user exits the field just prior to committing the value.

var para = event.value; 
var sentences = para.split(". ");
for (var i=0; i < sentences.length; i++) {
     sentences = sentences.trim().charAt(0).toUpperCase() + sentences.slice(1);
}
event.rc = true;
event.value = sentences.join(". ");

6 replies

Participant
May 6, 2017 11:01 AM UTC

Joel,

Lets fix it before they commit.  I just know there will be cutting and pasting by someone.

Translate
Joel Geraci
Community Expert
Joel GeraciCommunity ExpertCorrect answer
Community Expert
May 9, 2017 5:41 PM UTC

Add the following code to the custom validation script of the field. The code will split the paragraph into sentences as long as the separator is a period followed by at least one space. It then strips and leading spaces in case people double space after a period (yuck) and then converts the first character to uppercase... it doesn't bother to check. It then reassembles the sentences into a paragraph where the sentences are separated by a period and a single space.

The conversion happens when the user exits the field just prior to committing the value.

var para = event.value; 
var sentences = para.split(". ");
for (var i=0; i < sentences.length; i++) {
     sentences = sentences.trim().charAt(0).toUpperCase() + sentences.slice(1);
}
event.rc = true;
event.value = sentences.join(". ");
Translate
Participant
May 10, 2017 4:54 AM UTC

Thanks Joel, I will give this a try when I get into the office

Connie

Translate
Participant
May 5, 2017 9:58 PM UTC

Thank you for answering my question. 

We would want to capitalize the first character in the text then it would be the first non-space character after a period.  Yes the first non-space character should be capitalized.

Translate
Joel Geraci
Community Expert
Community Expert
May 5, 2017 10:11 PM UTC

It's easy enough to correct the capitalization when the user exits the field but correcting it as they type will take a bit of work. The case where a user is just typing is pretty easy but you'd also need to consider a user pasting text into a field or inserting text into a field that's already populated.

Do you just want it fixed before they commit the field or as they type/edit the field?

Translate
Joel Geraci
Community Expert
Community Expert
May 5, 2017 9:51 PM UTC

Do you care how many spaces come after the period or do you need to capitalize the first non-space character after a period? Also, are we assuming that the first non-space character of the field should be capitalized?

Translate