Skip to main content
Participant
May 5, 2017
Answered

capitalize first charcter sentence

  • May 5, 2017
  • 3 replies
  • 1075 views

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 has been closed for replies.
Correct answer Joel Geraci

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(". ");

3 replies

Participant
May 6, 2017

Joel,

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

Joel Geraci
Community Expert
Joel GeraciCommunity ExpertCorrect answer
Community Expert
May 9, 2017

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(". ");
Participant
May 10, 2017

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

Connie

Participant
May 5, 2017

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.

Joel Geraci
Community Expert
Community Expert
May 5, 2017

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?

Joel Geraci
Community Expert
Community Expert
May 5, 2017

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?