Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Validation Script Help

Community Beginner ,
Oct 18, 2017 Oct 18, 2017

Copy link to clipboard

Copied

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

Views

1.1K

Likes

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

Copy link to clipboard

Copied

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

Thanks

Likes

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

Copy link to clipboard

Copied

Where did you add the script?

Likes

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

Copy link to clipboard

Copied

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.

Likes

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

Copy link to clipboard

Copied

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.

Likes

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

Copy link to clipboard

Copied

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.

Likes

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

Copy link to clipboard

Copied

LATEST

Have you changed the value of the field?

Likes

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

Copy link to clipboard

Copied

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?

Likes

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

Copy link to clipboard

Copied

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;

}

Likes

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

Copy link to clipboard

Copied

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?

Likes

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

Copy link to clipboard

Copied

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);

Likes

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

Copy link to clipboard

Copied

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.

Likes

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

Copy link to clipboard

Copied

Just copy the entire code, including the last line I provided, under that field's custom calculation script and it will work.

Likes

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