Skip to main content
Participant
Jun 5, 2019 11:36 PM UTC
Answered

Making several fields required if value in prior field

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 is closed to new replies. Start a new post to keep the conversation going.
Thom Parker
Thom ParkerCorrect answer
Community Expert
Jun 6, 2019 1:15 AM UTC

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

3 replies

Participant
Jun 6, 2019 12:52 AM UTC

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;

}

Translate
Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
Jun 6, 2019 1:15 AM UTC

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.

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
Jun 6, 2019 1:17 AM UTC

Thom,

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

Translate