2015-06-01 58 views
0

一個簡單的javascript邏輯運算符驗證不適用於我。Javascript邏輯運算符不驗證

例如。目標價應不低於原單價格

x = input from user; 
y = 1000; // fixed 

if(x > 1000) 
{ 
    alert (x+'should not be greater than'+y); 
} 
else 
{ 
    alert (' proceed'); 
} 

這裏是我的EXAMPLE

+0

您的示例工作正常,在Chrome – Turnip

+0

使用,如果(parseInt函數(targetPrice)> parseInt函數(orginalPrice)) –

回答

0

這些價值string型的大,你需要將它們轉換爲number。你可以使用​​。

targetPrice = Number($('#pdiwap_dropamt').val()); 
    orginalPrice = Number($('#orgprice_wap').val()); 
0

因爲兩個操作數都是字符串,所以它進行字符串比較。

您可以將值轉換爲一個數字,然後不喜歡

targetPrice = +$('#pdiwap_dropamt').val(); 
    orginalPrice = +$('#orgprice_wap').val(); 
0

您compering兩個字符串進行比較。使用像這樣的東西

if (parseInt(targetPrice) > parseInt(orginalPrice)) 
0

只需使用parseInt函數來正確驗證。 Bucause html輸入以字符串格式給出值。

if (parseInt(targetPrice,10) > parseInt(orginalPrice,10)) 

Updated Link

0

我認爲這個問題是這被看作是一個字符串,你得到的輸入。比較前先嚐試轉換爲整數。

例如x = parseInt(來自用戶的輸入);

+0

退房此更新https://jsfiddle.net/5t422enw/3/ – neddinn

0

是的!它的輸入值是一個字符串,所以它沒有正確驗證。

試試這個。

var x = document.getElementById("input").value; 
    var y = 1000; 
    if(!isNaN(x) && x < 1000) { 
     alert ('proceed'); 
    } else { 
     alert(x+'should not be greater than'+y); 
    }