These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Need script to require one field or the other, but not both.

skiebus
Registered: Feb 7 2011
Posts: 5

In an Acrobat document, I have two fields, "Father or Legal Guardian" and "Mother or Legal Guardian". The applicant may have one, the other, or both. I need either one of the two to be required, but not both. In other words, only one of the fields HAS to have data entered and it can be either one.
I can't seem to find a script to cover this? Any input?
Thanks!
Steve

My Product Information:
Acrobat Pro 9.3.1, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You dynamically change the required property for the 'father' field and 'mother' field as either is completed or cleared. So if either or both the fields has a value, both the 'father' and 'mother' fields are not required, but if both are empty, then both fields' property are marked required.

Or you just check to see if one or the other has data, because you do not care if both have data.

George Kaiser

skiebus
Registered: Feb 7 2011
Posts: 5
gkaiseril wrote:
You dynamically change the required property for the 'father' field and 'mother' field as either is completed or cleared. So if either or both the fields has a value, both the 'father' and 'mother' fields are not required, but if both are empty, then both fields' property are marked required.Or you just check to see if one or the other has data, because you do not care if both have data.
I am not sure how to "dynamically change the required property for the 'father' field and 'mother' field as either is completed or cleared". Can you explain?
Thanks for your help!
Steve

George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
If by "required" you mean that the field needs to have a non-blank value before a Submit Form action can proceed, you can use the following custom Validate scripts in the fields:

// Validate script for Father text field
(function () {

var f = getField("Mother");

// If both fields are blank, set both fields to required...
if (!event.value && !f.valueAsString) {
f.required = true;
event.target.required = true;
} else { // ...otherwise, neither is required
f.required = false;
event.target.required = false;
}

})();

// Validate script for Mother text field
(function () {

var f = getField("Father");

// If both fields are blank, set both fields to required...
if (!event.value && !f.valueAsString) {
f.required = true;
event.target.required = true;
} else { // ...otherwise, neither is required
f.required = false;
event.target.required = false;
}

})();

Just change the field names in the getField lines to match the actual names of the fields on your form.
skiebus
Registered: Feb 7 2011
Posts: 5
Thanks!
Does this go in the mother/father fields or in the submit button?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The comments at the top of code list indicate the action (validate) and the field.

George Kaiser

skiebus
Registered: Feb 7 2011
Posts: 5
gkaiseril wrote:
The comments at the top of code list indicate the action (validate) and the field.
Actually, I knew that, don't know what made me ask. It didn't work and I was trying to think of why.
It still lets me submit with both fields empty.
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
You have to change the value of one of the fields before it will start working, and make sure to change the field names I used in the code.
skiebus
Registered: Feb 7 2011
Posts: 5
George_Johnson wrote:
You have to change the value of one of the fields before it will start working, and make sure to change the field names I used in the code.
Sorry :( What do you mean by "change the value of one of the fields before it will start working"?


I did add the names of my fields.
// Validate script for Father text field
(function () {

var f = getField("Mother or Legal Guardian");

// If both fields are blank, set both fields to required...
if (!event.value && !f.valueAsString) {
f.required = true;
event.target.required = true;
} else { // ...otherwise, neither is required
f.required = false;
event.target.required = false;
}

})();


and

// Validate script for Mother text field
(function () {

var f = getField("Father or Legal Guardian");

// If both fields are blank, set both fields to required...
if (!event.value && !f.valueAsString) {
f.required = true;
event.target.required = true;
} else { // ...otherwise, neither is required
f.required = false;
event.target.required = false;
}

})();


George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
skiebus wrote:
Sorry :( What do you mean by "change the value of one of the fields before it will start working"?
Just enter something in either of the fields and hit Enter or click outside of the field. This will trigger the Validate script for that field and set the required properties of the two fields.