rsheldon Posted December 31, 2015 Share Posted December 31, 2015 Good Morning, I am using VDP Creator 9.3.15 in Acrobat 11.0.13 on a Mac OS 10.10.5. The problem I am having is creating a rule that searches the first name field for any letter and a . and replaces with the field prefix and field last name. currently I have a rule that is written: { if (Len(Field("FIRST NAME"))<=2) return "Dear "+ToTitleCase(Field("PREFIX"))+"."+" "+Field("LAST NAME")+","; else if (Field("FIRST NAME")!="") return "Dear "+Field("FIRST NAME")+","; else return "Dear "+ToTitleCase(Field("PREFIX"))+"."+" "+Field("LAST NAME")+","; } This works, but if the first name is "Al" or "AC", the rule returns prefix and last, but my client would like the first name Quote Link to comment Share on other sites More sharing options...
David Miller Posted December 31, 2015 Share Posted December 31, 2015 (edited) Good Morning, I am using VDP Creator 9.3.15 in Acrobat 11.0.13 on a Mac OS 10.10.5. The problem I am having is creating a rule that searches the first name field for any letter and a . and replaces with the field prefix and field last name. currently I have a rule that is written: { if (Len(Field("FIRST NAME"))<=2) return "Dear "+ToTitleCase(Field("PREFIX"))+"."+" "+Field("LAST NAME")+","; else if (Field("FIRST NAME")!="") return "Dear "+Field("FIRST NAME")+","; else return "Dear "+ToTitleCase(Field("PREFIX"))+"."+" "+Field("LAST NAME")+","; } This works, but if the first name is "Al" or "AC", the rule returns prefix and last, but my client would like the first name You could try removing the period and find out if the length of the first name is less than or equal to 1. var firstName = Field("FIRST NAME").replace(/\./g,''); var thePrefix = Field("PREFIX").replace(/\./g,''); if (firstName.length <= 1) return "Dear " + ToTitleCase(thePrefix) + "." + " " + Field("LAST NAME") + ","; else return "Dear " + ToTitleCase(thePrefix) + "." + " " + Field("LAST NAME") + ","; Removing the periods from the prefix might be helpful should one exist. It would eliminate the possibility of multiple periods appearing after the prefix in your code. Edited December 31, 2015 by David Miller Quote Link to comment Share on other sites More sharing options...
rsheldon Posted December 31, 2015 Author Share Posted December 31, 2015 Thank you David, I had to change the Len portion of the rule but removing the "." and then searching the length works. I have tested on names like A.J., Ed, Al, A., A. Jacqui, etc. Quote Link to comment Share on other sites More sharing options...
step Posted December 31, 2015 Share Posted December 31, 2015 You could try removing the period and find out if the length of the first name is less than or equal to 1. var firstName = Field("FIRST NAME").replace(/\./g,''); var thePrefix = Field("PREFIX").replace(/\./g,''); if (firstName.length <= 1) return "Dear " + ToTitleCase(thePrefix) + "." + " " + Field("LAST NAME") + ","; else return "Dear " + ToTitleCase(thePrefix) + "." + " " + Field("LAST NAME") + ","; Removing the periods from the prefix might be helpful should one exist. It would eliminate the possibility of multiple periods appearing after the prefix in your code. Removing the periods in both cases is a good idea provided you're planning on adding them back. In the case of the "firstName" variable, a value of "A.J." gets altered to "AJ" but the periods are not added back. Also, I believe the if statement should be (note that you don't really need the 'else' statement since you're just going to return the line if the 'if' condition is met): if (firstName.length <= 1) return "Dear " + ToTitleCase(thePrefix) + "." + " " + Field("LAST NAME") + ","; [color="Red"]// else return "Dear " + Field("FIRST NAME") + ",";[/color] Another way to do this is using the test method. Which allows you to test whether a variable meets certain conditions or not (without altering the variable). For example: var name = Field("FirstName"); if (/^\w\.?$/i.test(name)) name = [ToTitleCase(Field("PREFIX").replace(/(\w)\.?\s*$/, '$1.')), Field("LAST NAME")].filter(String).join(' '); return name ? 'Dear ' + name + ',': ''; The code above sets the 'name' variable to the first name. Then it checks to see if 'name' only contains one letter (and potentially a period) regardless of the casing. If it is determined that the first name is an initial, the 'name' variable is set to the prefix followed by the last name. I should mention that I'm using an array to filter and join those values with a space in case one of them is empty. I'm also replacing the last letter of the "LAST NAME" field with the last letter and a period rather than adding a period to the prefix in case the prefix field is empty. Finally, if the 'name' field is not empty, it is prepended with 'Dear' and returned. If not, a null string is returned. The above code prevents lines such as: "Dear . Lastname," "Dear . ," Quote Link to comment Share on other sites More sharing options...
David Miller Posted December 31, 2015 Share Posted December 31, 2015 Removing the periods in both cases is a good idea provided you're planning on adding them back. In the case of the "firstName" variable, a value of "A.J." gets altered to "AJ" but the periods are not added back. Also, I believe the if statement should be (note that you don't really need the 'else' statement since you're just going to return the line if the 'if' condition is met): if (firstName.length <= 1) return "Dear " + ToTitleCase(thePrefix) + "." + " " + Field("LAST NAME") + ","; [color="Red"]// else return "Dear " + Field("FIRST NAME") + ",";[/color] Thank you for catching that. I deleted the wrong lines from the supplied code, totally botching the last two lines. Basically, I thought the intent was to check and see if the FIRST NAME field was an initial or empty. If so, return the PREFIX and LAST NAME. If not, return the FIRST NAME. (Not the firstName variable.) Another way to do this is using the test method. Which allows you to test whether a variable meets certain conditions or not (without altering the variable). For example: var name = Field("FirstName"); if (/^\w\.?$/i.test(name)) name = [ToTitleCase(Field("PREFIX").replace(/(\w)\.?\s*$/, '$1.')), Field("LAST NAME")].filter(String).join(' '); return name ? 'Dear ' + name + ',': ''; Agreed. A much better solution. Thank you. (I'll need to work on my RegExp knowledge though.) Quote Link to comment Share on other sites More sharing options...
step Posted December 31, 2015 Share Posted December 31, 2015 Basically, I thought the intent was to check and see if the FIRST NAME field was an initial or empty. If so, return the PREFIX and LAST NAME. If not, return the FIRST NAME. (Not the firstName variable.) I believe you are correct in that assumption. I was just pointing out the danger of returned the 'firstName' variable after performing a replace on it if that had been your intention. And as I'm reading that, I realized that my solution doesn't correctly handle the "FIRST NAME" being empty. To account for that you'd have to add: var name = Field("FirstName"); if (/^\w\.?$/i.test(name) [color="Red"]|| !name[/color]) name = [ToTitleCase(Field("PREFIX").replace(/(\w)\.?\s*$/, '$1.')), Field("LAST NAME")].filter(String).join(' '); return name ? 'Dear ' + name + ',': ''; Agreed. A much better solution. Thank you. (I'll need to work on my RegExp knowledge though.) As rsheldon pointed out, your solution worked for him so I don't think my solution is "better" by any means. I just wanted to share an alternate method. As far as RegExp goes, that's another part that could be approached from many different angles but in particular to the two expressions I wrote, here is the break down using the RegExp Constructor to allow for easy commenting: exp = new RegExp( '^' + // Match starting at the beginning of the line '\\w' + // Matches an alphanumeric character '\\.' + // Matches a period '?' + // Matches the preceeding (in this case the period) 0 or 1 times '$', // End of the string 'i'); // Case-insensitive flag return exp.test('a.'); // returns true exp = new RegExp( '(' + // Starts a capture group - Saved as $1 '\\w' + // Matches an alphanumeric character ')' + // Closes the capture group '\\.?' +// Matches a period 0 or 1 times '\\s' + // Matches a space '*' + // Matches the preceeding character (in this case a space) 0 or more times '$', // End of the string ''); // No flags necessary here. return 'Mr '.replace(exp,'$1.'); // returns "Mr." Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.