Skip to main content
francinec46247507
New Participant
April 25, 2018
Answered

Making textField required when checkbox is ticked

  • April 25, 2018
  • 2 replies
  • 1479 views

Hi
I am trying to make a textfield(input) become require if checkbox(special) is ticked.  I have tried everything I can think of.  I believe the problem is with the code for the
checkbox. The submit button works and populates email however it does not check to verify the textbox has something in it if the checkbox is ticked.

Code is on a submit button

var isFormValid = true;

if (this.getField("special").value == 'on' && this.getField("input").value == '')
 
{
  app.alert('Enter Data');
  isFormValid = false;
}
else{

if (isFormValid) {
var mySubject = "Leadership Development Request Form for" + "   " + this.getField("Name").value + "    " + "Circuit" + " " + this.getField("Circuit").value;

this.mailDoc( {
  bUI: true, cTo: "email@mail.com", cSubject: mySubject
})

}}

Any assistance will be appreciated

This topic has been closed for replies.
Correct answer George_Johnson

For this line:

if (this.getField("special").value == 'on' && this.getField("input").value == '')

Make sure that the export value of the checkbox is set to "on", as opposed to "Yes", "On", or anything else.

You should also look at the JavaScript console (Ctrl+J) to see of any errors are shown.

2 replies

New Participant
August 16, 2018

Is there a way to do this with radio buttons? I have a couple of questions in my PDF where if the radio button for yes is checked, I'd like the "If yes, what?" text field to be set as required.

Inspiring
August 16, 2018

Yes one can. You may also want to make the field read only and cleared when the 'No' option is selected. One would need to place the following code in the "Mouse Up" action for the "Yes" and "No" options of the radio button. The only value that needs to be changed is the text field's name.

var oTextField = this.getField("If_yes_what");

if(this.getField(event.target.name).value == "Yes") {

oTextField.readonly = false; // clear readonly;

oTextField.required = true; // make required;

oTextField.setFocus(); // move to text field;

} else {

oTextField.readonly = true; // set to readonly

oTextField.value = ""; // clear field;

oTextField.required = false; // clear required;

}

It could be made more universal and apply to all the radio button/text field combinations by creating a document level function of:

function MakeRequired(cTextField) {

var oTextField = this.getField(cTextField);

if(this.getField(event.target.name).value == "Yes") {

oTextField.readonly = false; // clear readonly;

oTextField.required = true; // make required;

oTextField.setFocus(); // move to text field;

} else {

oTextField.readonly = true; // set to readonly

oTextField.value = ""; // clear field;

oTextField.required = false; // clear required;

}

return true

}

and then call that function for each pair of radio buttons in the "Mouse Up" action with the following script and changing the function's parameter to the text field's name.

MakeRequired("If_yes_what");

All the above would also apply to check boxes.

New Participant
August 20, 2018

I appreciate your help, but found the following less complicated while producing the same result:

"Sure. In the Mouse Up event of each radio button, do something like:

// Mouse Up JavaScript for radio button;

getField("phone_number").required = event.target.value === "Yes" ? true : false;

where "Yes" is the button value of the Yes radio button and "phone_number" is the name of the phone number field. Adjust these to match what you're using." - George_Johnson​, thank you.

The above works for checkboxes as well.

George_JohnsonCorrect answer
Inspiring
April 25, 2018

For this line:

if (this.getField("special").value == 'on' && this.getField("input").value == '')

Make sure that the export value of the checkbox is set to "on", as opposed to "Yes", "On", or anything else.

You should also look at the JavaScript console (Ctrl+J) to see of any errors are shown.

francinec46247507
New Participant
April 25, 2018

Sorry it took me so long to respond to your reply but I had to look up how to change export value.  The export value is already set to "on"

try67
Adobe Expert
April 25, 2018

No, it's not. It's "On", with an upper-case "O"... JavaScript is case-sensitive, so "on" is not the same as "On" to it.