Skip to main content
benadamsphoto
Known Participant
March 27, 2018
Answered

Help with Required Field dependant in Drop Down Selection

  • March 27, 2018
  • 1 reply
  • 695 views

Hi there

I’ve got a slight quirk with a basic JavaScript...

I have a custom calculation script on a date field which is Required by default, but needs to become not Required if one of 2 choices (from a total of 7) is made from a preceding drop down menu.

The script on the date field to be dependantly Required currently is:

if (this.getField(“Choice”).value == “5” || value == “6”) {

    event.target.required = false;

}

else {

    event.target.required = true;

}

Now, the above DOES work, i.e. if a choice with the export value 5 or 6 is made the date field is no longer Required, however it will not change back to being Required if another option (export value 1,2,3 etc) is then selected.

However, the date field will correctly flick between Required or not Required if I modify the script to only be dependant on a single export value:

if (this.getField(“Choice”).value == “5”) {

    event.target.required = false;

}

else {

    event.target.required = true;

}

Is there something wrong with the way I have ‘5 or 6’ ??

Hope that makes sense and would massively appreciate a method to fix this!

This topic has been closed for replies.
Correct answer try67

First of all, your code won't work if you'll use "curly" quotes. You must only use straight ones, either single or double.

And yes, there's something wrong with how you're doing it. You have to include the entire condition, or use a variable, like this:

if (this.getField("Choice").valueAsString == "5" || this.getField("Choice").valueAsString == "6") {

Or:

var v = this.getField("Choice").valueAsString;

if (v == "5" || v == "6") {

I also changed it from value to valueAsString, otherwise you might get a number and then it won't work at all.

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 27, 2018

First of all, your code won't work if you'll use "curly" quotes. You must only use straight ones, either single or double.

And yes, there's something wrong with how you're doing it. You have to include the entire condition, or use a variable, like this:

if (this.getField("Choice").valueAsString == "5" || this.getField("Choice").valueAsString == "6") {

Or:

var v = this.getField("Choice").valueAsString;

if (v == "5" || v == "6") {

I also changed it from value to valueAsString, otherwise you might get a number and then it won't work at all.

benadamsphoto
Known Participant
March 27, 2018

try67 you are a legend! Thanks for helping me on here yet again!

Don’t worry, I realise about the curly quotes - I posted from my iPhone and couldn’t see a way to type in plain text.

You have fixed my problem - I went for the first option you provided! Easy when you know how to type ‘or’ - many thanks!