输入验证Javacript

时间:2019-04-26 12:15:09

标签: javascript validation input

我需要问用户一个提示,但是他们只能输入某些单词来接受答案,如果用户没有用正确的单词回答,则说它是无效的,并再次提出问题。 br />  这只能通过使用javascript来完成。

var entering = confirm('Confirm to add state or territory');
if (!entering) {
    return;
} else {
    while (true) {
        stInput = prompt('Enter state or territory');

        function validateUserChoice(stInput) {
            if (stInput !== "Queensland" && stInput !== "New South Wales" && stInput !== "Victoria" && stInput !== "Tasmania" && stInput !== "Northern Territory" && stInput !== "Western Australia" && stInput !== "South Australia" && stInput !== "Australian Capital Territory") {
                alert("invalid state or territory");
            } else {
                return false;
            }
        }
        populationInput = parseInt(prompt('Enter population for ' + stInput + ''));

        while (isNaN(populationInput)) {
            alert('Your input was invalid');
            populationInput = parseInt(prompt('Enter population for ' + stInput + ''));
        }
        changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));

        while (isNaN(changeInput)) {
            alert('Your input was invalid');
            changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));
        }

        break;
    }

}

这是我到目前为止所做的,但尚未验证,因此我做了错事,只是不确定。

用户只能使用的单词是:QueenslandNew South WalesVictoriaTasmaniaNorthern TerritoryWestern Australia,{ {1}},South Australia

3 个答案:

答案 0 :(得分:2)

编写 function validateUserChoice(stInput) 函数时,您是在声明一个函数,而不是调用它。因此,您应该声明函数还是将其调用为validateUserChoice(stInput)还是根本不使用函数。

我相信您打算执行以下代码段:

    var entering = confirm('Confirm to add state or territory');
    if (!entering) {
      return;
    } else {
      stInput = prompt('Enter state or territory');
      while (stInput !== "Queensland" && stInput !== "New South Wales" && stInput !== "Victoria" && stInput !== "Tasmania" && stInput !== "Northern Territory" && stInput !== "Western Australia" && stInput !== "South Australia" && stInput !== "Australian Capital Territory") {
        //stay in loop while stInput is not equal any of the strings
        alert("invalid state or territory");
        stInput = prompt('Enter state or territory');
      }
      populationInput = parseInt(prompt('Enter population for ' + stInput + ''));

      while(isNaN(populationInput)) {
        alert('Your input was invalid');
        populationInput = parseInt(prompt('Enter population for ' + stInput + ''));
      }
      changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));

      while(isNaN(changeInput)) {
        alert('Your input was invalid');
        changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));
      }
    }

答案 1 :(得分:1)

您实际上从未真正调用过validateUserChoice(stInput)函数。这是您代码的变更版本:

  var entering = confirm('Confirm to add state or territory');
  if (!entering) {
    return;
  } else {
      while (true) {
        stInput = prompt('Enter state or territory');

        // validate the state/territory and continue to prompt the user
        // until they enter correct data.
        while(!validateUserChoice(stInput)) {
            alert("invalid state or territory"); 
            stInput = prompt('Enter state or territory');
        }

        function validateUserChoice(stInput) {
            // put all valid entries into an array
            var validInputs = [
                "Queensland",
                "New South wales",
                "Victoria",
                "Tasmania",
                "Northern Territory",
                "Western Australia",
                "South Australia",
                "Australian Capital Territory"
            ]

            // if the user's input exists in the array the index will be > -1, otherwise the users
            // input is invalid.
            return validInputs.indexOf(stInput) > -1;
       }
        populationInput = parseInt(prompt('Enter population for ' + stInput + ''));

        while(isNaN(populationInput)) {
            alert('Your input was invalid');
            populationInput = parseInt(prompt('Enter population for ' + stInput + ''));
        }
        changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));

        while(isNaN(changeInput)) {
            alert('Your input was invalid');
            changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));
        }

        break;
      }

  }

我没有单独检查每个字符串,而是将所有可能的状态/区域条目集中到一个数组中,这使您可以简单地检查该数组中是否存在用户输入。如果存在,则用户输入有效,否则无效。

此外,我添加了另一种“同时”外观,该外观将继续提示用户输入有效的州或领地,直到他们提供一个有效的州或领地,就像您为其他入口点所做的一样。

值得注意的是,此验证区分大小写。也就是说,“昆士兰”有效,但“昆士兰”无效。此外,在提示中选择“取消”也被视为无效输入。这些情况可能应该处理。

答案 2 :(得分:0)

希望这就是您正在寻找的东西,

function validate()
{
var entering = prompt("Confirm to add state or territory");
var populationInput='',changeInput='',stInputchangeInput='';
        if (entering) {
          stInput = prompt('Enter state or territory');
          if (stInput != "Queensland" && stInput != "New South Wales" && stInput != "Victoria" && stInput != "Tasmania" && stInput != "Northern Territory" && stInput != "Western Australia" && stInput != "South Australia" && stInput != "Australian Capital Territory") {
                alert("invalid state or territory"); 
                 
          } 
          
          else {
                   
       populationInput = parseInt(prompt('Enter population for ' + stInput + ''));

         while(isNaN(populationInput)) {
                
                alert('Your input was invalid');
                populationInput = parseInt(prompt('Enter population for ' + stInput + ''));
                    
               }
                changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));

                while(isNaN(changeInput)) {
                alert('Your input was invalid');
                changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));
                }

           }
           console.log('State :'+stInput)
           console.log('Population :'+populationInput)
           console.log('Growth Rate :'+changeInput)
       }
     else if(entering !== null)
     validate()
}
validate()