简体   繁体   English

在将计算器输入传递给运算符 (=) 函数之前,如何在 JavaScript 中实时验证计算器输入?

[英]How can I validate calculator input in real time in JavaScript before passing it to operator (=) function?

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 operator(=) function .两天来,我一直在尝试在我的 JavaScript 计算器中实时验证用户输入,然后再将整体输入传递给operator(=) 函数 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;例如;

1A) 22.3.2. 1A) 22.3.2。 + 4 -6 **3 ++ 2-^ <-- THIS IS WRONG(not validated) + 4 -6 **3 ++ 2-^ <-- 这是错误的(未验证)

1B) 22.3+4-6*3+2 <-- THIS IS WHAT I WANT(validated) 1B) 22.3+4-6*3+2 <-- 这就是我想要的(已验证)

Below is the sample code I wrote so far.下面是我到目前为止编写的示例代码。

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 < 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) === ")") &&
            (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;
        }
    }
}
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 < p.length; i++) {
        if (p.charAt(i) !== "!" && p.charAt(i) !== "^" && p.charAt(i) !== "*" &&
            p.charAt(i) !== "/" && p.charAt(i) !== "+" && p.charAt(i) !== "-") {
            if (p.charAt(i) === "." && pointCount < 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;
        }
    }
}

I am looking for your help.我正在寻找你的帮助。

Checking input while typing打字时检查输入

const input = document.getElementById('userInput');
input.addEventListener('keydown', () => {
    const p = input.value;
    // validation...
})

Sounds like you are looking for an event listener.听起来您正在寻找事件侦听器。 Make sure you have the corresponding html input element, in this case: <input type="text" id="userInput">确保您有相应的 html input 元素,在这种情况下: <input type="text" id="userInput">

Recommendations建议

I believe it is best to let the user finish whatever they are typing and check afterwards.我相信最好让用户完成他们正在输入的任何内容并在之后检查。 Such behaviour can be achieved by using the event listener blur or validating just as it is submitted during the submit event (Not explained, but you may look it up or ask).这种行为可以通过使用事件侦听器blur或在submit事件期间提交时进行验证来实现(未解释,但您可以查找或询问)。

You may find a nicer way of validating your string if you craft a regular expression (regEx) together.如果您一起制作正则表达式 (regEx),您可能会找到一种更好的方法来验证您的字符串。 This is somewhat complicated and should only be done if you feel confident with them.这有点复杂,只有当您对它们有信心时才应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM