【问题标题】:isFinite of space giving true valueisFinite 的空间赋予真正的价值
【发布时间】:2013-07-04 22:05:57
【问题描述】:

我正在尝试验证价格字段。我正在尝试这个:

var productId = document.getElementById("productId");
var productName = document.getElementById("productName");
var productPrice = document.getElementById("price");

alert(isFinite(productPrice.value));

if(isNaN(productPrice.value)||!isFinite(productPrice.value)){
    error = document.getElementById("priceError");
    error.innerHTML = "Please enter correct value";
    productPrice.style.border="thin solid red";
}else{
    error = document.getElementById("priceError");
    error.innerHTML = "";
}

当输入为空格/多个空格时,行警报给了我真的。 这是我的 HTML 页面。

<td>Price</td>
<td><input type = "text" id = "price" size = "14"/></td>

谢谢

【问题讨论】:

  • 你在productPrice.value有什么?
  • isFinite of any String 其中+string 不是+/- InfinityNaN 将是true

标签: javascript type-coercion


【解决方案1】:

为什么会发生这种情况我不能说,但是这段代码应该可以解决问题

isFinite(parseFloat(" ")) // false
// because -->
parseFloat(" "); // => result NaN
// tested in Chrome 27+ on Win7

在 isNaN 的 MDN 引用中 here 它说

isNaN(" ");     // false: a string with spaces is converted to 0 which is not NaN

更新:

在 isFinite 的参考文献中找到 Here 它指出 isFinite 仅在参数为时才返回 false:

  • NaN
  • 正无穷大,(Number.POSITIVE_INFINITY)
  • 负无穷大 (Number.NEGATIVE_INFINITY) 在任何其他情况下,它都会返回 true。 (就像 Paul S 提到的)

现在我想我已经搞砸了,并且在那门课程中学到了一些东西。 :)

【讨论】:

    【解决方案2】:

    你可以使用正则表达式来做到这一点。

    试试这个。

    function validatePrice() {
        var el = document.getElementById('price');
        if ( 
             el.value.length < 14 && 
             /^ *\+?\d+ *$/.test( el.value )
           )
        {
            return true;
        }
        return false;
    }
    

    此函数检查输入是否为正整数。我不知道你是否也想要浮动值。 如果这样做,请将正则表达式切换为此 /^ *+?\d+((.|,)\d+)? *$/

    【讨论】:

      【解决方案3】:

      对于window.isFinite,您必须了解window.isNaNcoercing types 时遇到的问题。

      window.IsNaN 摘要

      确定一个值是否为 NaN。小心,这个功能是 破碎的。你可能对 ECMAScript 6 Number.isNaN 感兴趣。

      示例

      isNaN(NaN);       // true 
      isNaN(undefined); // true 
      isNaN({});        // true
      
      isNaN(true);      // false
      isNaN(null);      // false
      isNaN(37);       // false
      
      // strings 
      isNaN("37");      // false: "37" is converted to the number 37 which is not NaN 
      isNaN("37.37");   // false: "37.37" is converted to the number 37.37 which is not NaN
      isNaN("");        // false: the empty string is converted to 0 which is not NaN 
      isNaN(" ");  // false: a string with spaces is converted to 0 which is not NaN
      // This is a false positive and the reason why isNaN is not entirely reliable
      
      isNaN("blabla")   // true: "blabla" is converted to a number. Parsing this as a number fails and returns NaN
      

      在 ECMAScript 6 中有解决这些问题的新方法 Number.isNaNNumber.isFinite。 (of course these are not available in many browsers)

      Number.isFinite 等价于

      function isFinite(number) {
          return typeof number === "number" && window.isFinite(number);
      }
      

      因此,作为一种解决方案,您需要考虑这样的事情(跨浏览器)。

      注意:此解决方案仍允许您输入十六进制或科学记数法,“0xA”、“1e10”

      Javascript

      function isFinite(number) {
          return typeof number === "number" && window.isFinite(number);
      }
      
      function trim(string) {
          return string.replace(/^\s+|\s+$/g, "");
      }
      
      var price = document.getElementById("price");
      
      price.onchange = function (e) {
          var evt = e || window.event,
              target = evt.target || evt.srcElement,
              value = trim(target.value) || "NaN",
              number = +value;
      
          console.log("number:", number);
          console.log("isFinite:", isFinite(number));
      }
      

      开启jsfiddle

      【讨论】:

        猜你喜欢
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-31
        • 1970-01-01
        相关资源
        最近更新 更多