Skip to main content
Latitude2018
Participant
December 27, 2018
Answered

MIN validation without ZERO

  • December 27, 2018
  • 1 reply
  • 949 views

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 has been closed for replies.
Correct answer try67

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;

}

1 reply

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?

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.

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;

}