title: How can I validate calculator input in real time in JavaScript before passing it to operator (=) function?
body: <p>For two days now, I have been trying to validate in real-time in my JavaScript calculator user inputs before passing the overall input to the <strong>operator(=) function</strong>. What I want to achieve is: I don't want the user to input more than one operator sign or dot consecutively. As the user is typing each input I want the calculator to validate each input in realtime, so that if the user input more than one operator sign or dot consecutively the calculator will warn the user and automatically delete the last input and update the validated ones on the calculator screen. For example;</p>
  
  <p>1A) 22.3.2. + 4   -6 **3 ++ 2-^     &lt;-- THIS IS WRONG(not validated)</p>
  
  <p>1B) 22.3+4-6*3+2                    &lt;-- THIS IS WHAT I WANT(validated)</p>
  
  <p>Below is the sample code I wrote so far.</p>
  
  <pre><code>JavaScript
  let userInput = "22.2. + 3 -4^"; //as the user is inputting, i want to be validating it in realtime
  function validateWrongInput() {
  
      let p = userInput;
      for (i = 0; i &lt; p.length; i++){
          if ((p.charAt(i) === "!" || p.charAt(i) === "^" ||
              p.charAt(i) === "*" || p.charAt(i) === "/" || p.charAt(i) === "+" ||
              p.charAt(i) === "-" || p.charAt(i) === "(" || p.charAt(i) === ")") &amp;&amp;
              (p.charAt(i - 1) === "!" || p.charAt(i - 1) === "^" ||
              p.charAt(i - 1) === "*" || p.charAt(i - 1) === "/" ||
              p.charAt(i - 1) === "+" || p.charAt(i - 1) === "-" ||
              p.charAt(i - 1) === "(" || p.charAt(i - 1) === ")")) {
              let a = p.split("");
              a.pop();
              let c = a.join("");
              upDisplay.textContent = c; //This shows the user input as he is typing
              updisplayResult = c;
              downDisplay.textContent = "ERROR-Why Two Operators?";  // this shows the final result when the = sign is pressed
              return true;
          } else {
              return false;
          }
      }
  }
  </code></pre>
  
  <pre><code>JavaScript
  let userInput = "22.2. + 3 -4^"; //as the user is inputting, i want to be validating it in realtime
  function validateDecimal() {
      let p = userInput;
      let pointCount = 0;
      for (let i = 0; i &lt; p.length; i++) {
          if (p.charAt(i) !== "!" &amp;&amp; p.charAt(i) !== "^" &amp;&amp; p.charAt(i) !== "*" &amp;&amp;
              p.charAt(i) !== "/" &amp;&amp; p.charAt(i) !== "+" &amp;&amp; p.charAt(i) !== "-") {
              if (p.charAt(i) === "." &amp;&amp; pointCount &lt; 1) {
                  pointCount += 1
                  downDisplay.textContent = " ";
                  return "ok";
              } else {
                  pointCount = 0;
                  let a = p.split("");
                  a.pop();
                  let c = a.join("");
                  upDisplay.textContent = c; //This shows the user input as he is typing
                  updisplayResult = c;
                  downDisplay.textContent = "ERROR-Why Two Operators?"; // this shows the final result when the = sign is pressed
                  return "ERROR-Why Two Operators?"+c;
              }
          } else {
              pointCount = 0;
          }
      }
  }
  </code></pre>
  
  <p>I am looking for your help.</p>