簡體   English   中英

isnan函數接受點

[英]isnan function accepting dots

isnan()函數也接受。(點)。 如何預防。 這是示例代碼:

        var Price = $("#Price").val();            
        if (Price == "") {

            alert ("Required!");
        }
        else if (isNaN(Price)) {               

            $("#Price").val(Price);
            alert("Enter digits");

        }

我在文本框的KeyUp事件中調用此JS代碼。

else if (!/^\d+$/.test(Price)) {
    // only digits
}

您可以通過以下方式檢查值是數字

 function isNumber(obj) {
     return isFinite(obj) && !isNaN(parseFloat(obj));
 };

我找到了解決方案:

var Price = "10.00";            
if (Price == "") {
 alert ("Required!");
 }
else if(Price.search(".") != -1)
{
   alert(" cannot insert dots!");
}
else if (isNaN(Price)) {            
 alert("Enter digits");    
 }

此處: http//jsfiddle.net/K8FUW/

試試這個:

else if(price.match(/^[0-9]+$/)){
//Valid number
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM