These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Javascript for Title Case - Force First Letter of Every Word to Uppercase with Exceptions

keetan
Registered: Nov 11 2010
Posts: 3
Answered

I am currently using the following Javascript for Title Case, but I would like to include exceptions (i.e. last names starting with Mc, force uppercase of State abbreviations, and abbreviations for North East - NE, etc.).
 
---Beginning of JavaScript - Entered at Document Level---
function UpperCase(sString) {
return sString.toUpperCase();}
 
function LowerCase(sString) {
return sString.toLowerCase();}
 
function FirstUpperCase(sInput) {
var aInput = sInput.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
---Beginning Script Added to Field's Custom Keystroke Script---
event.value = event.value.toLowerCase().replace(/\b\w/g, function(match){return match.toUpperCase();});
---End of Keystroke Script---
 
For example, I enter the following:
 
john mcelroy
123 any street ne
anywhere, ca 92130
 
Which is converted to:
 
John Mcelroy
123 Any Street Ne
Anywhere, Ca 92130
 
...but it needs to be the following:
 
John McElroy
123 Any Street NE
Anywhere, CA 92130
 
Any suggestions would be greatly appreciated.

Thank you,
Keeta

My Product Information:
Acrobat Pro 9.3.1, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2399
This kind of processing is far from trivial.
I would completely separate the different steps. Start with your generic TitleCase script, and then create another method (or methods) that looks for special cases.

For example, this method will convert any two-letter word to upper-case:

function specialCaseStateCode(str) {
if (/^[a-zA-Z]{2}$/.test(str))
str = str.toUpperCase();
return str;
}

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

keetan
Registered: Nov 11 2010
Posts: 3
What code needs to be added to the field level (Custom Kestroke Script) to call this function?

Thank you,
Keeta

try67
Expert
Registered: Oct 30 2008
Posts: 2399
You need to create a loop over each word and pass it to this function.
Something like:

var words = event.value.split(" ");
for (i=0; i < words.length; i++) {
words[i] = specialCaseStateCode(words[i]);
}
event.value = words.join(" ");

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

keetan
Registered: Nov 11 2010
Posts: 3
Thank you sooooo very much! That worked. :o)

Thank you,
Keeta