如何将输入限制为仅0-9数字?

时间:2019-09-21 07:30:27

标签: javascript input numbers

我需要以一种只能将1到9的数字输入文本字段的方式编写代码。

无论在文本字段中输入的数字是多少,都会输出该数字从1到9的时间表。

例如,如果输入数字“ 1”:将会列出1 x 1 = 1到1 x 9 = 9。

我不知道如何将输入数字限制为仅1,2,3,4,5,6,7,8和9。这是到目前为止的代码:

<html>
<head>
<script>
function table(){
  var integerInput = document.getElementById("integer");
  var integer = Number(integerInput.value);

  var displayField = document.getElementById("display");
  //Reset the innerHTML when a new integer is inserted
  displayField.innerHTML = "";
  //Loop from 1 -> 9
  for(let i = 1; i <= 9; i++) {
      //Append the current times table to the HTML
      displayField.innerHTML += `${integer} x ${i} = ${integer*i}<br>`
  }
}
</script>
</head>
<body>
Enter an integer from 1 to 9:
  <input id="integer" type="text">
  <button onclick="table()" onclick="table2()">Generate times table</button>
  <p id="display"></p>


</body>
</html>

2 个答案:

答案 0 :(得分:2)

只需确保您的integer变量是1到9之间的整数:

function table() {
  var integerInput = document.getElementById("integer");
  var integer = Number(integerInput.value);
  if (!Number.isInteger(integer) || integer < 1 || integer > 9) {
    throw new Error('Number is not an integer between 1 and 9');
  }
  var displayField = document.getElementById("display");
  //Reset the innerHTML when a new integer is inserted
  displayField.innerHTML = "";
  //Loop from 1 -> 9
  for (let i = 1; i <= 9; i++) {
    //Append the current times table to the HTML
    displayField.innerHTML += `${integer} x ${i} = ${integer*i}<br>`
  }
}
Enter an integer from 1 to 9:
<input id="integer" type="text">
<button onclick="table()" onclick="table2()">Generate times table</button>
<p id="display"></p>

另一种选择是使用格式为[1-9]的表单(尽管不幸的是,您不能将patterninput type="number"组合在一起):

document.querySelector('form').addEventListener('submit', (e) => {
  e.preventDefault();
  var integerInput = document.getElementById("integer");
  var integer = Number(integerInput.value);
  if (!Number.isInteger(integer) || integer < 1 || integer > 9) {
    throw new Error('Number is not an integer between 1 and 9');
  }
  var displayField = document.getElementById("display");
  //Reset the innerHTML when a new integer is inserted
  displayField.innerHTML = "";
  //Loop from 1 -> 9
  for (let i = 1; i <= 9; i++) {
    //Append the current times table to the HTML
    displayField.innerHTML += `${integer} x ${i} = ${integer*i}<br>`
  }
});
Enter an integer from 1 to 9:
<form>
  <input id="integer" pattern="[1-9]">
  <button>Generate times table</button>
  <p id="display"></p>
</form>

答案 1 :(得分:0)

<html>
<head>
<script>
function table(){
  var integerInput = document.getElementById("integer");
  var integer = Number(integerInput.value);

  var displayField = document.getElementById("display");
  //Reset the innerHTML when a new integer is inserted
  displayField.innerHTML = "";
  //Loop from 1 -> 9
  for(let i = 1; i <= 9; i++) {
      //Append the current times table to the HTML
      displayField.innerHTML += `${integer} x ${i} = ${integer*i}<br>`
  }
}

function numbersOnly(input){
    var regex = /[^1-9]/;
    input.value = input.value.replace(regex, "");
}

</script>
</head>
<body>

Enter an integer from 1 to 9:
  <input id="integer" onkeyup="numbersOnly(this)" maxlength="1">
  <button onclick="table()" onclick="table2()">Generate times table</button>
  <p id="display"></p>


</body>
</html>

我所做的主要更改是:

function numbersOnly(input){
    var regex = /[^1-9]/;
    input.value = input.value.replace(regex, "");
}

<input id="integer" onkeyup="numbersOnly(this)" maxlength="1">

修改

<html>
<head>
<script>
function table(){
  var integerInput = document.getElementById("integer");
  var integer = Number(integerInput.value);

  display_iterator(integer, "display")
  display_iterator(integer, "display2")
}

function display_iterator(integer, display_id){
    var displayField = document.getElementById(display_id);
    //Reset the innerHTML when a new integer is inserted
    displayField.innerHTML = "";
    //Loop from 1 -> 9
    for(let i = 1; i <= 9; i++) {
        //Append the current times table to the HTML
        displayField.innerHTML += `${integer} x ${i} = ${integer*i}<br>`
    }
}

function numbersOnly(input){
    var regex = /[^1-9]/;
    input.value = input.value.replace(regex, "");
}

</script>
</head>
<body>
Enter an integer from 1 to 9:
  <input id="integer" onkeyup="numbersOnly(this)" maxlength="1">
  <button onclick="table()">Generate times table</button>
  <table>
      <tr>
          <td>
              <p id="display"></p>
          </td>
          <td>
              <p id="display2"></p>
          </td>
      </tr>
  </table>
</body>
</html>

希望这会有所帮助:)