Adding line breaks to a multi-line field using a custom format script

Copy link to clipboard
Copied
.Hi Everyone,
I'm creating a PDF that generates a report, but I'm stuck on how to automatically format the final text field, specifically to get line breaks before each bullet point.
This is how the report works - Depending on the answer given to a question, the PDF populates 2 fields; one with the positive elements and another with the ways to improve. All the positive answers are then gathered into a text field (reportPositiveFinal) using a simple JavaScript. When a 'Generate Report' button is hit, it triggers a JavaScript to transfer the positive text into the positive final report field (repP), as a string.
The problem - The text that comes out is fine, but there is no formatting. I want to have the text appear as a bulleted list; with a bullet beginning the line and a line break at the end of item.
Partial Solution - The closest I think I got to an answer was by using a custom format script using a regular expression to identify the bullet and replace it with a bullet with a line break before it, but I tried and failed to get it to work. (I'm still not entirely sure what a regular expression is!)
if (event.value!="• ")
event.value=event.value.replace(<br/>"• ")
My coding level is minimal, so I'm way out of my depth.
Is this the right idea? If not, what is the way to go?
Thanks!
Copy link to clipboard
Copied
Form fields don't use HTML code. Try '\r' or '\n'.

Copy link to clipboard
Copied
I tried replacing the <br/> in the code with \r and \n (as below) but both gave the error "Syntax Error: illegal character"
if (event.value!="• ")
event.value=event.value.replace(\r "•")
Any advice?
Copy link to clipboard
Copied
Look at the documentation of 'replace'.

Copy link to clipboard
Copied
I'm sorry. As I said, I'm inexperienced with the code, so I don't know what you mean.
Copy link to clipboard
Copied
You can find the documentation of the replace function on the web.
Search the web for "javascript replace".

Copy link to clipboard
Copied
I've looked and I still can't understand it. As I said, my level of coding skill is close to zero.
Can anyone give me an answer?
Copy link to clipboard
Copied
You want use JavaScript but doesn't understand JavaScript?

Copy link to clipboard
Copied
Yes, that about sums it up.
Primarily I want my PDF to work, but learning some JavaScript is a nice bonus; hence why I'm asking questions.
If there was another way to get the result I'm after, I'd pursue that. JavaScript seems to be the way to go, but I need some help.
Can anyone help?
Copy link to clipboard
Copied
Did you read the documentation of the replace function?

Copy link to clipboard
Copied
I did, but I didn't understand it.
I've tried a number of the methods I've seen;
var ans = this.getField("reportPositiveFinal").value;
var res = ans.replace(/•/g, "\r" + "•");
I even tried creating a button to trigger a replace function, but it didn't work.
Copy link to clipboard
Copied
Your last code is correct, but you're not doing anything with the "res" variable, it seems...

Copy link to clipboard
Copied
Hot diggety damn! Thanks try67!
I added the bold bit of code and it worked!
var ans = this.getField("reportPositiveFinal").value;
var res = ans.replace(/•/g, "\r" + "•");
event.value = res

Copy link to clipboard
Copied
There's a slight problem with this still. It's not major, but if there's a simple answer I'd like to sort it out.
The text displays as it should (Brilliant!) but when you click to enter the field it returns to it's previous unformatted state. Once you click away, it goes back to being correctly formatted using the code. I assume this is because the formatting is being done by the custom format script in the field.
Is there a way to make it so that once the text has been changed by the code, it stays that way?
Copy link to clipboard
Copied
That's problematic. If you change the code to a custom validation script, for example, it will actually change the value, but it will add a new line-break each time you edit the field. To avoid that you will need to change your regular expression so that it only adds a line-break before a bullet if one isn't already there. Also, you would need to access the field's value using event.value.

Copy link to clipboard
Copied
That sounds difficult. I'll just leave it how it is. Thanks for the help!

