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

Add validation script to multiple fields using setAction?

Community Beginner ,
May 12, 2021 May 12, 2021

Copy link to clipboard

Copied

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

Views

1.5K

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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? 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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