Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Set text field to required if a checkbox is checked

Avatar

Level 3

Hi All,

Is there a script that will make a text field required if a checkbox is checked?

Thanks!!!!

6 Replies

Avatar

Level 7

Yes.

For example, suppose you have a checkbox named cbRequired, and a text field named tfSomething that you would make required if the box is checked.

cbRequired::change (javascript)


if(this.rawValue == 1) tfSomething.validate.nullTest= "error";


else tfSomething.validate.nullTest = "disabled";


Avatar

Level 3

I tried adding this code to the click event in the checkbox. It works if I enter text in the required field. I then tab out of the field, tab back into the field and delete the text. Once I exit the field, now the error message shows up. It will only work this way. Am I missing something?

Thanks for your help!

Avatar

Level 7

Making a field required (as shown in the script) is more intended for stopping user from submitting a form via HTTP or email if the fields marked required are empty. (Not filling in required forms doesn't stop the user from printing it out.) If you want to stop a user from leaving a field blank as they're filling out the form, I'd recommend two scripts: one in the exit event of the field they fill out and another one in the change event of the checkbox.

cbRequired::change


if (this.rawValue) xfa.host.setFocus("tfSomething");



tfSomething::exit


if (cbRequired.rawValue && this.isNull) {


  xfa.host.messageBox("Something has to be entered in this field");


  xfa.host.setFocus(this);


}



In plain English: The first script looks for a checkmark in the box. If it finds one, it forces the user to go the field "tfSomething". The second script waits for the user to exit the field. If the checkbox is filled, and the field "tfSomething" is empty, then the user sees an error message, and the cursor is set back to that field. Keep in mind that if the user were to accidentally select the checkbox, this will essentially trap them and force them to enter something into the box before moving on.

Avatar

Level 1

This code worked for me when I asked the same question.

As the Mouse Up event of the check-box enter this code:

this.getField("Text1").required = event.target.value!="Off";

Is there a way to make a text field required if a check box is clicked?

Avatar

Level 1

Hello,

I have a form which has 9 multiple choice check boxes, 7 of those check boxes brings up a box for the user to type in a justification. I would like to make the justification text box a mandatory field if any of the 7 check boxes are selected. However, if  none of them are selected or if all 7 are deselected then I would like the justification box to return to optional. I would also like the form to check that something has been entered in the justification box if any of the 7 boxes are selected before allowing the user to move to the next section.

I came across this post and used the script that jasotastic81 suggested but I cant get it to work properly in the sense that if the user selects the check box then it wont let them deselect it or do anything else until something is typed in the justification box - this is sort of what im after but I would also like to allow the user to deselect the check box at this point if they wanted to.

When using the script below it will only allow you to deselect the check box only after something has been typed in it.

If anyone is able to advise where im going wrong that would be great.

cbRequired::change

  1. if (this.rawValue) xfa.host.setFocus("tfSomething"); 

tfSomething::exit

  1. if (cbRequired.rawValue && this.isNull) { 
  2.   xfa.host.messageBox("Something has to be entered in this field"); 
  3.   xfa.host.setFocus(this); 

Many thanks,

Mark

Avatar

Level 7

You can't uncheck it because you are always forcing the focus to be set on the textField.

If it is blank on exit, you could add code to deselect the checkbox, probably above the setFocus:

this.resolveNode("cbRequired").rawValue = "0";

You could also put code on the textfield so if it isn't null, the checkbox is checked automatically.

If you want to be really tricky and use some extra code, you could do this in tfSomething: exit:

-------------------------------------------------------

var emptyField = app.alert("When cbRequired is checked, tfSomething must not be empty. \n\nDid you want to leave it blank?",2,2, "Blank Field");

if(emptyField == "4") //4 equals yes

{

this.resolveNode("cbRequired").rawValue = "0"; //if it is already blank, just uncheck the checkbox

}

if(emptyField == "3") //3 equals no

{

this.resolveNode("cbRequired").rawValue = "1"; //ensure box is checked

xfa.host.setFocus(this); //if the user didnt intend to leave it blank, focus on the textfield after the checkbox is checked

}

1569744_pastedImage_0.png