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

Javascript for checkboxes and text field

Community Beginner ,
Feb 25, 2022 Feb 25, 2022

Hey there,

 

I haven't dabbled much into validation coding, so any help would be appreciated!

 

I am working on a form that has a set of A and B questions. A and B are yes/no checkboxes (not radio buttons.) Following those, there is a text field asking 'if yes to a or b, explain.'

 

So far, I have it coded to mark the text field required when either 'yes' is checked for A or B. However, if I go through and check both boxes 'yes' and then uncheck one to change to 'no', the field becomes not required.

 

I have tried to fix this problem myself, and have searched around for another problem like this one but am not really sure what I am looking for haha.

 

If someone knows, please reply! Thanks so much!

TOPICS
How to , JavaScript , PDF forms
849
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Feb 25, 2022 Feb 25, 2022

It's good practice to post scripts you used and give us field names that are included in your problem.

From what you posted I understand you have 4 checkboxes 2 for 'yes' 2 for 'no' and text field that should be required if any of 'yes' checkboxes is checked and unrequired if both 'yes' is unchecked?

If you use scripts as 'MouseUP' event you need to check that both 'yes' are unchecked before setting text field back to unrequired in all 4 checkboxes, as try67 suggested it may be easier to have only one script in text field as custom calculation script.

Lets say your 'yes' checkboxes are named "Check Box1" and "Check Box2", as custom calculation script of text field you would use this:

var y1 = this.getField("Check Box1").valueAsString;
var y2 = this.getField("Check Box2").valueAsString;
event.target.required = (y1 == "Off" && y2 == "Off") ? false : true;

View solution in original post

Translate
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 ,
Feb 25, 2022 Feb 25, 2022

Did you use the Validation event of the text field? If so, you shouldn't have, as that is only triggered when that field is edited. Use the Calculation event, instead, and it will be triggered whenever any field is edited.

Or you could use the MouseUp event of the check-boxes themselves.

Translate
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 ,
Feb 25, 2022 Feb 25, 2022

I have them in the MouseUp event of the check-boxes. They trigger properly when both or one field is checked. The issue I run into is specifically when I check both, and uncheck only one. Then the text field becomes unrequired again.

Translate
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 ,
Feb 26, 2022 Feb 26, 2022

If you don't want the fields to be selected at the same time give them the same name but unique export values, and they will automatically become mutually exclusive.

Translate
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 ,
Feb 25, 2022 Feb 25, 2022

It's good practice to post scripts you used and give us field names that are included in your problem.

From what you posted I understand you have 4 checkboxes 2 for 'yes' 2 for 'no' and text field that should be required if any of 'yes' checkboxes is checked and unrequired if both 'yes' is unchecked?

If you use scripts as 'MouseUP' event you need to check that both 'yes' are unchecked before setting text field back to unrequired in all 4 checkboxes, as try67 suggested it may be easier to have only one script in text field as custom calculation script.

Lets say your 'yes' checkboxes are named "Check Box1" and "Check Box2", as custom calculation script of text field you would use this:

var y1 = this.getField("Check Box1").valueAsString;
var y2 = this.getField("Check Box2").valueAsString;
event.target.required = (y1 == "Off" && y2 == "Off") ? false : true;

Translate
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 ,
Mar 03, 2022 Mar 03, 2022
LATEST

This worked like a charm. Thank you so much!

Translate
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