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

To convert Firstname and Lastname's first char to uppercase.

aksahoo17
Registered: Sep 20 2010
Posts: 5

Hi,
I need help, to convert a string of Firstname and Lastname to uppercase(only First character of both).
I tried as below but fail....so plz
 
var nam = String(dataFld);
var length = nam.length;
var nam1 = nam.charAt(0).toUpperCase();
var i=1;
do
{
if(nam[i]==" ")
nam1 += nam[i+1].toUpperCase();
else
nam1 += nam[i];
i++;
}while(i<length);

My Product Information:
LiveCycle Designer, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
There are a number of ways to do this, but when doing names it can get you into trouble. Here's a function that converts the first letter of each word in a string to upper case, and optionally converts the other letters in each word to lower case. A side effect is converting multiple spaces to single space.

function toTitleCase(s) { // Regular expression for a letter, and followed by none or more lettersvar re1 = /(\w)(\w*)/; // Make an array of words that are in the input stringvar a1 = s.split(/\s+/g); // Loop through each wordfor (i = 0; i < a1.length; i++ ) { // Split word into first letter// followed by the rest of wordvar a2 = a1[i].match(re1); // Capitalize first character of worda1[i] = a2[1].toUpperCase() + a2[2]; // Alternatively, also convert rest of word to lower case//a1[i] = a2[1].toUpperCase() + a2[2].toLowerCase(); } // Return new string built from words in array, separated by spacesreturn a1.join(" ");}
aksahoo17
Registered: Sep 20 2010
Posts: 5
function chkValidName(dataFld)
{

var re1 = /(\w)(\w*)/;

//Make an array of words that are in the input string
var a1 = dataFld.split(/\s+/g);

// Loop through each word;
for( i = 0; i < a1.length ; i++)
{

// Split word into first letter
// followed by the rest of word
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 new string built from words in array, separated by spaces
var nam = a1.join(" ");

resolveNode(dataFld).rawValue = resolveNode(nam).rawValue;

}

I using this code as for your instruction ....
still then no result....

Can U plz check this.

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The function was intended to be a general purpose function that returns a value based on the string that you pass to it. You would call it like this:

// Set field value to title casexfa.resolveNode("dataFld").rawValue = toTitleCase(xfa.resolveNode("nam").rawValue);
or

xfa.resolveNode("nam").rawValue = toTitleCase(xfa.resolveNode("dataFld").rawValue);
depending on which field is your input and which is your output. It not clear to me which is which.


aksahoo17
Registered: Sep 20 2010
Posts: 5
Hello Sir as I m a beginner... i send U the total coding like..

Form.variables.validate.chkValidNameBlock("this");


function chkValidName(dataFld)
{
var re1 = /(\w)(\w*)/;

//Make an array of words that are in the input string
var a1 = dataFld.split(/\s+/g);// Loop through each word;
for( i = 0; i < a1.length ; i++)
{
// Split word into first letter
// followed by the rest of word
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 new string built from words in array, separated by spaces
var nam = a1.join(" ");
resolveNode(dataFld).rawValue = resolveNode(nam).rawValue;

}


as toTitleCase() is a user defined function. I can Change it...
Thanks.