4 Replies Latest reply on Jul 9, 2012 4:05 PM by pzkevco

    Custom Validation based on another field

    KJCONSULTING

      Hi -
      I have not done anything with JavaScript so I apoligize in advance for my ignorance.  I have used VB so as I'm digging around - this makes sense. Here is what I'm trying to do.

       

      Create a form with multiple fields.  One field (Choice) has yes or no for value choices and is required.  Next Field (Date) is required only if the choice is "Yes" and can be blank otherwise.  How would I do this?

       

      Seems pretty simple but can't quite find an example similar enough.

       

      Also, I assume I would put this in the "Custom Validation Script" location in the properties of the Date field and that this validation takes place when the user submits the form.  Is that correct?

       

      Any help would be GREATLY appreciated. Thanks a million!!  These forums are very impressive at  how thorough and active they are.

        • 1. Re: Custom Validation based on another field
          George_Johnson MVP & Adobe Community Professional

          For the field named "Choice", is it a text field, combo box (aka dropdown), check box, or something else?

           

          Note that the validate event of a field is triggered whenever the field value changes, not when the form is submitted.

           

          One approach to this type of situation is to use the calculate event of the date text field, which is triggered whenever any field value in the form changes. It could be something like:

           

          // Custom calculate event for text field

          (function () {

           

              // Get the other field's value

              var val = getField("Choice").valueAsString;

           

              // If the field value is Yes, make this field required, otherwise it's not

              event.target.required = val === "Yes";

           

          })();

           

          The first and last lines are not necessary, but do prevent the unnecessary creation of document-global variables, which is good.

           

          The other approach is to do something similar in the validate or Mouse Up event of the Choice field, but the event and code would depend on the type of field it is.

          1 person found this helpful
          • 2. Re: Custom Validation based on another field
            KJCONSULTING Level 1

            George – thanks for replying. The choice field is a combo box. It is the first field, then if they choose yes, the date field which would be the next field, would be required.  If they choose no – the date field is not necessary. Is that enough information?

            • 3. Re: Custom Validation based on another field
              KJCONSULTING Level 1

              Hi George – I’m still working on this and wondering if you have any further insight knowing the choice field is a combo box. I think the custom calculate may work as well - What do you think?

              • 4. Re: Custom Validation based on another field
                pzkevco

                I think this will work.  It may not be the most elegant solution.

                 

                You could have your choice combo box (Choice) set to No (as the default) and your date text field (Dates in the example below) set to Hidden.

                 

                Place this in the Custom calculate event for the date text field:

                 

                if (this.getField("Choice").value == "Yes"){

                this.getField("Dates").display = display.visible;

                this.getField("Dates").required = true;

                }

                else {

                this.getField("Dates").display = display.hidden;

                this.getField("Dates").required = false;

                }

                 

                If the user chooses Yes, the date text field (Dates) will be visible and required.  If the user chooses No (or takes the default), the date text field (Dates) will be hidden and not required.

                 

                The Javascript can be written this way as well as long as it is in the Custom calculate event for the date text field:

                 

                if (this.getField("Choice").value == "Yes"){

                event.target.display = display.visible;

                event.target.required = true;

                }

                else {

                event.target.display = display.hidden;

                event.target.required = false;

                }