Skip navigation
KJCONSULTING
Currently Being Moderated

Custom Validation based on another field

Jun 21, 2012 11:03 AM

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.

 
Replies
  • George Johnson
    9,222 posts
    Aug 11, 2002
    Currently Being Moderated
    Jun 21, 2012 11:26 AM   in reply to KJCONSULTING

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 9, 2012 4:05 PM   in reply to KJCONSULTING

    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;

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points