You are viewing limited content. For full access, please sign in.

Question

Question

How do I perform calculations on fields in Forms?

asked on December 14, 2013 Show version history

 Dear Team

i am creating forms & in forms there is some fields required Calculation . is there any idea how to calculate two field value ? 

for example 

Cost * Qty = Total Value

 

Best Regards

Mazahir Naya

0 0

Answer

APPROVED ANSWER
replied on December 16, 2013

The specific code for performing calculations with field values really depends on the field types you're using, but this page in the online help is a great place to start. Here's the "adding fields together" example from that page, slightly modified to multiply fields.

 

$(document).ready(function () {
    $('.sum').on('blur', 'input', sumtotal);
    function sumtotal() {
        var s = 1;
        $('.sum input').each(function () {
            s *= parseNumber($(this).val());
        });
        $('.total input').val(s);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

The code works by adding the "sum" class to each field that will be multiplied, and the "total" class to the field where the total will go. This example will work if there's one set of fields to be multiplied. If you have several sets on your form, you'll need to differentiate them by using additional classes.

4 0
replied on February 20, 2014

i used the above code and i get the wrong answer.  ive been testing it and when i put lets say field 1= 2 field 2= 2 total= 16.  any ideas?

0 0
replied on February 20, 2014

Did you modify the code at all? How many fields are you multiplying together? Here's a screenshot of the setup mentioned in the example:

 

0 0
replied on October 7, 2014

How would you change this so it would take the value from a field and multiple it by a value in a dropdown?

1 0
replied on June 11, 2015

Update,

I got the same problem as Armando Ontiveros  where the input multiplies itself. That only happens when you use the number or currency input field. It woks fine when you use this code in the "single line" input field

0 0
replied on June 11, 2015

The issue Armando reported is explained in this thread. The values in the input are getting doubled before they run through the calculation.

Instead of using

$('.sum input').each(function () {

you would use

$('.sum input').filter(':visible').each(function () {
1 0
replied on June 11, 2015

Noted, thanks for the update...

0 0

Replies

replied on December 16, 2013 Show version history

Hello Mazahir,

 

This is something easily accomplished with the use of Javascript. How you implement this calculation is up to you, as there are many ways to do it. 

 

Laserfiche provides documentation in the Online Help files for Forms on how to do this calculation one type of way. Refer to the link below to try the recommended way to do this calculation.

 

LINK TO HELP FILE

 

Good Luck

1 0
replied on December 16, 2013

hi to All

Thanks for all your support i found from Laserfiche Help sample code & modify as per my requirement and it is working fine.

 

Best Regards

Mazahir Naya

 

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.