2011-07-19 120 views
1

目標:
如果輸入數據包含任何字母,我不想檢索任何數據。輸入數據驗證過程出錯

問題:
如果我在變量ddd中輸入了數據「23w」,轉換過程在變量currentvalue中可以接受爲「23」。

我不希望它被轉換成數字,如果輸入數據包含 任何字母。

源代碼是在jQuery中寫的,如果可能的話,在jQuery中檢索新的解決方案會很好。

// Fullmetalboy


var ddd = $('#field_hourInput').val(); 

    var currentValue = parseInt(ddd); 


    // Validating message 
    if(currentValue <= 0) 
    { 
     alert("Value must be positiv"); 
     nonError = false; 
    } 
    else if( (isNaN(currentValue)) && (ddd != "") ) 
    { 
     alert("value must contain numbers"); 
     nonError = false; 
    } 
    else if( (isNaN(currentValue)) && (ddd == "") ) 
    { 
     alert("value must contain value in the textbox"); 
     nonError = false; 
    } 
+3

什麼不正確? – PeeHaa

+1

我喜歡你的「目標」如何開始「我不想要」。 –

回答

0

您可以使用正則表達式來驗證。 Using regex with jquery。並使用正則表達式

[\ d]

這將匹配任何數字應該做的伎倆。

0

將字符串轉換爲int的另一種方法是Number(ddd),它可以滿足您的期望。但你也可以通過正則表達式檢查ddd,這對我來說更好。

正則表達式測試:/^\d+$/.test(ddd)