使用isNaN和!== 0进行Javascript验证

时间:2016-10-27 11:40:48

标签: javascript validation

我是javascript的新手,我的输入框必须不允许零值或非数字。我原本试图创建一个正则表达式,但我似乎无法让它们中的任何一个正常工作。然后我提出了以下解决方案,但它似乎只在某些时候工作。我认为我的if语句被搞砸了。任何有关代码的帮助,只要使它更好,将不胜感激。

HTML:

<input name="payrate" id="payrate"></td>
<input name="hours" id="hours" value="0" onclick="dollars()" onchange="dollars()"></td>

使用Javascript:

    function dollars(){
  var rate = 0;
  rate= document.getElementById("payrate").value;
  var hours= document.getElementById("hours").value;
  if(!isNaN(hours)){
    // !isNan - not a Number
    // !rate == 0 - value not equal to 0
    if (!isNaN(rate) && !rate == 0) {
      //round value of payrate to 2 decimal places
      var adjrate = Math.round(rate*100)/100;
      document.getElementById("payrate").value="";
      document.getElementById("payrate").value= adjrate;
      for (i=0; i<6; i++){
        document.paycheck['tax'+i].disabled = false;
      }
    }else{
      alert("You entered an invalid rate.\n"+
      "Please enter your hourly pay.\n"+
      "Example: 8.87 value entered: " + rate);
      rate = "";
      disableRadio();
      resetForm();
    }
  }else{
    alert("You entered invalid or empty hours.\n"+
    "Please enter the number of hours worked.\n"+ hours);
    hours = "";
    disableRadio();
    resetForm();
  }
}

3 个答案:

答案 0 :(得分:1)

无需为isNaN检查两次。尝试简化这样的条件:

function dollars(){
  var rate = 0;
  rate= document.getElementById("payrate").value;
  var hours= document.getElementById("hours").value;
  if(!isNaN(hours)){
    // !isNan - not a Number
    // !rate == 0 - value not equal to 0
    if (rate > 0) {
      //round value of payrate to 2 decimal places
      var adjrate = Math.round(rate*100)/100;
      document.getElementById("payrate").value="";
      document.getElementById("payrate").value= adjrate;
      for (i=0; i<6; i++){
        document.paycheck['tax'+i].disabled = false;
      }
    }else{
      alert("You entered an invalid rate.\n"+
      "Please enter your hourly pay.\n"+
      "Example: 8.87 value entered: " + rate);
      rate = "";
      disableRadio();
      resetForm();
    }
  }else{
    alert("You entered invalid or empty hours.\n"+
    "Please enter the number of hours worked.\n"+ hours);
    hours = "";
    disableRadio();
    resetForm();
  }
}

答案 1 :(得分:0)

您可以使用<input type="number" min="0.01" step="0.01" value="0.01">元素。见doc。因此,您将确保价值率和小时数是一个整数。

答案 2 :(得分:0)

作为示例 - 您应该能够添加您需要的任何内容我已经注释掉了一些未包含的其他函数调用 - 但您应该可以从这里开始。

<input name="payrate" id="payrate">
<input name="hours" id="hours" value="0" onclick="dollar()" onkeyup="dollar()">

<script>
function dollar(){
  var rate = document.getElementById("payrate").value;
  var hours= document.getElementById("hours").value;
  if(!hours || isNaN(hours)){
    alert('hours must be a numeric value greater than zeo');
   // disableRadio();
   // resetForm();
    return false; 
  }
  if (!rate || isNaN(rate)) {
    alert('rate must be a numeric value greater than zeo');
    //disableRadio();
    //resetForm();
    return false; 
  }
  var adjrate = Math.round(rate*100)/100;

  /**
   * commented out for example since not included in example code - 
  document.getElementById("payrate").value="";
  document.getElementById("payrate").value= adjrate;
  for (i=0; i<6; i++){
    document.paycheck['tax'+i].disabled = false;
  }
  */

}

</script>