Javascript:为什么我的输入验证不起作用?

时间:2014-06-17 01:06:27

标签: javascript

这是我正在努力的验证,但似乎无法正确。当我输入正确的引脚“12345”时,它可以工作并且功能Done()显示警报“Well Done”。但是,我们在引脚上输入3次失败尝试,它确实显示错误但功能完成仍然在错误后程序应该停止时激活。任何解决方案?

<head>
    <script>
        valid = false;

        function Start(valid) {


            check(valid);


            if (valid = true) {
                Done();
            }
            if (valid = false) {
                alert('End');
                return;
            }

        }

        function check(valid) {

            var PinNum;
            var Attempts = 0;


            while (Attempts < 3) {
                Attempts = (Attempts + 1);
                PinNum = prompt('Enter the pin number');

                if (PinNum == '12345') {
                    alert('Welcome!');
                    valid = true;
                    return valid;
                    return;
                } else {
                    alert('Wrong pin number, this is attemp ' + Attempts + ' of 3');
                }
            }
            alert('Too many failed attempts, giving up');
            return;
        }


        function Done() {
            alert("well done")
        }
    </script>
</head>

<body>
    <button onclick="Start()">Start</button>
</body>

</html>

2 个答案:

答案 0 :(得分:1)

if else声明中,您已将true分配给valid,而非进行比较。您可以将语句简化为:

var valid = false;

function Start() {
    check();

    if (valid) {
        Done();
    } else {
        alert('End');
    }
}

function check() {

    var PinNum;
    var Attempts = 0;

    while (Attempts < 3) {
        Attempts = (Attempts + 1);
        PinNum = prompt('Enter the pin number');

        if (PinNum == '12345') {
            alert('Welcome!');
            valid = true;
            return;
        } else {
            alert('Wrong pin number, this is attemp ' + Attempts + ' of 3');
        }
    }
    alert('Too many failed attempts, giving up');
    return;
}


function Done() {
    alert("well done")
}

demo

答案 1 :(得分:0)

一些项目:

  1. valid函数中的变量Start与您在函数外定义的变量不同。每次单击该按钮,undefined都会传递给该函数。您应该阅读JavaScript范围 - http://www.w3schools.com/js/js_function_closures.asp

  2. valid = true是一项任务。单个等号表示&#34;分配给&#34;而双重或三重等号===意味着比较。您应该阅读比较运算符 - http://www.w3schools.com/js/js_comparisons.asp

  3. 全局变量也被认为是不好的做法,因此最好完全避免使用valid变量。这是一个可能的解决方案 - http://www.jasonbuckboyer.com/playground/stack-overflow/javascript-why-does-my-input-validation-not-work.html


  4. <!DOCTYPE html>
    <html>
    <head>
    <script>
    function start() {
        if (is_valid()) {
            Done();
        } else {
            alert('End');
            return;
        }
    }
    function is_valid() {
        var PinNum,
            Attempts = 0;
        while (Attempts < 3) {
            Attempts = (Attempts + 1);
            PinNum = prompt('Enter the pin number');
            if (PinNum == '12345') {
                alert('Welcome!');
                return true;
            } else {
                alert('Wrong pin number, this is attempt ' + Attempts + ' of 3');
            }
        }
        alert('Too many failed attempts, giving up');
        return false;
    }
    function Done() {
        alert("well done")
    }
    </script>
    </head>
    <body>
       <button onclick="start()">Start</button>
    </body>
    </html>