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

Validation Script Help

Guest
Oct 18, 2017 Oct 18, 2017

I'm using the following validation script which compares the input argument against the "IC Ref" value and fills the field red or green depending on whether it is greater or less than the "IC Ref" value. 

The problem is that if you input a IC Ref value and fill in the form fields then go back and change the IC Ref value the fill colors do not refresh based off of the new IC Ref value.

Is there a way to get the validation to update based off of a new IC Ref value being entered?

var v = Number(event.value);

var ic = this.getField("IC Ref").value;

if (v>0 && v<=ic) {event.target.fillColor = color.green;}

else if (v>ic) {event.target.fillColor = color.red;}

else event.target.fillColor = color.white;

TOPICS
PDF forms
2.8K
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
Guest
Oct 27, 2017 Oct 27, 2017

Can anybody help with this?  Even if its just to let me know that this isn't possible within an acrobat form?

Thanks

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 ,
Oct 27, 2017 Oct 27, 2017

Where did you add the script?

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
Guest
Oct 27, 2017 Oct 27, 2017

I'm not sure I understand the question?

The script is part of the field's properties under the "Run custom validation script" section of the "Validate" tab.

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 ,
Oct 27, 2017 Oct 27, 2017

A validation script only executes when you change the value of that field. So when you go and edit the IC Ref field it doesn't trigger this event, which is why it's not working for you. Try moving the code to the custom calculation script.

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

When the value of a field is edited, it is changed and has a new value, right?  So why wouldn't a validation script execute?  Pls explain.

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

Have you changed the value of the field?

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
Guest
Oct 27, 2017 Oct 27, 2017

Thanks try67.  This has led to one other little issue for my form. 

One of the fields was averaging the values from 6 other fields and then performing the same validation script on it.  I can't have it perform the simple average function and also have a custom calculation script too so I need to write a simple average function before the validation formula.  All of the examples I'm finding are for much more complex scenarios.

I just need to average 6 cells.  Can you help me withe a quick javascript for this to add to my validation script?

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 ,
Oct 27, 2017 Oct 27, 2017

I've actually written a function that does exactly that, and it's better than the built-in version because you can tell it to ignore empty fields and/or zero values.

/*

    @params

        aFields : an array of the field names to use for the calcluation

        bIgnoreBlanks : a boolean that tells the function whether or not to ignore blank fields

        bIgnoreZeros : a boolean that tells the function whether or not to ignore zero values

*/

function calcAverage(aFields, bIgnoreBlanks, bIgnoreZeros) {

    var total = 0;

    var n = 0;

    for (var i in fields) {

        var f = this.getField(fields);

        if (f==null) {

            console.println("Error! Can't locate a field called: " + fields);

            continue;

        }

        if (f.valueAsString=="" && ignoreBlanks) continue;

        var v = Number(f.valueAsString);

        if (isNaN(v)) continue;

        if (v==0 && ignoreZeros) continue;

        total+=v;

        n++;

    }

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

    else event.value = total/n;

}

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
Guest
Oct 27, 2017 Oct 27, 2017

Forgive my javascript inexperience, but I'm not fully understanding how to implement your function. 

I will always have 6 fields filled with none being zero, and none being empty.  They are called fill0, fill1 etc.  Can you provide a little more guidance on how I would modify your function to work in my form?

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 ,
Oct 27, 2017 Oct 27, 2017

You don't need to modify it at all. Just copy it to a doc-level script, and then call it like this from the custom calculation script of your field:

calcAverage(["fill0", "fill1", "fill2", "fill3", "fill4", "fill5"], false, false);

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
Guest
Oct 27, 2017 Oct 27, 2017

I apologize again, but I'm a very low level Javascript user and I'm not sure what you mean when referring to doc-level script or what "call it" means.

I think I need to find a more basic function that just averages 6 values.

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 ,
Oct 27, 2017 Oct 27, 2017

Just copy the entire code, including the last line I provided, under that field's custom calculation script and it will 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