Skip navigation
a.n.bentz
Currently Being Moderated

Simple If-Then Statement

Aug 9, 2012 6:28 AM

Hello,

 

I'm working on a form and I have a question pertaining to three fields: V1, V3, and V4. I want V4 to be able to be the product of V1 and V3, if those values are filled. If not, I want to be able to manually put a number into V4.

 

I've researched and tried various different if,then statements, but none seem to be working. Please help!

 

Thank you so much,

Amanda

 
Replies
  • Currently Being Moderated
    Aug 9, 2012 6:44 AM   in reply to a.n.bentz

    How about using 2 fields, one calculated, and one for user entered value:

     

     

    var v1 = this.getField('V1').valueAsString

    var v2 = this.getField('V3').valueAsString

     

    if (v1 == '' && v2 =='') {

         this.getField('V4_UserEntry').display = display.visible

         this.getField('V4_Calculated').display = display.hidden

    } else {

         this.getField('V4_UserEntry').display = display.hidden

         this.getField('V4_Calculated').display = display.visible

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 7:01 AM   in reply to MarkWalsh

    Another idea would be to use a single field, and calculate the value in the format script. Try something like this to see if it works:

     

    var v1 = this.getField('V1').valueAsString

    var v2 = this.getField('V3').valueAsString

     

     

    if (v1 == '' && v2 =='') {

         event.target.readOnly = false

    } else {

         var prod = // ... do your calculation here

     

         event.target.readOnly = true

         event.value = prod

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 8:57 AM   in reply to a.n.bentz

    This should work in the Custom Calculation script:

     

    var v1 = this.getField('V1').valueAsString

    var v2 = this.getField('V3').valueAsString



    if (v1 == '' && v2 =='') {

         event.target.readonly = false

    } else {

         var prod = this.getField('V1').value * this.getField('V3').value


         event.target.readonly = true

         event.value = prod

    }

     

    I was using the 'Format' script since it would retain the value the user has entered (the Calculate script will replace that value).

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 9:42 AM   in reply to MarkWalsh

    This should work: Put the code in my previous message into the Custom Format script, and put the following into the Custom Calculation script:

     

    event.value = event.value

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 11:13 AM   in reply to a.n.bentz

    Your initial message stated that you needed that to happen if Both fields were filled. If you only need to do the calculation if V3 is filled with a value, and allow entering a value in V4 oherwise, then change the IF line to:

     

     

     

    if (v2 =='') {

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 12:02 PM   in reply to a.n.bentz

    To round the number to 2 digits, Change this:

     

    event.value =prod

    to this:

     

    event.value = Math.round(prod* 100)/100;

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 3:28 PM   in reply to a.n.bentz

    You have 1 double quote, you need to type 2 single quotes. (or 2 double quotes)

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 3:44 PM   in reply to a.n.bentz

    Also, 'event.target.readonly' should not be capitalized (That was my mistake in the first script, the 'O' should be lowercase)

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 10, 2012 7:01 AM   in reply to MarkWalsh

    I would be careful using JavaScript's Math.round method since there are known issues with it working properly for all values.

     

    JavaScript Rounding Errors

    Round-off error

     

    Grade School Sample Files look at the Rounding test, it contains a document level funciton that does not suffer from the rounding error common in JavaScript.

     
    |
    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