发出验证输入字段。 (JS)

时间:2017-01-04 05:55:08

标签: javascript

我需要validInput函数来检查输入的所有输入是否都是数字字符/ 问题是当第一个if语句返回true时,函数似乎跳过输入检查。
例如,如果我为input1输入1,则输入2为“a”,输入3为-3,该函数将返回true并显示结果。

https://codepen.io/regnagleppod/pen/NdWLYx

HTML:

        <label>Starting Number: </label>
        <input id="input1" type="text">
        <br>
        <label>Ending Number: </label>
        <input id="input2" type="text">
        <br>
        <label>Step: </label>
        <input id="input3" type="text">
        <button onclick="return playButton()" id="play">Display Evens</button>

JS:

       function playButton(){
            run();
            if (validInput()){
                showResult();
            }  
        };

       function validInput(){
            var x = document.getElementById("input1").value;
            var y = document.getElementById("input2").value;
            var z = document.getElementById("input3").value;
            if((x == "")||(isNaN(x))||(x <= 0)){
                alert("Please enter positive numberic character only.");
                return false;
            }
            if((y == "")||(isNaN(x))||(x <= 0)){
                alert("Please enter positive numberic character only.");
                return false;
            }
            if((z == "")||(isNaN(x))||(x <= 0)){
                alert("Please enter positive numberic character only.");
                return false;
            }                
            return true;    
        };

3 个答案:

答案 0 :(得分:1)

莫希特是对的,

按如下方式更改功能

function validInput(){
            var x = document.getElementById("startingNum").value;
            var y = document.getElementById("endingNum").value;
            var z = document.getElementById("step").value;
            if((x == "")||(isNaN(x))||(x <= 0)){
                alert("Please enter positive numberic character only.");
                return false;
            }
            if((y == "")||(isNaN(y))||(y <= 0)){
                alert("Please enter positive numberic character only.");
                return false;
            }
            if((z == "")||(isNaN(z))||(z <= 0)){
                alert("Please enter positive numberic character only.");
                return false;
            }                
            return true;    
        };

答案 1 :(得分:1)

我修改了您的代码,请修改代码

function playButton(){
        run();
        if (validInput()){
            showResult();
        }  
    };

   function validInput(){
        var x = document.getElementById("input1").value;
        var y = document.getElementById("input2").value;
        var z = document.getElementById("input3").value;
        if((x == "")||(isNaN(x))||(x <= 0)){
            alert("Please enter positive numberic character only.");
            return false;
        }
        if((y == "")||(isNaN(y))||(y <= 0)){
            alert("Please enter positive numberic character only.");
            return false;
        }
        if((z == "")||(isNaN(z))||(z <= 0)){
            alert("Please enter positive numberic character only.");
            return false;
        }                
        return true;    
    };

答案 2 :(得分:0)

你的错误非常简单,你将&lt; = 0和isNaN与x的值进行比较,即input1。(value),更改第2和第3 if(条件)它将起作用。

以下是更改后的代码

DetectHiddenWindows, On
WinGet, vWinList, List, Untitled - Notepad
Loop, %vWinList%
{
hWnd := vWinList%A_Index%
WinActivate, ahk_id %hWnd%
Sleep 3000
}
Return