如何验证输入的公式是否有效? [英] How Can I Validate An Entered Formula Is Valid Or Not?

查看:183
本文介绍了如何验证输入的公式是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的要求:

用户可以从文本框中添加公式(假设A + B),并将其保存为数据库中的公式表格及之后他可以根据此公式计算其他值,并通过适当的货币换算获得计算出的值。

如果用户输入了有效的公式,那么它是精细。但是如果用户输入(假设A + * B)无效并尝试保存它,我想向用户显示一个弹出窗口,显示这不是一个有效的公式或某事......给用户。 />


那么如何验证输入的公式?

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?

推荐答案

不确定这是否包含您需要的内容,但是值得一试。



Math.js [ ^ ]



一个广泛的JavaScript数学库和Node.js



Math.js是一个用于JavaScript和Node.js的扩展数学库。它具有灵活的表达式解析器,并提供了一个集成的解决方案,可以处理数字,大数字,复数,单位和矩阵。功能强大且易于使用。



功能



支持数字,大数,分数,复数,单位,字符串,数组和矩阵。

与JavaScript的内置数学库兼容。

包含灵活的表达式解析器。

支持链接操作。

附带一大堆内置函数和常量。

没有依赖关系。在任何JavaScript引擎上运行。

易于扩展。

开源。
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.


谢谢大家,

当我浏览 Math.js [^] 时。

我想到了一个想法,我解决了这个问题像这样。



Thank you guys,
When I gone through the Math.js[^].
An idea came to my mind and I solved the issue like this.

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)用数值1替换所有字母。

2)使用jquery eval()评估

3)向用户显示错误。

4)如果尝试失败然后捕获并向用户显示错误

isDataValid =用作标志。

addErrorState()=方法用于向特定的formulaTextArea显示错误。



请注意,这是一个正确的方法。



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.


这篇关于如何验证输入的公式是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆