• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Making textField required when checkbox is ticked

New Here ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 25, 2018 Apr 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.

Votes

Translate

Translate
LEGEND ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

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"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

Thanks that was it

Appreciate all your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2018 Apr 25, 2018

Copy link to clipboard

Copied

One can use JavaScriptś case sensitivity by using the string methods toLowerCase() and toUpperCase() methods.

Acrobat JavaScript also has a defaultValue so one can test any field to see if it is set to the default value for that field. This will allow one to generically test a field´s default value without having to know the exact value.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 16, 2018 Aug 16, 2018

Copy link to clipboard

Copied

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. Screen Shot 2018-08-16 at 1.05.32 PM.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 16, 2018 Aug 16, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 20, 2018 Aug 20, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 21, 2018 Aug 21, 2018

Copy link to clipboard

Copied

LATEST

But when the answer is changed to "No"  after a phone number has been entered the phone number remains and then if the radio button is changed back to yes is the possible wrong number updated?

How would change the script if a new radio button is added to the group for "N/A"?

Since one can uncheck a selected check box within a group, how does your script handle the value of "Off"?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines