Click here to Skip to main content
15,881,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a requirement like this:
User can add the formula (suppose A + B) from the text box and it is saved as a formula in the database table and later he can calculate the other values as per this formula and get back the calculated value as a result with proper currency convert.
If user entered valid formula then it is fine. but if user enter (suppose A + * B) which is invalid and try to save it, I want to show the user a pop-up showing "This is not a valid formula or something..." to the user.

So how can I validate the entered formula?
Posted
Updated 1-Feb-23 21:52pm
Comments
Mohibur Rashid 20-Jan-16 4:25am    
have you tried google?
Bikram Chhetri 20-Jan-16 4:36am    
I have tried a lot. :)
click here
Mohibur Rashid 20-Jan-16 18:58pm    
have you ever heard of postfix notation or prefix notation?
Andreas Gieriet 20-Jan-16 7:59am    
Regular expression is not suited for this. Regular expressions cannot nest nor count. Having that said, you may do a rough check with regular expression assuming the entered expressions are very basic (i.e. no nesting).
The regex would be something like (.Net regex):
^\s*([-+]?\s*(\d+|\w+))(\s*[-+*/]\s*([-+]?\s*(\d+|\w+)))*\s*$
Cheers
Andi

Not sure if this contains what you need, but it is worth a try.

Math.js[^]

An extensive math library for JavaScript and Node.js

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices. Powerful and easy to use.

Features

Supports numbers, big numbers, fractions, complex numbers, units, strings, arrays, and matrices.
Is compatible with JavaScript's built-in Math library.
Contains a flexible expression parser.
Supports chained operations.
Comes with a large set of built-in functions and constants.
Has no dependencies. Runs on any JavaScript engine.
Is easily extensible.
Open source.
 
Share this answer
 
Thank you guys,
When I gone through the Math.js[^].
An idea came to my mind and I solved the issue like this.

C#
var isDataValid = false;
calculationFormula = calculationFormula.Formula;
<pre>calculationFormula = calculationFormula.replace(/[A-Za-z]/g, "1"); //" ----1 "
      try {
             var expression = eval(calculationFormula); //" ----2 "
             if (isNaN(expression)) {
             addErrorState(formulaTextArea, "Please enter correct formula."); //" ----3 "
             isDataValid = false;
             }
          }
       
          catch (e) { //" ----4 "
          addErrorState(formulaTextArea, "Please enter correct formula."); //" ----3 "
          isDataValid = false;
        }
//" 


1) Replace all the alphabets with numeric value 1.
2) evaluate with jquery eval()
3) showing error to the user.
4) if try fail then catch and show error to the user
isDataValid = use as a flag.
addErrorState() = method use to show error to particular formulaTextArea.

Please suggest, is this a correct approach.
 
Share this answer
 
v2
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900