2

I need to accept a string from a user.

Something like (2*34)+96/2*6-98 and not an illegal string like 4*(2*aggw*^^)*as.,,;

I need to verify if it is a valid arithmetic equation.

kotAPI
  • 1,073
  • 2
  • 13
  • 37
  • Do you just need to recognize valid strings, or do you need to parse and evaluate them too? – abluejelly Dec 07 '15 at 22:49
  • @abluejelly I have to evaluate them too, but I'm only worried about checking if it is a legal arithmetic string or not for now. – kotAPI Dec 07 '15 at 22:52
  • @jperezov I don't wanna evaluate, I wanna check if it is legal. Read the question properly. – kotAPI Dec 07 '15 at 22:53
  • The point is that the question I flagged it a duplicate of should answer yours. You can verify that it's a legal arithmetic string by evaluating it. – jperezov Dec 07 '15 at 22:59
  • Yeah if you're gonna calculate it anyways, I suggest doing both simultaneously, as a lot of the steps you need to go through to validate are going to be repeated. A note: unary is a bitch. – abluejelly Dec 07 '15 at 23:24

2 Answers2

1

I'd set up a function that checks for the following:

  • Invalid pairs of adjacent characters, such as *^, -), and +/, while allowing other unspecified combinations (such as ((,--,+(-()
  • Incorrect parentheses, whether that be the incorrect number of open vs close, or closing parentheses with no corresponding opening ones.
  • Non-numeric or infinite (Infinity keyword) values between specified, allowable operators

document.querySelector("input").addEventListener("keyup", function() {
  document.getElementById("output").innerHTML = isValid(this.value);
});

function isValid(str) {
  var invalidOperatorPairs = ["**", "*/", "/*", "//", "()", "^^", "^/", "/^", "^*", "*^", "-)", "+)", "*)", "/*", "^)", "-*", "-/", "-^", "+*", "+/", "+^", "(*", "(/", "(^","/)","*)","+)","-)","^)"]
  str = "(" + str + ")";
  var open = 0;
  for (var i = 0, len = str.length; i < len; i++) {
    var curr = str[i];
    if (curr === "(") {
      open += 1;
    } else if (curr === ")") {
      open -= 1;
      if (open < 0) {
        return false
      }
    }
    if (i > 0) {
      for (var j = 0, oplen = invalidOperatorPairs.length; j < oplen; j++) {
        if (str[i - 1] == invalidOperatorPairs[j][0] && curr == invalidOperatorPairs[j][1]) {
          return false
        }
      }
    }
  }
  if (open !== 0) return false;
  var sections = str.split(/[\+\-\*\/\^\)\(]/g);
  for (i = 0, len = sections.length; i < len; i++) {
    if ((sections[i].length > 0) &&
      !(Number(sections[i]) !== NaN && isFinite(sections[i]))) {
      return false
    }
  }
  return true;
}
<input type="text" value="" />
<div id="output"></div>
Thriggle
  • 7,009
  • 2
  • 26
  • 37
0

You can use regex in javascript to do so: For example:

var op1= "(2*34)+96/2*6-98";
var op2 = "4*(2*aggw*^^)*as.,,;";

//Regular expression to use
var regEx = /([-+]?[0-9]*\.?[0-9]+[\/\+\-\*])+([-+]?[0-9]*\.?[0-9]+)/g;

//Following will be true
alert(regEx.test(op1));
//Following will be false
alert(regEx.test(op2));

You can probably find better matching regular expression. I found this one here .

Community
  • 1
  • 1
NavidM
  • 1,515
  • 1
  • 16
  • 27
  • 1
    That incorrectly says `25*(53+5` is valid and `2*(3)` is not. If you wanna try and find more: https://jsfiddle.net/a7eg9a78/ – abluejelly Dec 07 '15 at 23:06
  • The last one is good ( http://stackoverflow.com/a/28413269/5116879 ) but doesn't work in Javascript Regex due to a lack of subroutines. – abluejelly Dec 08 '15 at 00:03