【问题标题】:Issue validating input field. (js)问题验证输入字段。 (js)
【发布时间】:2017-01-04 05:55:08
【问题描述】:

我需要validInput函数来检查输入的所有输入是否都是数字字符/ 问题是当第一个 if 语句返回 true 时,该函数似乎跳过了输入检查。
例如,如果我在 input1 中输入 1,然后在 input2 中输入“a”,在 input3 中输入 -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;    
        };

【问题讨论】:

  • 错字:if((z == "")||(isNaN(x))||(x &lt;= 0)){ 这些不应该是 if((z == "")||(isNaN(z))||(z &lt;= 0)){ 和 y 一样吗?
  • 我无语了.....请把我投到地狱,让我下次记住这一点。

标签: javascript


【解决方案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;    
        };

【讨论】:

    【解决方案2】:

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

    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;    
        };
    

    【讨论】:

      【解决方案3】:

      您的错误非常微不足道,您将

      这是修改后的代码

                 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;
                  }              
      

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 2021-10-14
        • 1970-01-01
        • 2011-04-16
        • 2014-10-13
        • 2018-12-23
        • 2020-07-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多