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

5
  • 1
    Can you show the working code which is "a huge amount of work" ? Commented May 25, 2014 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... Commented May 25, 2014 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 ? Commented May 25, 2014 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 +, -, *, /. Commented May 25, 2014 at 10:48
  • 1
    Possible duplicate. Good candidate for using an external library to parse the expression for you! Commented May 25, 2014 at 10:54

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

If you're going the eval way, at least warn about security.
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.
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.

1 Comment

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
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 ));

1 Comment

You can't make eval safe with a single regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.