给出真值的空间有限

isFinite of space giving true value

本文关键字:空间      更新时间:2023-09-26

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

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 = "";
}

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

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

感谢

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

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

在isNaN的MDN引用中上面写着

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

更新:

在isFinite的引用中,它指出isFinite只在参数为时返回false

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

现在,我想我已经无计可施了,在那门课上我学到了一些东西

对于window.isFinite,您必须意识到window.isNaN在强制类型时会遇到的问题。

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来解决这些问题。(当然,这些在许多浏览器中都不可用)

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 上

您可以使用reqular表达式来完成此操作。

试试这个。

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

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