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

Capitalize first letter of word not working

cyfu
Registered: Apr 26 2011
Posts: 32
Answered

I used this code from another post:
 
function toTitleCase(s)
{
var re1 = /(\w)(\w*)/; // Make an array of words that are in the input string
var a1 = s.split(/\s+/g);
 
for (i = 0; i < a1.length; i++ )
{
var a2 = a1[i].match(re1); // Capitalize first character of word
a1[i] = a2[1].toUpperCase() + a2[2];
// Alternatively, also convert rest of word to lower case
a1[i] = a2[1].toUpperCase() + a2[2].toLowerCase(); }
return a1.join(" ");
}
 
In the field action, i'm using a mouse up event to run the following javascript to call the function:
 
this.getField("name").value = toTitleCase(("name")).value;
 
The errors i'm getting from the debugger is this.
 
InvalidSetError: Set not possible, invalid or unknown.
Field.value:1:Field name:Keystroke

My Product Information:
Acrobat Pro 7.0, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If this script is in any action for the "name" field, you should be using the 'event' object and not the field field object.

You also have to pass the field value to the function and not the character string of the field name.

event.value = toTitleCase(event.value);

George Kaiser

cyfu
Registered: Apr 26 2011
Posts: 32
Changed it to event.value = toTitleCase(event.value);

Now, i'm getting the following error:

9:Document-Level:Capitalize
TypeError: a2 has no properties
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
There is a much simpler script for capitalizing the first letter of a word

str = this.getField("name").value

event.value = str.replace(/\b\w/g,function(cWrd){return cWrd.toUpperCase()});

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

cyfu
Registered: Apr 26 2011
Posts: 32
Thank you Thom!
Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
cyfu wrote:
Thank you Thom!
+++
:-)
Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
thomp wrote:
There is a much simpler script for capitalizing the first letter of a word
str = this.getField("name").value
event.value = str.replace(/\b\w/g,function(cWrd){return cWrd.toUpperCase()});
Hi Thomp,

there is an issue in this script.
Entering accented characters cause the following letter to be capitalized…

Example : entering the word "thérèse" cause it to be formated as "ThéRèSe".

Can this be solved?

Thanks.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The code is grabbing the first letter at a word boundary. So it seems JS regular expressions see the accented character as a word boundary. Interesting because there characters are in the western European ASCII set. Looks like a bug in the core JS engine:(

The only solution I can find is to rewrite the regular expression to be more explicit about the word boundary, such as

/(^|\s|[\-\,\.])\w/

I think you've have to add in quite a few more punctuation marks to be totally covered.

I did some testing and apparently the accented characters register a non-word characters:)

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
thomp wrote:
Looks like a bug in the core JS engine:(
Argglll !
:-(

Your script works fine with accented characters but it works only onto the first word, not onto following.

What I need is to capitalize any word preceded by a space or by a "-" (since coumpound names are made with a "-").

Can I do this modifying the regex ?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, you can do it by modifying the RegEx. I didn't include the global flag on my code above. It should be.

/(^|\s|[\-\,\.])\w/g


There is an art to writing Regular expressions, and there is also web site on this topic. I have a link to it in this article.Regular Expressions.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
I'm a GREP user but… I use it only in InDesign… but metacharacters are not exactly the same, that's why I'd asked you about the regex.

So, you're last regex works fine.

Thanks you very much! (once again).

;-)