Uppercase the first letter only in a field using JS

How can I tweak the keystroke event
if(!event.willCommit)
event.change = event.change.toUpperCase();
so that only the first letter of every word will be capitalised inside a field displaying a first name. It needs to deal with the following possibilities:
John
John Carl
John-Carl
John C. Carl


Mathieu Lacasse


7 Answers

Voted Best Answer

The problem with the keystroke event is that it is called at two different times, while the user is typing and when the entered data is commited to the field value. You need to detect the first character that the user types into the field. The event.value property holds the data already typed in, so if this value is empty the data in event.change is the first.

if(!event.willCommit)
{
if(event.value == "")
event.change = event.change.toUpperCase();
}
else
{
.. Put any fix up here for the committed data ..
(for example, the code JR Boulay provided)
}

This first part of the script, where the user is typing, does not work for the case where the user pastes overtop of existing text, or data is set thorugh a script or import. For those cases the "else" fixes up the whole value entered.

Thom Parker
The source for PDF Scripting Info pdfscripting.com

The Acrobat JavaScript Reference, Use it Early and Often

The most important JavaScript Development tool in Acrobat
The Console Window (Video tutorial)
The Console Window(article)

Having trouble, Why Doesn't my Script Work


By Thom Parker   

Try this custom keystroke JavaScript

if (event.value!=null && event.value!="") {

event.value = event.value.substr(0,1).toUpperCase() + event.value.substr(1);

}


Eugene Williams   

I found this one which deals with word separated by a space " ".

var aInput = event.value.split(" "); // make an array split at space
var sCharacter = '';
var sWord='';

// for each element of word array, capitalize the first letter
for(i = 0; i <aInput.length; i++) {
aInput[i] = aInput[i].substr(0, 1).toUpperCase() + aInput[i].substr(1) .toLowerCase();
} // end loop for input string
// rebuild input string with modified words with spaces
event.value = aInput.join(' ');

I need to adapt this to add words also separated by "-".

Would it work if i just repeat the same script removing the part where it sets "other letters" to lowercase. Would it be like a 3 steps process?
1. split at " " and capitalize next letter
2. lowercase other characters
3. split at "-" and capitalisze next letter


Mathieu Lacasse   

It worked!!! Here's the result:

var aInput = event.value.split(" "); // make an array split at space
var sCharacter = '';
var sWord='';


// for each element of word array, capitalize the first letter
for(i = 0; i <aInput.length; i++) {
aInput[i] = aInput[i].substr(0, 1).toUpperCase() + aInput[i].substr(1) .toLowerCase();
} // end loop for input string
// rebuild input string with modified words with spaces
event.value = aInput.join(' ');


var aInput2 = event.value.split("-"); // make an array split at space
var sCharacter2 = '';
var sWord2='';


// for each element of word array, capitalize the first letter
for(i = 0; i <aInput2.length; i++) {
aInput2[i] = aInput2[i].substr(0, 1).toUpperCase() + aInput2[i].substr(1);
} // end loop for input string
// rebuild input string with modified words with spaces
event.value = aInput2.join('-');


Mathieu Lacasse   

Hi,

try this one as a "Custom format script", it capitalise any first letters of any word preceded by a " - " or by a space :

str = event.target.value;event.value = str.replace(/(^|\s|[\-\,\.])\w/g,function(cWrd){return cWrd.toUpperCase()});

;-)


JR Boulay   

Thanks Thom,
This is really helpful for me.
:-)


JR Boulay   

Check this one.....

function allTitleCase(inStr)
{
return inStr.replace(/\w\S*/g, function(tStr)
{
return tStr.charAt(0).toUpperCase() + tStr.substr(1).toLowerCase();
});
}

Full Source

http: // www. corelangs. com /js /string / cap. html


Mark


jovier mark   


Please specify a reason: