2013-08-27 90 views
1

我想給喜歡的驗證條件,正則表達式,僅允許數字

  var BlkAccountIdV = $('#txtBlkAccountId').val(); 
    if (BlkAccountIdV == "") {    
     document.getElementById('lblAccountId').innerText = "Enter Valid AccountID"; 
     errorflag=1; 
    } 

如果條件應才執行時輸入的文本框中值包含Letter.What值我能給引號內(如果(BlkAccountIdV ==「」))?

回答

2
var re = /[^0-9]/g; 
var error = re.test(BlkAccountIdV); 

error將是真實的,如果的BlkAccountIdV值不是數字

也就是說,這個正則表達式將匹配除了數字

一切,讓你的代碼看起來應該以某種方式是這樣的:

var BlkAccountIdV = $('#txtBlkAccountId').val(); 
var re = /[^0-9]/g;  
if (re.test(BlkAccountIdV)){ 
    // found a non-numeric value, handling error 
    document.getElementById('lblAccountId').innerText = "Enter Valid AccountID"; 
    errorflag=1; 
} 
0

if (BlkAccountIdV.match(/[0-9]+/) == null)

0

在if條件下可以使用isNaN()函數。這將檢查字符串是否是「不是一個數字」

所以你的情況,如果這個條件無效,然後串號

if(!isNaN(BlkAccountIdV) && BlkAccountIdV != ''){ 
    //Your code 
}