Skip to main content
Participant
June 5, 2019
Answered

Making several fields required if value in prior field

  • June 5, 2019
  • 1 reply
  • 510 views

Hey there.

I have a form for naming corporate officers. One company can have 2, another can have 22 so what I would like to do is:

If information is entered into "First Name" then require

"Last Name"

"Title"

"DOB"

Any help is GREATLY appreciated!!!

This topic has been closed for replies.
Correct answer Thom Parker

You are sooo close.  Try this.

var sVal = getField("ar_first_name_2").valueAsString;

if (sval != "") {

getField("ar_last_name_2").required = true;

getField("ar_title_position_2").required = true;

getField("ar_dob_2").required = true;

getField("ar_address_2").required = true;

getField("ar_city_2").required = true;

getField("ar_state_2").required = true;

getField("ar_postalcode_2").required = true;

}

Actually, you should use this code, so that required is turned off if the user deletes the first name.

This code is placed in a custom, format or validation script.

var bReq = (event.value != "");

getField("ar_last_name_2").required = bReq;

getField("ar_title_position_2").required = bReq;

getField("ar_dob_2").required = bReq;

    ...etc...

And since all the lines required the same code. I'd generalize the code into a document level function. This reduces human error when copying the code to all the different lines and makes it much easier to change the code.

1 reply

Participant
June 6, 2019

Sorry I should have posted what I tried and failed:

var sVal = getField("ar_first_name_2").valueAsString;

if (inputtx.value.length != 0) {

getField("ar_last_name_2").required = true;

getField("ar_title_position_2").required = true;

getField("ar_dob_2").required = true;

getField("ar_address_2").required = true;

getField("ar_city_2").required = true;

getField("ar_state_2").required = true;

getField("ar_postalcode_2").required = true;

}

and also tried this one to no avail.  

var sVal = getField("ar_first_name_2").valueAsString;

if (!sval) {

getField("ar_last_name_2").required = true;

getField("ar_title_position_2").required = true;

getField("ar_dob_2").required = true;

getField("ar_address_2").required = true;

getField("ar_city_2").required = true;

getField("ar_state_2").required = true;

getField("ar_postalcode_2").required = true;

}

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
June 6, 2019

You are sooo close.  Try this.

var sVal = getField("ar_first_name_2").valueAsString;

if (sval != "") {

getField("ar_last_name_2").required = true;

getField("ar_title_position_2").required = true;

getField("ar_dob_2").required = true;

getField("ar_address_2").required = true;

getField("ar_city_2").required = true;

getField("ar_state_2").required = true;

getField("ar_postalcode_2").required = true;

}

Actually, you should use this code, so that required is turned off if the user deletes the first name.

This code is placed in a custom, format or validation script.

var bReq = (event.value != "");

getField("ar_last_name_2").required = bReq;

getField("ar_title_position_2").required = bReq;

getField("ar_dob_2").required = bReq;

    ...etc...

And since all the lines required the same code. I'd generalize the code into a document level function. This reduces human error when copying the code to all the different lines and makes it much easier to change the code.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
June 6, 2019

Thom,

Thank you SOOO much, especially with the extra part of if they go in, type something, and then delete it!  You ROCK