PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Prevent negative number in calculation

  1. I'm building a simple insurance calculator. All is good except one thing. You can enter your deductible whatever that value is subtracts from the calculated total. The only problem is I don't want to happen if your deductible value is GREATER than or equal to the total.

    Is there a simple and clean solution for this? I'd be happy to do it with conditional logic as a solution. This other forum post seemed to indicated I could compare my subtotal: http://www.gravityhelp.com/forums/topic/free-shipping-for-a-certain-postcode-or-if-order-value-is-greater-than-x

    However, that doesn't seem to be the case either. My subtotal field is not in the list of options for conditional logic comparison. Although I'm not sure conditional logic would help either way.

    Any tips?? Thanks!!

    Posted 7 years ago on Thursday January 31, 2013 | Permalink
  2. Do you have a link to the page on your site where we can see the form, and can you tell us what field you want to ensure does not go below zero?

    Posted 7 years ago on Thursday January 31, 2013 | Permalink
  3. Yes, sorry should have done that in the first place. Form is here:
    http://dei.calypsocomhost.com/form/

    You have to select "All Insurances" in Step 1. This brings up the "Deductible" field. This is the field that has be greater than the total on Step 4 or else shouldn't be deducted from the total cost. Ex: If the total cost is $110 and you enter 110 as your deductible if should not reduce your total. It should only do so if the deductible is less than total cost.

    Posted 7 years ago on Thursday January 31, 2013 | Permalink
  4. To constrain an input like that, I think you will need to use some jQuery to validate the field before the form is entered. The calculations in Gravity Forms do not allow something as complex as this with an IF statement (IF $total >= $deductible, THEN $newtotal = $deductible, I think.) The most someone will ever pay is their deductible, correct? And they will always have to pay at least their deductible? Why wouldn't the new total always just be the deductible? I probably don't understand all the complexities in there, but will try to help you if I can follow along.

    Posted 7 years ago on Friday February 1, 2013 | Permalink
  5. Hi Chris...Sorry again, let me try to explain it better. I think we should be able to do this with a little bit of jQuery but I'm still trying to figure out how.

    Entering a number for the deductible will subtract from the total cost. If the deductible amount is greater than or equal to the cost number, the resulting number would remain the same. (Ex: If cost is 350 and deductible is 5000 the resulting number would be 350). In variable terms it would be as such. Note, using subtotal variable for you because the total is a calculation field.

    I'm thinking I need to perform something like this:

    if $deductible >= $subtotal; then $deductible = 0;
    $subtotal = $subtotal - $deductible;

    That really should be all we need to do. The question is how to do this with a combo of jQuery and Gravity Forms.

    Thanks!!

    Posted 7 years ago on Saturday February 2, 2013 | Permalink
  6. To add jQuery to your Gravity Form, you can enter it into an HTML field in the form, or you can add it to the head section of the page where the form is embedded. If you want to add it to the head section of the page, you can use the gform_enqueue_scripts filter:

    http://www.gravityhelp.com/documentation/page/Gform_enqueue_scripts

    The script you write will be pretty simple. It will watch for the inputs and then perform your calculation and populate another field with the result.

    Posted 7 years ago on Monday February 4, 2013 | Permalink
  7. Excellent! Once I get the script sorted I can embed it with this -- thanks! Unfortunately I'm having a heck of a time actually getting my script to change the value. I'm also having trouble comparing the values as they are strings not integers. Do I have to strip out all the other characters "." and "," - I've just started by striping the "$" but didn't go further as I wasn't sure if this was the right way to go.

    My main issue though is I can't seem to change the value of total. In the code below I just have it trying to replace it with "123". Ultimately I would just add the deductible back into the total if condition below is true.

    $("#gform_next_button_2_4").click(function(){
            var deductible = $("#input_2_12").val().replace('$', '');
            var total = $("#ginput_base_price_2_6").val().replace('$', '');
            if ((deductible) >= (total)) {
                $("#ginput_base_price_2_6").val('123');
                $("#input_2_6").val('123');
            }
        });

    I know this is above and beyond, but if you have a few moments to look at this awesome. If not, no worries!

    Posted 7 years ago on Monday February 4, 2013 | Permalink
  8. If you don't want to do string comparisons and want to just compare numbers (and perform math on the numbers), then you will need to strip out any of those extraneous characters, as you have done with the dollar sign.

    Do you have a link to a page where this is in practice now, so we can take a look at how the JavaScript is working with the page?

    Posted 7 years ago on Monday February 4, 2013 | Permalink
  9. Yes, it's at the same place as before... http://dei.calypsocomhost.com/form/

    Thanks! And good to know the character stripping is needed...I figured.

    Posted 7 years ago on Monday February 4, 2013 | Permalink
  10. Did you add your calculations to an external script, or should they be visible in the source of the page? I could not find them.

    Posted 7 years ago on Tuesday February 5, 2013 | Permalink
  11. Currently the bulk of my scripts, including this one, are in my scripts.js file:
    http://dei.calypsocomhost.com/wp-content/themes/imaging/library/js/scripts.js

    Posted 7 years ago on Tuesday February 5, 2013 | Permalink
  12. UPDATE - fixed. code ain't pretty but it works. Thank anyhow.

    Posted 7 years ago on Wednesday February 6, 2013 | Permalink
  13. Are you all sorted out now?

    Posted 7 years ago on Thursday February 7, 2013 | Permalink
  14. Yes, all good for now thank you. Cheers.

    Posted 7 years ago on Thursday February 7, 2013 | Permalink
  15. Thanks for the update Trevor.

    Posted 7 years ago on Thursday February 7, 2013 | Permalink

This topic has been resolved and has been closed to new replies.