Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在使用getElementById验证函数_Javascript_Html_Validation - Fatal编程技术网

Javascript 正在使用getElementById验证函数

Javascript 正在使用getElementById验证函数,javascript,html,validation,Javascript,Html,Validation,我试图使用getElementById验证数量和价格都是整数。如有任何疑难解答帮助,将不胜感激。我还用HTML创建了一些输入 <body> <section> <p><input name="quantity" type="text" id="quantity" placeholder="quantity" autofocus></p> <p><input name="price"

我试图使用
getElementById
验证数量和价格都是整数。如有任何疑难解答帮助,将不胜感激。我还用HTML创建了一些输入

   <body>
   <section>
    <p><input name="quantity" type="text" id="quantity" 
   placeholder="quantity" autofocus></p>
    <p><input name="price" type="text" id="price" placeholder="price"></p>

  </section>
  <script>
    quantity.addEventListener("blur", validate, false);
    price.addEventListener("blur", validate, false);

    function validate() {
      box = this.id;
      data = this.value;
      thequantity = document.getElementById("quantity");
      theprice = document.getElementById("price");
      thetotal = document.getElementById("total");
      if (data) {
        error1 = thequantity.value.match(/[0-9\.]/);
        error2 = thequantity.value.match(/[0-9\.]/);
        if (!error1 && !error2) {
          alert("Please enter a number");
          price = Number(theprice.value);
          quantity = Number(thequantity.value);
          thetotal.value = thequantity.value * theprice.value;
        }
        if (error1) {
          alert("Please enter a number");
          document.getElementById(box).value = "";
        }
        if (error2) {
          alert("Please enter a number");
          document.getElementById(box).value = "";

        }
      } else {
        var num = box.value(/[0-9]/);
        document.getElementById(box).value = thetotal;
      }
    }
  </script>

数量。addEventListener(“模糊”、验证、假); price.addEventListener(“模糊”、验证、假); 函数验证(){ box=this.id; 数据=该值; 数量=document.getElementById(“数量”); 价格=document.getElementById(“价格”); 总计=document.getElementById(“总计”); 如果(数据){ 错误1=数量.值.匹配(/[0-9\.]/); error2=数量.值.匹配(/[0-9\.]/); 如果(!error1&&!error2){ 警报(“请输入一个数字”); 价格=数字(价格值); 数量=数量(数量值); total.value=quantity.value*price.value; } 如果(错误1){ 警报(“请输入一个数字”); document.getElementById(box).value=“”; } 如果(错误2){ 警报(“请输入一个数字”); document.getElementById(box).value=“”; } }否则{ var num=box.value(/[0-9]/); document.getElementById(box).value=总数; } }
只需检查转换为数字是否不会导致
NaN

if (isNaN(Number(document.getElementById("quantity").value))) {
    alert("Please enter a number.");
}

这会验证它们是否为整数吗?@MarkMeyer Yes-如果值是数字,则可以运行
else if
语句。@JBDouble05插入此代码的最佳位置是哪里,对于这两个错误,是否需要放置两次。