2013-07-07 41 views
1

我想验证价格字段。我正在试试这个:isFinite的空间给出真实值

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> 

感谢

+0

你有什么在'productPrice.value'? – mishik

+0

任何_String_的isFinite,其中'+ string'不是'+'/'-''Infinity'或'NaN'将是'true'。 –

+0

@mishik:编辑 – NewUser

回答

2

为什么出现这种情况我不能说,但是这个代码就可以解决这个问题

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

在isNaN here的MDN参考资料 它说

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

Upd吃:

在isFinite的的参考发现Here它指出,如果该说法是isFinite的只有返回false:

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

现在我想我得到了所有松散的结局,并在那个过程中学到了一些东西。 :)

+0

很好的答案。简单! +1 – NewUser

+0

我发现你的答案很好。感谢您的帮助。 – NewUser

0

你可以用reqular表达式来做到这一点。

试试这个。

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

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

0

window.isFinite,您必须知道window.isNaN遭遇的问题coercing 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