Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

javascript color border

New Here ,
Feb 20, 2023 Feb 20, 2023

hello I am looking for an example of a javascript script for a form on acrobat as soon as the form field contains data I would like the border to be a blue color and as soon as the form field has no data I would like it to be red in color

TOPICS
JavaScript , PDF forms
3.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2023 Feb 20, 2023

Use a custom Validation or Custom Format Script

if(event.value && event.value.toString().length)
  event.target.strokeColor = color.blue;
else
  event.target.strokeColor = color.red;

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 20, 2023 Feb 20, 2023

first of all thank you for your return but the problem is that the event does not work to fill out a form I have to go through this.getField I have already tested

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2023 Feb 20, 2023

The script I posted is correct and works.

 

Did you try it out?  In what event did you enter it?

Were any errors reported in the Console Window (Ctrl-J)?

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 21, 2023 Feb 21, 2023

I inject the data into the form via a crm

if I pass by putting your code directly in Adobe the event is taken into account and the color changes

but as soon as I inject them via the crm they are not taken into account I must in the javascript of the form variable I must put my javascript in the global document here is an example that I have tested but which only works halfway

{if (this.getField("works.remarks.1.remarks").value != "")

{
this.getField("works.remarks.1.remarks").borderWidth = 1;

}
else

{
this.getField("works.remarks.1.remarks").borderWidth = 0;
this.getField("works.remarks.1.remarks").borderColor = color.black;
}
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2023 Feb 21, 2023

All JavaScript is event driven.  So if you are "injecting it via a CRM", then it is being run in an event. I suspect it is running immediately against the document object. This won't work for you.  Why can't you modify the form up front with the correct code before it is added to the CRM system? 

Also, this method, "injection via CRM" is critical information. You did us all a disservice by not explaining this in your first post. In fact you ignored it for several posts. If you want help, you have to provide all relevant information. 

 

If this is not possible, the use the "field.setAction()" function in your CRM injection to put the script where it needs to be. 

Here's an example using the pattern provided by ls_rbs above:

 

var strValScript = "event.target.strokeColor = (event.value && event.value.toString().length)?color.blue:color.red;";

this.getField("works.remarks.1.remarks").setAction("Validate",strValScript);

 

 

 If the form is then saved, then the validation script will be saved with it.

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 21, 2023 Feb 21, 2023

I just took the test again
it works when i open it with adobe but when i inject the data with the crm it doesn't work

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2023 Feb 21, 2023
LATEST

Are you sure Acrobat is the viewer used by the CRM app? 

Are there any errors reported by the CRM app when the code is injected?

Did you check the PDF after injection to see if the Validate script was actually inserted?

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2023 Feb 20, 2023

Hi ,

 

This can be accomplished with various scripting methods.

 

In my example below, I decided to use a custom fomat keystroke script.

 

(event.willCommit && (event.value =="")) ? event.target.strokeColor = color.red : event.target.strokeColor = color.blue;

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines