1

I have found ways to add the numbers of multiple input fields together and output the result. However I am trying to find a way to add numbers within one field, if possible. I basically want it to work as if it was an excel cell and add up numbers using a + in between each one.

So as the user enters =125+11+110 it automatically is adding those numbers to and displaying the total next to the box.

sampathsris
  • 21,564
  • 12
  • 71
  • 98

5 Answers5

2

You can split the string:

var expression = "125+11+110";
var operands = expression.split("+");
var sum = 0;

for (var i = 0; i < operands.length; i++) {
    sum += parseInt(operands[i]); // use parseFloat if you use decimals as well.
}

But the above example will not know hot to do multiple types of operations in single text box. For example, 100+76-45 will give you incorrect answer.

You can also use eval, but know that it can be harmful if you used it in a wrong way.

var expression = "125+11+110";
var sum = eval(expression);

If you use eval:

  • do not store the strings user submitted in the database without sanitation.
  • do not send back the strings user submitted to the browser as JavaScript.
  • to be safe, just do not even send the user's strings to server at all.
Community
  • 1
  • 1
sampathsris
  • 21,564
  • 12
  • 71
  • 98
0

You should use eval. Like so...

var input = "125+11+110";
var answer = eval(input);

http://www.w3schools.com/jsref/jsref_eval.asp

Trevor Hutto
  • 2,112
  • 4
  • 21
  • 29
0

This is the dirty method. It splits the input between the plus signs and adds up the values in the array:

var userinput = "125+11+110";
var userinputarray = userinput.split("+");
var totalamount = 0;
$(userinputarray).each(function(key,value){
    totalamount += parseFloat(value);
})

It does not handle subtraction. However you can do a regex match instead:

var userinput = "125+11+110";
var userinputarray = userinput.match(/(\+|\-){0,1}[0-9.]{1,}/g);
var totalamount = 0;
$(userinputarray).each(function(key,value){
    totalamount += parseFloat(value);
});

Parsefloat will remove the + symbol, while still using the negative sign.

Jack Cole
  • 1,528
  • 2
  • 19
  • 41
0
var data = $('#inputfeildID').text();
var arr = data.split('+');

var total = 0; 

for(i=0; i < arr.length ; i ++){
  total += arr[i];
}
console.log( total );

When user click enter or submit , first get the value or text in the input field & then split it from + , then add the array elements . you will get the total value of the input box ..

if you want to add those number automatically when user is typing , you have to use jQuery keyup function & when user press + , then add the next number to previous one

Udaya Sri
  • 2,372
  • 2
  • 25
  • 35
0

You can put a keyup listener on the text box. Then in the code fired by the listener, if "myinput" is the id of your input field:

var exp = document.getElementById('myinput').value;
var result = eval(exp);

"result" will be the value of the expression. You should also really check "exp" to see that it is a valid entry, because the user could put any kind of garbage into the input field, and there could also be security issues.

scrayne
  • 701
  • 7
  • 18