Skip to main content
lacyh527
Participant
May 23, 2017
Question

Setting a field required and non-required conditionally

  • May 23, 2017
  • 1 reply
  • 540 views

I have a form with about 8 text fields that are used to mark how many of a certain thing they want to order.  If they change any of the values to more than zero, it sets the budget field as required.  Currently I'm using the following javascript on the onblur action on the 8 text fields:

// test if field A is greater than 0;

if(event.value > 0) {

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

}

This works and it sets the other field as required. But, I'm having an issue with it going back to non-required if they change their mind. Any ideas?  Can I have a hidden field that does a tally and sets it or not? But then how do I subtract values? 

Any help would be appreciated.

Thanks

Lacy

This topic has been closed for replies.

1 reply

Inspiring
May 24, 2017

You have to add code for the false condition of your if statement to set the required property to the false value.

// test if field A is greater than 0;

if(event.value > 0) {

     this.getField("txtGlCode").required = true; // set to required;

} else {

     this.getField("txtGlCode").required = false;  // set to not required;

}  // end if event value greater than zero;

Another approach would be to set the required property to the result of the comparison statement used in the if statement.

// set required property value to result of comparison event greater than zero;

this.getField("txtGlCode").required = (event.value > 0);

The required property of a field is  a Boolean value. Boolean values evaluate to either logical  true or logical false or expressed as  single digit binary value of 1 or zero. 1 is the true condition and zero is the false condition.