0

I have an input type= text that I take the value of to compare to another know value. When the user enters an integer or floating point number it works great. However, if the user enters a calculation say 12/297 or 12 + 9 I would like to perform the calculation and then compare the result to a known value. Any suggestions on how I would do this.

The input field is simply

<input type="text" id="A1">

The code that is getting the value as a float is:

parseFloat(document.getElementById("A1").value

I have tried just taking the value as a string and attempting to manipulate it but no luck. Also I tried tried parsing as a string on the operation sign and then applying the operation to each part of the string but that is a huge amount of work for a tiny issue so there has got to be a better solution.

Thanks

  • 1
    Can you show the working code which is "a huge amount of work" ? – Denys Séguret May 25 '14 at 10:36
  • 3
    The issue is not as tiny as you might think. You effectively have to parse the expression there, meaning reading the string and determining the operands and operators. You *could* experiment with `eval` but then you'll have a lot more programming to do to keep everything secure... – MarioDS May 25 '14 at 10:36
  • Using `eval` would assume the user enters not really a "calculation" but a JavaScript "calculation". This is a big assumption for most users. Will you teach your users how to use the Math object ? – Denys Séguret May 25 '14 at 10:41
  • The "Huge" amount of work was referring to the fact that logically it didn't make sense to me to parse the input on math operations then reconstruct the operation depending on what the parsing operation was +, -, *, /. – user2514231 May 25 '14 at 10:48
  • 1
    [Possible duplicate](http://stackoverflow.com/questions/5066824/safe-evaluation-of-arithmetic-expressions-in-javascript). Good candidate for using an external library to parse the expression for you! – Benji XVI May 25 '14 at 10:54

3 Answers3

1

you can use javascript eval function on the input value. this will evaluate the text as a javascript code. for example the next statement produces the value 7:

eval("2 + 5")

or for your example:

eval(document.getElementById("A1").value)
Gal Ziv
  • 6,890
  • 10
  • 32
  • 43
  • 3
    If you're going the `eval` way, at least warn about security. – MarioDS May 25 '14 at 10:40
  • Thanks I will try this, security for this is not a concern as the use is limited on a closed system. I will make not of the use of eval in case it needs to be publicly available at some point. – user2514231 May 25 '14 at 10:51
1

Forget about eval, it's more trouble than it's worth.

You should parse the string and execute the mathematical expressions in the correct order, which can be quite a big algorithm depending on your needs. I'll leave the implementation up to you, but on a high level it would go about like this:

  1. Cleanup the input: remove all whitespace, it'll improve performance.
  2. Parse the string: loop over the characters and separate numbers from operators. You could end up with for example an array that maintains the order of everything.
  3. Apply the operands in correct order to the numbers left and right from it. Search the array of the previous step for * and / first etc. If you need support for ( and ), it'll make matters more complicated.

Simple example: [10, *, 10, /, 10] is your array. You search for * and /, and you find the first index 1 for *. You apply arr[1-1] * arr[1+1], the new result is [100, /, 10]. You continue , etc.

Of course there are a lot of possible approaches, but the bottom line is: it isn't a "tiny" issue, and "a lot of work" is very relative.

MarioDS
  • 12,895
  • 15
  • 65
  • 121
  • I agree that your answer is correct however for my needs eval will work on a personal application that I need now so the better approach will have to wait. Thansk – user2514231 May 26 '14 at 05:49
0

Since for this problem you should use eval, that isn't secure thing. But you can clean your entry string before this operation:

var expression = document.getElementById("A1").value;

var saveExpression = expression.replace(/[^-()\d/*+.]/g, '');

console.log(eval(saveExpression ));
Artem Petrosian
  • 2,964
  • 1
  • 22
  • 30