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

Add validation script to multiple fields using setAction?

Community Beginner ,
May 12, 2021 May 12, 2021

I want to add this script to multiple fields as a validation script.

if (event.value==0) (event.value = "");

 

I created a document level function:

 function valZero()
{
if (event.value==0) (event.value = "");
}

 

I tried running it in the console and also as a custom command

this.getField("FieldName").setAction("Validate"," valZero();"); 

 

I also tried writing it out instead of using my function:

 

this.getField("FieldName").setAction("Validate"," if (event.value==0) (event.value = "");"); 

 

What am I doing wrong?

TOPICS
JavaScript , PDF forms
1.9K
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 ,
May 12, 2021 May 12, 2021

Hi,

 

Have you tried using the code on a field validation script using the UI to make sure it functions as expected?

And what is not working? a bit more information would be useful, is there anything in the console?

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 ,
May 12, 2021 May 12, 2021

You have several errors both in your function and in your second setAction command, which is why neither worked.

In the function change this line:

if (event.value==0) (event.value = "");

To:

if (event.value==0) event.value = "";

 

In the second setAction you also didn't escape the quotes. So change this:

this.getField("FieldName").setAction("Validate"," if (event.value==0) (event.value = "");");

To this:

this.getField("FieldName").setAction("Validate","if (event.value==0) event.value = \"\";");

 

I would use the function version, though, if you want to apply it to multiple fields.

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 Beginner ,
May 12, 2021 May 12, 2021

So if I fix the syntax in the function, is this the correct statement to run in the console?

 

this.getField("FieldName").setAction("Validate"," valZero();");

 

Can you explain why the second set of parantheses needed to be removed? 

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 ,
May 12, 2021 May 12, 2021
LATEST

Because a block of code is delimited with curly brackets, or none at all (if it's a single line).

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