Skip to main content
Latitude2018
Participant
December 27, 2018
Answered

MIN validation without ZERO

Dear All,

I have 4 text fields with number values, where I would like validate the MIN value without zero. Which means that the validation script should ignore zero value(s) and only validate the min value among values, which are (x) > 0. Any idea for a Javascript? Thanks in advance?

Best regards,

Angela.

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
December 27, 2018

OK, for future reference, this is called a calculation in the PDF world, not a validation. A validation is when you want to verify that the value of a field follows a specific pattern.

Anyway, you can do it using the following code as the custom calculation script of your minimum-value field (I've also added a command to set the average field value, as you requested):

var fields = ["Field 1", "Field 2", "Field 3", "Field 4"];

var values = [];

var total = 0;

var n = 0;

for (var i in fields) {

    var v = Number(this.getField(fields).valueAsString);

    if (v!=0) {

        values.push(v);

        total+=v;

        n++;

    }

}

if (n==0) {

    event.value = "";

    this.getField("Average").value = "";

} else {

    values.sort(function (a,b) {return a - b;});

    event.value = values[0];

    this.getField("Average").value = total/n;

}

4 replies

try67
Community Expert
Community Expert
December 27, 2018

In what way is this a validation? Do you just mean that you want to display the lowest (non-zero) number?

And what are the names of the fields?

Translate
Latitude2018
Participant
December 27, 2018

Hello try67,

many thanks for your prompt reply. Yes, the validation filed should show the lowest (non-zero) number.

Field names of validated values: Field 1, Field 2, Field 3, Field 4

In addition I would like to add a second field for validation of the average value of the 4 o.m fields, with exclusion of zero values.

Many thanks for your support.

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 27, 2018

OK, for future reference, this is called a calculation in the PDF world, not a validation. A validation is when you want to verify that the value of a field follows a specific pattern.

Anyway, you can do it using the following code as the custom calculation script of your minimum-value field (I've also added a command to set the average field value, as you requested):

var fields = ["Field 1", "Field 2", "Field 3", "Field 4"];

var values = [];

var total = 0;

var n = 0;

for (var i in fields) {

    var v = Number(this.getField(fields).valueAsString);

    if (v!=0) {

        values.push(v);

        total+=v;

        n++;

    }

}

if (n==0) {

    event.value = "";

    this.getField("Average").value = "";

} else {

    values.sort(function (a,b) {return a - b;});

    event.value = values[0];

    this.getField("Average").value = total/n;

}

Translate